Wiki

root/cobra/trunk/Source/Enums.cobra

Revision 2579, 4.5 KB (checked in by Charles.Esterbrook, 10 months ago)

Code cleanup.

  • Property svn:eol-style set to native
Line 
1
2class EnumDecl
3    is partial
4    inherits Container<of EnumMember>
5    implements IBoxMember, INameSpaceMember
6
7    # TODO: defaultAccessLevel = 'public'
8
9    var _attribs as AttributeList
10    var _storageTypeNode as ITypeProxy?
11    var _storageType as IType?
12    var _nativeType as NativeType?
13    var _needScanNativeType as bool
14
15    cue init(parent as IParentSpace?, token as IToken, idToken as IToken, name as String, isNames as List<of String>, attribs as AttributeList, storageTypeNode as ITypeProxy?, docString as String, enumMembers as List<of EnumMember>)
16        base.init(parent, token, name, isNames, docString)
17        _idToken = idToken
18        _attribs = attribs
19        _storageTypeNode = storageTypeNode
20        for em in enumMembers
21            .addDecl(em)
22            em.enumDecl = this
23
24    cue init(parent as IParentSpace?, nativeType as NativeType, isNames as List<of String>, docString as String?)
25        base.init(parent, TokenFix.empty, nativeType.name, isNames, docString)
26        _idToken = Token.empty
27        _attribs = AttributeList()
28        _nativeType = nativeType
29        assert .compiler
30        .backEnd.setUnderlyingType(this)  # sets _storageTypeNode
31        #_storageTypeNode = ClrTypeProxy(Enum.getUnderlyingType((_nativeType to ClrNativeType).backEndType))  # TODO: fix native
32        _needScanNativeType = true
33       
34    def _scanNativeType
35        assert .compiler
36        .backEnd.scanNativeType(this)
37
38    get attributes as AttributeList is override
39        return _attribs
40
41    get canDeclNamesDifferOnlyByCase as bool is override
42        return _nativeType is not nil
43
44    def cobraSourceSignature as String
45        return .idString
46
47    get defaultAccessLevel as String is override
48        return 'public'
49
50    get englishName as String is override
51        return 'enumeration type'
52
53    get idToken from var as IToken
54   
55    def isDescendantOf(type as IType) as bool is override
56        or require .nativeType
57        if .nativeType and not .didBindInh, .bindInh
58        return type is this
59
60    get isFromBinaryLibrary as bool
61        return _nativeType is not nil
62
63    get isShared as bool is override
64        return true
65
66    get isReference as bool is override
67        # enums are not reference types
68        return false
69
70    pro isUsed as bool
71        get
72            return base.isUsed
73        set
74            base.isUsed = value
75            if _needScanNativeType, _scanNativeType
76
77    get nativeType from var
78   
79    get typeForIdentifier as IType is override
80        return .compiler.typeType
81
82    get typeForReceiver as IType is override
83        return this
84
85    def _bindInt is override
86        base._bindInt
87        for attrib in .attributes, attrib.bindInt
88        if _storageTypeNode
89            _storageType = _storageTypeNode.realType
90            if not _storageType.isDescendantOf(.compiler.anyIntType)
91                .throwError('The enumeration storage type must be a primitive integer type such as int8, uint8, int16, uint16, int, uint, int64 or uint64.')
92                if _needScanNativeType, _scanNativeType
93
94    def _bindImp is override
95        base._bindImp
96        for attrib in .attributes, attrib.bindImp
97        # the storage type is always a primitive type like `int` so no _bindImp is necessary
98
99    def extensionMemberFor(box as Box, name as String) as IMember?
100        return nil
101
102    def mangleName(name as String) as String
103        return name
104
105    def memberForName(name as String) as IMember?
106        if _needScanNativeType, _scanNativeType
107        m = base.memberForName(name)
108        if m is nil
109            objClass = .compiler.objectType
110            return objClass.memberForName(name)
111        return m
112
113    def mergedIntoPartialBox(newBox as Box)
114        require
115            newBox is not .parentBox
116            newBox.name == .parentBox.name
117        body
118            _parentBox = newBox
119
120
121class EnumMember
122    is partial
123    inherits NamedNode
124    implements IMember
125    """
126    Holds the name and (optionally) value of a member of an EnumDecl.
127    """
128
129    var _value as int?
130    var _enumDecl as EnumDecl?
131
132    cue init(token as IToken, value as int?)
133        require token.which=='ID'
134        base.init(token, token.text)
135        _value = value
136
137    cue init(name as String, value as int)
138        base.init(name)
139        _value = value
140
141    def addMinFields is override
142        base.addMinFields
143        .addField('value', _value)
144
145    get attributes as AttributeList
146        return AttributeList()
147
148    pro enumDecl from var
149
150    get isCallable as bool
151        return false
152
153    get isShared as bool
154        return true
155
156    get englishName as String
157        return 'enumeration value'
158
159    pro parentNameSpace as NameSpace?
160        get
161            return nil
162        set
163            throw NotSupportedException()
164
165    get requiresThis as bool
166        # This property probably doesn't make much sense for an enum member which must always
167        # be accessed via its containg enum type.
168        return false
169
170    get resultType as IType
171        require .enumDecl
172        return _enumDecl to !
173
174    get typeForIdentifier as IType is override
175        return _enumDecl to !
176   
177    get typeForReceiver as IType is override
178        return _enumDecl to !
179   
180    get value from var
181
182    def unNilReturnType
183        # TODO: can this be axed when IMember gets broken up?
184        pass
Note: See TracBrowser for help on using the browser.