/usr/share/gnu-smalltalk/kernel/CType.st is in gnu-smalltalk-common 3.2.5-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | "======================================================================
|
| Base class definition for C data type description objects.
|
|
======================================================================"
"======================================================================
|
| Copyright 1990,91,92,94,95,99,2000,2001,2007,2008
| Free Software Foundation, Inc.
| Written by Steve Byrne.
|
| This file is part of the GNU Smalltalk class library.
|
| The GNU Smalltalk class library is free software; you can redistribute it
| and/or modify it under the terms of the GNU Lesser General Public License
| as published by the Free Software Foundation; either version 2.1, or (at
| your option) any later version.
|
| The GNU Smalltalk class library is distributed in the hope that it will be
| useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
| General Public License for more details.
|
| You should have received a copy of the GNU Lesser General Public License
| along with the GNU Smalltalk class library; see the file COPYING.LIB.
| If not, write to the Free Software Foundation, 59 Temple Place - Suite
| 330, Boston, MA 02110-1301, USA.
|
======================================================================"
Object subclass: CType [
| cObjectType |
<category: 'Language-C interface'>
<comment: 'I am not part of the standard Smalltalk kernel class hierarchy.
I contain type information used by subclasses of CObject, which represents
external C data items.
My only instance variable, cObjectType, is used to hold onto the CObject
subclass that gets created for a given CType. Used primarily in the C part
of the interpreter because internally it cannot execute methods to get values,
so it has a simple way to access instance variable which holds the desired
subclass.
My subclasses have instances which represent the actual data types; for the
scalar types, there is only one instance created of each, but for the
aggregate types, there is at least one instance per base type and/or number of
elements.'>
TypeMap := nil.
CType class >> initialize [
"Initialize the receiver's TypeMap"
<category: 'initialization'>
Smalltalk at: #CObjectType put: (CType cObjectType: CObject).
Smalltalk at: #CCharType put: (CScalarCType cObjectType: CChar).
Smalltalk at: #CUCharType put: (CScalarCType cObjectType: CUChar).
Smalltalk at: #CShortType put: (CScalarCType cObjectType: CShort).
Smalltalk at: #CUShortType put: (CScalarCType cObjectType: CUShort).
Smalltalk at: #CLongType put: (CScalarCType cObjectType: CLong).
Smalltalk at: #CULongType put: (CScalarCType cObjectType: CULong).
Smalltalk at: #CLongLongType put: (CScalarCType cObjectType: CLongLong).
Smalltalk at: #CULongLongType put: (CScalarCType cObjectType: CULongLong).
Smalltalk at: #CIntType put: (CScalarCType cObjectType: CInt).
Smalltalk at: #CUIntType put: (CScalarCType cObjectType: CUInt).
Smalltalk at: #CSmalltalkType put: (CScalarCType cObjectType: CSmalltalk).
Smalltalk at: #CFloatType put: (CScalarCType cObjectType: CFloat).
Smalltalk at: #CDoubleType put: (CScalarCType cObjectType: CDouble).
Smalltalk at: #CLongDoubleType put: (CScalarCType cObjectType: CLongDouble).
Smalltalk at: #CStringType put: (CStringCType cObjectType: CString).
Smalltalk at: #CByteType put: (CScalarCType cObjectType: CByte).
Smalltalk at: #CBooleanType put: (CScalarCType cObjectType: CBoolean).
TypeMap := (IdentityDictionary new)
at: #long put: CLongType;
at: #uLong put: CULongType;
at: #longLong put: CLongLongType;
at: #uLongLong put: CULongLongType;
at: #byte put: CByteType;
at: #char put: CCharType;
at: #uChar put: CUCharType;
at: #uchar put: CUCharType;
at: #short put: CShortType;
at: #uShort put: CUShortType;
at: #ushort put: CUShortType;
at: #int put: CIntType;
at: #uInt put: CUIntType;
at: #uint put: CUIntType;
at: #float put: CFloatType;
at: #double put: CDoubleType;
at: #longDouble put: CLongDoubleType;
at: #string put: CStringType;
at: #smalltalk put: CSmalltalkType;
yourself
]
CType class >> cObjectBinding: aCObjectSubclassBinding [
"Create a new CType for the given subclass of CObject"
<category: 'C instance creation'>
^self basicNew init: aCObjectSubclassBinding
]
CType class >> cObjectType: aCObjectSubclass [
"Create a new CType for the given subclass of CObject"
<category: 'C instance creation'>
^self cObjectBinding: aCObjectSubclass binding
]
CType class >> from: type [
"Private - Pass the size, alignment, and description of CType for aBlock,
given the field description in `type' (the second element of each pair)."
<category: 'C instance creation'>
| typeInfo typeString |
type class == Array ifTrue: [^self computeAggregateType: type].
"must be a type name, either built in or struct, either a Symbol
or an Association"
type isSymbol ifFalse: [^self cObjectBinding: type].
^TypeMap at: type ifAbsent: [(Namespace current at: type) type]
]
CType class >> computeAggregateType: type [
"Private - Called by from: for pointers/arrays.
Format of type:
(#array #int 3) or
(#ptr #{FooStruct})
"
<category: 'C instance creation'>
| structureType |
structureType := type at: 1.
structureType == #array ifTrue: [^CArrayCType from: type].
structureType == #ptr ifTrue: [^CPtrCType from: type]
]
= anObject [
"Return whether the receiver and anObject are equal."
<category: 'basic'>
^self class == anObject class and: [
self cObjectType = anObject cObjectType
]
]
hash [
"Return a hash code for the receiver."
<category: 'basic'>
^self class hash bitXor: self cObjectType hash
]
gcNew [
"Allocate a new CObject with the type (class) identified by the receiver.
The object is movable in memory, but on the other hand it is
garbage-collected automatically."
<category: 'C instance creation'>
^CObject gcAlloc: self sizeof type: self
]
gcNew: anInteger [
"Allocate a new CObject with room for anInteger C object of the type
(class) identified by the receiver. The object is movable
in memory, but on the other hand it is garbage-collected
automatically."
<category: 'C instance creation'>
^CObject gcAlloc: self sizeof * anInteger type: self
]
new [
"Allocate a new CObject with the type (class) identified by the receiver.
It is the caller's responsibility to free the memory allocated for it."
<category: 'C instance creation'>
^CObject alloc: self sizeof type: self
]
new: anInteger [
"Allocate a new CObject with room for anInteger C objects of the type
(class) identified by the receiver. It is the caller's
responsibility to free the memory allocated for it."
<category: 'accessing'>
^CObject alloc: self sizeof * anInteger type: self
]
address: cObjOrInt [
"Create a new CObject with the type (class) identified by the receiver,
pointing to the given address (identified by an Integer or CObject)."
<category: 'C instance creation'>
^(self cObjectType basicNew: 1)
type: self;
address: (cObjOrInt isInteger
ifTrue: [cObjOrInt]
ifFalse: [cObjOrInt address]);
yourself
]
arrayType: size [
"Answer a CArrayCType which represents an array with the given size
of CObjects whose type is in turn represented by the receiver"
<category: 'accessing'>
^CArrayCType elementType: self numberOfElements: size
]
ptrType [
"Answer a CPtrCType which represents a pointer to CObjects whose
type is in turn represented by the receiver"
<category: 'accessing'>
^CPtrCType elementType: self
]
cObjectType [
"Answer the CObject subclass whose instance is created when new is sent
to the receiver"
<category: 'accessing'>
^cObjectType value
]
sizeof [
"Answer the size of the receiver's instances"
<category: 'accessing'>
^self cObjectType sizeof
]
alignof [
"Answer the size of the receiver's instances"
<category: 'accessing'>
^self cObjectType alignof
]
valueType [
"valueType is used as a means to communicate to the interpreter the
underlying type of the data. For anything but scalars, it's just 'self'"
<category: 'accessing'>
^self
]
storeOn: aStream [
"Store Smalltalk code that compiles to the receiver"
<category: 'storing'>
aStream
nextPut: $(;
print: self class;
nextPutAll: ' cObjectType: ';
print: self cObjectType;
nextPut: $)
]
init: aCObjectClass [
"Initialize the receiver's instance variablers"
<category: 'private'>
cObjectType := aCObjectClass
]
]
CType subclass: CScalarCType [
<category: 'Language-C interface'>
<comment: nil>
storeOn: aStream [
"Store Smalltalk code that compiles to the receiver"
<category: 'storing'>
aStream
print: self cObjectType;
nextPutAll: 'Type'
]
valueType [
"valueType is used as a means to communicate to the interpreter the
underlying type of the data. For scalars, it is supplied by the
CObject subclass."
<category: 'accessing'>
^self cObjectType cObjStoredType
]
]
CScalarCType subclass: CStringCType [
<category: 'Language-C interface'>
<comment: nil>
elementType [
"Answer the type of the elements in the receiver's instances"
<category: 'accessing'>
^CCharType
]
]
CType subclass: CPtrCType [
| elementType |
<category: 'Language-C interface'>
<comment: nil>
CPtrCType class >> from: type [
"Private - Called by computeAggregateType: for pointers"
<category: 'instance creation'>
| subType typeInfo |
subType := type at: 2.
typeInfo := CType from: subType.
^self elementType: typeInfo
]
CPtrCType class >> elementType: aCType [
"Answer a new instance of CPtrCType that maps pointers to the given CType"
<category: 'instance creation'>
^(self cObjectType: CPtr)
elementType: aCType;
yourself
]
= anObject [
"Return whether the receiver and anObject are equal."
<category: 'basic'>
^super = anObject and: [self elementType = anObject elementType]
]
hash [
"Return a hash code for the receiver."
<category: 'basic'>
^super hash bitXor: self elementType hash
]
elementType [
"Answer the type of the elements in the receiver's instances"
<category: 'accessing'>
^elementType
]
storeOn: aStream [
<category: 'storing'>
aStream
nextPutAll: '(CPtrCType elementType: ';
store: self elementType;
nextPut: $)
]
elementType: aCType [
"Initialize the receiver's instance variables"
<category: 'private'>
elementType := aCType
]
]
CPtrCType subclass: CArrayCType [
| numElements |
<category: 'Language-C interface'>
<comment: nil>
CArrayCType class >> from: type [
"Private - Called by CType>>from: for arrays"
<category: 'instance creation'>
| numElts elementType typeInfo |
elementType := type at: 2.
numElts := type at: 3.
typeInfo := CType from: elementType.
^self elementType: typeInfo numberOfElements: numElts
]
CArrayCType class >> elementType: aCType [
<category: 'instance creation'>
self shouldNotImplement
]
CArrayCType class >> elementType: aCType numberOfElements: anInteger [
"Answer a new instance of CPtrCType that maps an array whose elements
are of the given CType, and whose size is exactly anInteger elements
(of course, anInteger only matters for allocation, not for access, since
no out-of-bounds protection is provided for C objects)."
<category: 'instance creation'>
^(self cObjectType: CArray)
elementType: aCType;
numberOfElements: anInteger;
yourself
]
= anObject [
"Return whether the receiver and anObject are equal."
<category: 'basic'>
^super = anObject and: [
self numberOfElements = anObject numberOfElements]
]
hash [
"Return a hash code for the receiver."
<category: 'basic'>
^super hash bitXor: self numberOfElements hash
]
storeOn: aStream [
"As with super."
<category: 'storing'>
aStream
nextPutAll: '(CArrayCType elementType: ';
store: self elementType;
nextPutAll: ' numberOfElements: ';
store: numElements asInteger;
nextPut: $)
]
sizeof [
"Answer the size of the receiver's instances"
<category: 'accessing'>
^elementType sizeof * numElements
]
alignof [
"Answer the alignment of the receiver's instances"
<category: 'accessing'>
^elementType alignof
]
numberOfElements [
"Answer the number of elements in the receiver's instances"
<category: 'accessing'>
^numElements
]
numberOfElements: anInteger [
"Initialize the receiver's instance variables"
<category: 'private'>
numElements := anInteger
]
]
Eval [
CType initialize
]
|