1 define: #Type &basedOn: Types Type &slots: {#dispatcher -> Cloneable clone}.
2 "A dispatching object for statically determining which pidgin method to invoke."
6 resend `>> [dispatcher := t dispatcher clone. ]
9 t@(Type traits) dependencies
10 [Set new `>> [addDependency: t withVisited: Set new. remove: t. ]].
12 c addDependency: t@(Type traits) withVisited: visited
16 t@(Type traits) alignment
18 (bits := t bitSize) >= LongInt bitSize \/ [bits = 0]
19 ifTrue: [LongInt bitSize]
23 t@(Type traits) byteSize
25 (t bitSize + Char bitSize - 1) // Char bitSize
28 t@(Type traits) wordSize
30 (t bitSize + LongInt bitSize - 1) // LongInt bitSize
33 define: #NamedType &basedOn: Type &slots: {#cName}.
35 define: #Pointer &basedOn: Type &slots: {#targetType}.
37 p@(Pointer traits) to: targetType
38 "Creates a new Pointer Type for the targetType."
39 "TODO: Map an IdentityDictionary from types to types of pointers to those
40 types, and unify implicitly?"
42 p clone `>> [targetType := targetType. ]
45 p@(Pointer traits) cName ['P' ; p targetType slateName].
46 p@(Pointer traits) slateName ['UndefinedSlatePointer'].
49 _@(Pointer traits) bitSize
54 t@(Type traits) pointer
59 p@(Pointer traits) = q@(Pointer traits)
60 [p targetType = q targetType].
62 c addDependency: p@(Pointer traits) withVisited: visited
67 (p targetType isSameAs: Synonym)
68 ifTrue: [c addDependency: p targetType withVisited: visited]]
71 define: #Integer &basedOn: Type &slots: {#pointerType. #cPrintName}.
73 _@(Integer traits) baseByteSize [overrideThis].
75 n@(Integer traits) byteSize &platform: p
77 n baseByteSizeOn: (p `defaultsTo: Platform Current)
80 n@(Integer traits) bitSize &platform: p
81 [(n byteSize &platform: p) * 8].
83 n@(Integer traits) cName
84 [n cPrintName `defaultsTo: n printName name fromCamelCase].
86 n@(Integer traits) slateName
89 n@(Integer traits) pointer
90 [n pointerType `defaultsTo: (Pointer to: n)].
92 define: #Signed &basedOn: Integer.
93 define: #Unsigned &basedOn: Integer.
95 define: #Char &basedOn: Signed.
96 _@(Char traits) baseByteSizeOn: _ [1].
97 define: #UnsignedChar &basedOn: Unsigned.
98 _@(UnsignedChar traits) baseByteSizeOn: _ [1].
99 define: #ShortInt &basedOn: Signed.
100 _@(ShortInt traits) baseByteSizeOn: _ [2].
101 define: #UnsignedShortInt &basedOn: Unsigned.
102 _@(UnsignedShortInt traits) baseByteSizeOn: _ [2].
103 define: #LongInt &basedOn: Signed.
104 _@(LongInt traits) baseByteSizeOn: p [p bytesPerWord].
105 define: #UnsignedLongInt &basedOn: Unsigned.
106 _@(UnsignedLongInt traits) baseByteSizeOn: p [p bytesPerWord].
107 define: #LongLongInt &basedOn: Signed.
108 _@(LongLongInt traits) baseByteSizeOn: p [p bytesPerWord * 2].
109 define: #UnsignedLongLongInt &basedOn: Unsigned.
110 _@(UnsignedLongLongInt traits) baseByteSizeOn: p [p bytesPerWord * 2].
111 define: #Float &basedOn: Type.
112 _@(Float traits) baseByteSizeOn: p [4].
113 define: #Double &basedOn: Float.
114 _@(Double traits) baseByteSizeOn: p [8].
115 define: #Void &basedOn: Type.
116 _@(Void traits) baseByteSizeOn: _ [0].
118 define: #FixedInteger &basedOn: Integer.
120 define: #FixedSigned &basedOn: FixedInteger.
121 define: #FixedUnsigned &basedOn: FixedInteger.
123 define: #Modifier &basedOn: Type &slots: {#baseType -> Type}.
125 t@(Modifier traits) cName [t modifierName ; ' ' ; t baseType cName].
127 m@(Modifier traits) for: b
128 [m clone `>> [baseType := b. ]].
130 define: #Mutability &basedOn: Modifier.
131 define: #Const &basedOn: Mutability.
132 _@Const modifierName [#const].
133 t@(Type traits) const [Const for: t].
134 define: #Volatile &basedOn: Mutability.
135 _@Volatile modifierName [#volatile].
136 t@(Type traits) volatile [Volatile for: t].
138 define: #Storage &basedOn: Modifier.
139 Storage traits None ::= Storage clone.
140 Storage traits Auto ::= Storage clone.
141 _@(Storage Auto traits) modifierName [#auto].
142 t@(Type traits) auto [Storage Auto for: t].
143 Storage traits Static ::= Storage clone.
144 _@(Storage Static traits) modifierName [#static].
145 t@(Type traits) static [Storage Static for: t].
146 Storage traits Extern ::= Storage clone.
147 _@(Storage Extern traits) modifierName [#extern].
148 t@(Type traits) extern [Storage Extern for: t].
149 Storage traits Register ::= Storage clone.
150 _@(Storage Register traits) modifierName [#register].
151 t@(Type traits) register [Storage Register for: t].
153 define: #Array &basedOn: Type &slots:
154 {#elementType -> UnsignedLongInt.
157 t@(Array traits) of: elementType size: size
158 [t clone `>> [elementType := elementType. size := size. ]].
160 t@(Array traits) of: elementType
161 [t of: elementType size: Nil].
163 c addDependency: t@(Array traits) withVisited: visited
165 (visited includes: t)
169 c addDependency: t elementType withVisited: visited
173 t@(Array traits) bitSize
175 t elementType bitSize * t size
178 define: #Synonym &basedOn: NamedType
179 &slots: {#targetType. #pointer -> Pointer. #isExported -> False}.
181 t@(Synonym traits) newNamed: name type: type
183 t clone `>> [| :newT |
185 pointer := t pointer to: newT.
186 targetType := type. ]
189 t@(Synonym traits) export
191 t isExported := True.
195 c addDependency: t@(Synonym traits) withVisited: visited
197 (visited includes: t)
201 c addDependency: t targetType withVisited: visited]
204 t@(Synonym traits) bitSize
209 define: #Structure &basedOn: NamedType
210 &slots: {#basis. #pointer -> Pointer. #isExported -> False.
211 #elementSpecs -> ExtensibleArray new}.
213 t@(Structure traits) newNamed: name basedOn: basis
216 [| :newT | cName := name. basis := basis.
217 pointer := t pointer to: newT.
218 "pointer rules rawAddDelegate: #basisPointer valued: basis pointer rules.
219 pointer dispatcher rawAddDelegate: #basisPointer valued: basis pointer dispatcher."
220 elementSpecs := basis elementSpecs copy.
222 [| :se | [| :_ | se type] asMethod: se cName asInferenceRule
226 t@(Structure traits) newNamed: name
228 t clone `>> [| :newT |
231 pointer := t pointer to: newT.
232 elementSpecs := t elementSpecs new. ]
235 t@(Structure traits) export
237 t isExported := True.
241 c addDependency: t@(Structure traits) withVisited: visited
243 (visited includes: t)
248 t elementSpecs do: [| :se | c addDependency: se type withVisited: visited]
252 t@(Structure traits) doElements: block
254 previousSE := t elementSpecs first.
255 block applyTo: {previousSE. 0}.
256 bits := previousSE bitSize.
257 t elementSpecs allButFirstDo:
259 previousSE packed /\ [se packed]
262 alignment := se type alignment.
265 [alignment := alignment max: previousSE type bitSize].
266 bits := (bits / alignment) ceiling * alignment].
267 block applyTo: {se. bits}.
272 t@(Type traits) walkElements: block &bitOffset: offset
274 t walkElements: block withPath: Stack new atBitOffset: (offset ifNil: [0])
277 _@(Type traits) walkElements: _ withPath: _ atBitOffset: _
281 t@(Structure traits) walkElements: block withPath: path atBitOffset: baseOffset
286 block applyTo: {path. baseOffset + offset}.
287 se type walkElements: block withPath: path atBitOffset: baseOffset + offset.
291 t@(Structure traits) unpaddedSize
292 [| lastSE lastOffset |
293 t elementSpecs isEmpty
298 lastOffset := offset].
299 lastOffset + lastSE bitSize
302 t@(Structure traits) alignment
304 t elementSpecs inject: 0 into:
305 [| :maxAlign :se | maxAlign max: se type alignment]
308 t@(Structure traits) bitSize
310 maxAlign := t alignment.
311 (t unpaddedSize / maxAlign) ceiling * maxAlign
315 define: #Union &basedOn: Structure.
317 t@(Union traits) doElements: block
320 [| :se | block applyTo: {se. 0}]
323 t@(Union traits) unpaddedSize
328 maxSize := maxSize max: se type bitSize].
332 define: #StructureElement &basedOn: NamedType
333 &slots: {#type -> UnsignedLongInt}.
335 se@(StructureElement traits) newNamed: name type: type
337 se clone `>> [cName := name. type := type. ]
340 t@(Structure traits) addElement: se
343 do: [| :each | each cName = se cName
344 ifTrue: [warn: 'Attempted Redefinition of type StructureElement, named "' ; each cName ; '".'.
346 [| :_ | se type] asMethod: se cName asInferenceRule on: {t}.
347 [| :_ | se type] asMethod: se cName asInferenceRule on: {t pointer}.
348 t elementSpecs addLast: se
351 t@(Structure traits) addElementNamed: name &type
353 type `defaultsTo: UnsignedLongInt.
354 t addElement: (StructureElement newNamed: name type: type)
357 t@(StructureElement traits) bitSize
362 _@(StructureElement traits) packed
367 define: #PackedStructureElement &basedOn: StructureElement
368 &slots: {#bitSize -> 0}.
370 se@(PackedStructureElement traits) newNamed: name size: size
372 (se newNamed: name type: UnsignedLongInt) `>> [bitSize := size. ]
375 t@(Structure traits) addPackedElementNamed: name size: size
377 t addElement: (PackedStructureElement newNamed: name size: size)
380 _@(PackedStructureElement traits) packed
383 define: #Function &basedOn: Type
384 &slots: {#argumentTypes -> {}. #resultType -> Void}.
386 fun@(Function traits) from: argumentTypes to: resultType
389 [| :newFun | argumentTypes := argumentTypes as: newFun argumentTypes.
390 resultType := resultType. ]
393 define: #FunctionPointer &basedOn: Pointer.
395 fun@(Function traits) pointer
397 FunctionPointer to: fun
400 _@(FunctionPointer traits) from: argumentTypes to: resultType
402 (Function from: argumentTypes to: resultType) pointer
405 define: #Module &basedOn: Type.