1 define: #Type &parents: {Types Type}
2 &slots: {#dispatcher -> Cloneable clone}.
3 "A dispatching object for statically determining which pidgin method to invoke."
7 resend `>> [dispatcher := t dispatcher clone. ]
10 t@(Type traits) dependencies
11 [Set new `>> [addDependency: t withVisited: Set new. remove: t. ]].
13 c addDependency: t@(Type traits) withVisited: visited
17 t@(Type traits) alignment
19 (bits := t bitSize) >= LongInt bitSize \/ [bits = 0]
20 ifTrue: [LongInt bitSize]
24 t@(Type traits) byteSize
26 (t bitSize + Char bitSize - 1) // Char bitSize
29 t@(Type traits) wordSize
31 (t bitSize + LongInt bitSize - 1) // LongInt bitSize
34 define: #NamedType &parents: {Type} &slots: {#cName}.
36 define: #Pointer &parents: {Type} &slots: {#targetType}.
38 p@(Pointer traits) to: targetType
39 "Creates a new Pointer Type for the targetType."
40 "TODO: Map an IdentityDictionary from types to types of pointers to those
41 types, and unify implicitly?"
43 p clone `>> [targetType := targetType. ]
46 p@(Pointer traits) cName ['P' ; p targetType slateName].
47 p@(Pointer traits) slateName ['UndefinedSlatePointer'].
50 _@(Pointer traits) bitSize
55 t@(Type traits) pointer
60 p@(Pointer traits) = q@(Pointer traits)
61 [p targetType = q targetType].
63 c addDependency: p@(Pointer traits) withVisited: visited
68 (p targetType isSameAs: Synonym)
69 ifTrue: [c addDependency: p targetType withVisited: visited]]
72 define: #Integer &parents: {Type} &slots: {#pointerType. #cPrintName}.
74 _@(Integer traits) baseByteSize [overrideThis].
76 n@(Integer traits) byteSize &platform: p
78 n baseByteSizeOn: (p `defaultsTo: Platform Current)
81 n@(Integer traits) bitSize &platform: p
82 [(n byteSize &platform: p) * 8].
84 n@(Integer traits) cName
85 [n cPrintName `defaultsTo: n printName name fromCamelCase].
87 n@(Integer traits) slateName
90 n@(Integer traits) pointer
91 [n pointerType `defaultsTo: (Pointer to: n)].
93 define: #Signed &parents: {Integer}.
94 define: #Unsigned &parents: {Integer}.
96 define: #Char &parents: {Signed}.
97 _@(Char traits) baseByteSizeOn: _ [1].
98 define: #UnsignedChar &parents: {Unsigned}.
99 _@(UnsignedChar traits) baseByteSizeOn: _ [1].
100 define: #ShortInt &parents: {Signed}.
101 _@(ShortInt traits) baseByteSizeOn: _ [2].
102 define: #UnsignedShortInt &parents: {Unsigned}.
103 _@(UnsignedShortInt traits) baseByteSizeOn: _ [2].
104 define: #LongInt &parents: {Signed}.
105 _@(LongInt traits) baseByteSizeOn: p [p bytesPerWord].
106 define: #UnsignedLongInt &parents: {Unsigned}.
107 _@(UnsignedLongInt traits) baseByteSizeOn: p [p bytesPerWord].
108 define: #LongLongInt &parents: {Signed}.
109 _@(LongLongInt traits) baseByteSizeOn: p [p bytesPerWord * 2].
110 define: #UnsignedLongLongInt &parents: {Unsigned}.
111 _@(UnsignedLongLongInt traits) baseByteSizeOn: p [p bytesPerWord * 2].
112 define: #Float &parents: {Type}.
113 _@(Float traits) baseByteSizeOn: p [4].
114 define: #Double &parents: {Float}.
115 _@(Double traits) baseByteSizeOn: p [8].
116 define: #Void &parents: {Type}.
117 _@(Void traits) baseByteSizeOn: _ [0].
119 define: #FixedInteger &parents: {Integer}.
121 define: #FixedSigned &parents: {FixedInteger}.
122 define: #FixedUnsigned &parents: {FixedInteger}.
124 define: #Modifier &parents: {Type}
125 &slots: {#baseType -> Type}.
127 t@(Modifier traits) derive
130 lowerName := newT printName copy toLowercase intern.
131 _@(newT traits) modifierName [lowerName].
132 [| :t | newT for: t] asMethod: lowerName on: {Type traits}.
136 t@(Modifier traits) cName [t modifierName ; ' ' ; t baseType cName].
138 m@(Modifier traits) for: b
139 [m clone `>> [baseType := b. ]].
141 define: #Mutability &parents: {Modifier}.
142 define: #Const &parents: {Mutability}.
143 define: #Volatile &parents: {Mutability}.
145 define: #Storage &parents: {Modifier}.
146 Storage traits None ::= Storage clone.
147 Storage traits Auto ::= Storage clone.
148 Storage traits Static ::= Storage clone.
149 Storage traits Extern ::= Storage clone.
150 Storage traits Register ::= Storage clone.
152 define: #Array &parents: {Type} &slots:
153 {#elementType -> UnsignedLongInt.
156 t@(Array traits) of: elementType size: size
157 [t clone `>> [elementType := elementType. size := size. ]].
159 t@(Array traits) of: elementType
160 [t of: elementType size: Nil].
162 c addDependency: t@(Array traits) withVisited: visited
164 (visited includes: t)
168 c addDependency: t elementType withVisited: visited
172 t@(Array traits) bitSize
174 t elementType bitSize * t size
177 define: #Synonym &parents: {NamedType}
178 &slots: {#targetType. #pointer -> Pointer. #isExported -> False}.
180 t@(Synonym traits) newNamed: name type: type
182 t clone `>> [| :newT |
184 pointer := t pointer to: newT.
185 targetType := type. ]
188 t@(Synonym traits) export
190 t isExported := True.
194 c addDependency: t@(Synonym traits) withVisited: visited
196 (visited includes: t)
200 c addDependency: t targetType withVisited: visited]
203 t@(Synonym traits) bitSize
208 define: #Structure &parents: {NamedType}
209 &slots: {#basis. #pointer -> Pointer. #isExported -> False.
210 #elementSpecs -> ExtensibleArray new}.
212 t@(Structure traits) newNamed: name basedOn: basis
215 [| :newT | cName := name. basis := basis.
216 pointer := t pointer to: newT.
217 "pointer rules rawAddDelegate: #basisPointer valued: basis pointer rules.
218 pointer dispatcher rawAddDelegate: #basisPointer valued: basis pointer dispatcher."
219 elementSpecs := basis elementSpecs copy.
221 [| :se | [| :_ | se type] asMethod: se cName asInferenceRule
225 t@(Structure traits) newNamed: name
227 t clone `>> [| :newT |
230 pointer := t pointer to: newT.
231 elementSpecs := t elementSpecs new. ]
234 t@(Structure traits) export
236 t isExported := True.
240 c addDependency: t@(Structure traits) withVisited: visited
242 (visited includes: t)
247 t elementSpecs do: [| :se | c addDependency: se type withVisited: visited]
251 t@(Structure traits) doElements: block
253 previousSE := t elementSpecs first.
254 block applyTo: {previousSE. 0}.
255 bits := previousSE bitSize.
256 t elementSpecs allButFirstDo:
258 previousSE packed /\ [se packed]
261 alignment := se type alignment.
264 [alignment := alignment max: previousSE type bitSize].
265 bits := (bits / alignment) ceiling * alignment].
266 block applyTo: {se. bits}.
271 t@(Type traits) walkElements: block &bitOffset: offset
273 t walkElements: block withPath: Stack new atBitOffset: (offset ifNil: [0])
276 _@(Type traits) walkElements: _ withPath: _ atBitOffset: _
280 t@(Structure traits) walkElements: block withPath: path atBitOffset: baseOffset
285 block applyTo: {path. baseOffset + offset}.
286 se type walkElements: block withPath: path atBitOffset: baseOffset + offset.
290 t@(Structure traits) unpaddedSize
291 [| lastSE lastOffset |
292 t elementSpecs isEmpty
297 lastOffset := offset].
298 lastOffset + lastSE bitSize
301 t@(Structure traits) alignment
303 t elementSpecs inject: 0 into:
304 [| :maxAlign :se | maxAlign max: se type alignment]
307 t@(Structure traits) bitSize
309 maxAlign := t alignment.
310 (t unpaddedSize / maxAlign) ceiling * maxAlign
314 define: #Union &parents: {Structure}.
316 t@(Union traits) doElements: block
319 [| :se | block applyTo: {se. 0}]
322 t@(Union traits) unpaddedSize
327 maxSize := maxSize max: se type bitSize].
331 define: #StructureElement &parents: {NamedType}
332 &slots: {#type -> UnsignedLongInt}.
334 se@(StructureElement traits) newNamed: name type: type
336 se clone `>> [cName := name. type := type. ]
339 t@(Structure traits) addElement: se
342 do: [| :each | each cName = se cName
343 ifTrue: [warn: 'Attempted Redefinition of type StructureElement, named "' ; each cName ; '".'.
345 [| :_ | se type] asMethod: se cName asInferenceRule on: {t}.
346 [| :_ | se type] asMethod: se cName asInferenceRule on: {t pointer}.
347 t elementSpecs addLast: se
350 t@(Structure traits) addElementNamed: name &type: type
352 type `defaultsTo: UnsignedLongInt.
353 t addElement: (StructureElement newNamed: name type: type)
356 t@(StructureElement traits) bitSize
361 _@(StructureElement traits) packed
366 define: #PackedStructureElement &parents: {StructureElement}
367 &slots: {#bitSize -> 0}.
369 se@(PackedStructureElement traits) newNamed: name size: size
371 (se newNamed: name type: UnsignedLongInt) `>> [bitSize := size. ]
374 t@(Structure traits) addPackedElementNamed: name size: size
376 t addElement: (PackedStructureElement newNamed: name size: size)
379 _@(PackedStructureElement traits) packed
382 define: #Function &parents: {Type}
383 &slots: {#argumentTypes -> {}. #resultType -> Void}.
385 fun@(Function traits) from: argumentTypes to: resultType
388 [| :newFun | argumentTypes := argumentTypes as: newFun argumentTypes.
389 resultType := resultType. ]
392 define: #FunctionPointer &parents: {Pointer}.
394 fun@(Function traits) pointer
396 FunctionPointer to: fun
399 _@(FunctionPointer traits) from: argumentTypes to: resultType
401 (Function from: argumentTypes to: resultType) pointer
404 define: #Module &parents: {Type}.