/usr/share/gnu-smalltalk/kernel/CStruct.st is in gnu-smalltalk-common 3.2.4-2.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 | "======================================================================
|
| C struct definition support classes.
|
|
======================================================================"
"======================================================================
|
| Copyright 1992,94,95,99,2000,2001,2002,2008,2009
| 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.
|
======================================================================"
CObject subclass: CCompound [
<shape: #word>
<category: 'Language-C interface'>
<comment: nil>
CCompound class [
| declaration |
]
CCompound class >> gcNew [
"Allocate a new instance of the receiver, backed by garbage-collected
storage."
<category: 'instance creation'>
^self gcAlloc: self sizeof
]
CCompound class >> new [
"Allocate a new instance of the receiver. To free the memory after
GC, remember to call #addToBeFinalized."
<category: 'instance creation'>
^self alloc: self sizeof
]
CCompound class >> sizeof [
"Answer 0, the size of an empty struct"
<category: 'subclass creation'>
^0
]
CCompound class >> alignof [
"Answer 1, the alignment of an empty struct"
<category: 'subclass creation'>
^1
]
CCompound class >> classPragmas [
"Return the pragmas that are written in the file-out of this class."
<category: 'subclass creation'>
^super classPragmas copyWith: #declaration
]
CCompound class >> newStruct: structName declaration: array [
"The old way to create a CStruct. Superseded by #subclass:declaration:..."
<category: 'subclass creation'>
^self
subclass: structName
declaration: array
classVariableNames: ''
poolDictionaries: ''
category: 'Synthetic Class'
]
CCompound class >> subclass: structName declaration: array classVariableNames: cvn poolDictionaries: pd category: category [
"Create a new class with the given name that contains code
to implement the given C struct. All the parameters except
`array' are the same as for a standard class creation message;
see documentation for more information"
<category: 'subclass creation'>
| newClass |
newClass := self
variableWordSubclass: structName asSymbol
instanceVariableNames: ''
classVariableNames: cvn
poolDictionaries: pd
category: category.
newClass declaration: array.
^newClass
]
CCompound class >> declaration [
"Return the description of the fields in the receiver class."
<category: 'subclass creation'>
declaration isNil ifTrue: [declaration := #()].
^declaration
]
CCompound class >> declaration: array [
<category: 'subclass creation'>
self subclassResponsibility
]
CCompound class >> declaration: array inject: startOffset into: aBlock [
"Compile methods that implement the declaration in array. To
compute the offset after each field, the value of the
old offset plus the new field's size is passed to aBlock,
together with the new field's alignment requirements."
<category: 'subclass creation'>
| offset maxAlignment inspStr |
(self declaration notEmpty and: [self declaration ~= array])
ifTrue: [self error: 'cannot redefine CStruct/CUnion'].
declaration := array.
offset := startOffset.
maxAlignment := self superclass alignof.
inspStr := WriteStream on: (String new: 8).
inspStr
nextPutAll: 'fieldSelectorList [';
nl;
nextPutAll: ' ^#('.
"Iterate through each member, doing alignment, size calculations,
and creating accessor methods"
array do:
[:dcl |
| typeDecl name str type |
name := dcl at: 1.
typeDecl := dcl at: 2.
self emitFieldNameTo: inspStr for: name.
type := CType from: typeDecl.
offset := aBlock value: offset value: type alignof.
maxAlignment := type alignof max: maxAlignment.
str := WriteStream on: (String new: 20).
str
nextPutAll: name;
nextPutAll: ' [';
nl;
nextPutAll: ' ^self at: ';
print: offset;
nextPutAll: ' type: ';
store: type;
nl;
nextPut: $].
self compile: str classified: 'accessing'.
offset := offset + type sizeof].
inspStr nextPut: $); nl; nextPut: $].
self compile: inspStr contents classified: 'debugging'.
self compileSize: offset align: maxAlignment
]
CCompound class >> compileSize: size align: alignment [
"Private - Compile sizeof and alignof methods"
<category: 'subclass creation'>
| sizeofMethod alignofMethod |
sizeofMethod := 'sizeof [
^' , (size alignTo: alignment) printString, '
]'.
alignofMethod := 'alignof [
^' , alignment printString, '
]'.
self compile: sizeofMethod classified: 'accessing'.
self class compile: sizeofMethod classified: 'accessing'.
self compile: alignofMethod classified: 'accessing'.
self class compile: alignofMethod classified: 'accessing'
]
CCompound class >> emitFieldNameTo: str for: name [
"Private - Emit onto the given stream the code for adding the
given selector to the CCompound's #examineOn: method."
<category: 'subclass creation'>
str
nl;
next: 8 put: Character space;
nextPut: $#;
nextPutAll: name
]
fieldSelectorList [
"Answer a list of selectors whose return values should be printed
by #examineOn:."
"We can't call subclassResponsibility because #inspect should never
fail. So answer an empty array.
For subclasses, it will answer an Array of the selectors whose
values are to be shown in #examineOn: or a GUI inspector."
<category: 'debugging'>
^#()
]
examineOn: aStream [
"Print the contents of the receiver's fields on aStream"
"This method applies to every instance of the receiver
and their subclasses, which only override #fieldSelectorList."
<category: 'debugging'>
aStream print: self; nl.
self fieldSelectorList do:
[:each |
Transcript
nextPutAll: ' ';
nextPutAll: each;
nextPutAll: ': ';
print: (self perform: each) value;
nl]
]
]
CCompound subclass: CStruct [
<shape: #word>
<category: 'Language-C interface'>
<comment: nil>
CStruct class >> declaration: array [
"Compile methods that implement the declaration in array."
<category: 'subclass creation'>
self
declaration: array
inject: self superclass sizeof
into: [:oldOffset :alignment | oldOffset alignTo: alignment]
]
]
CCompound subclass: CUnion [
<shape: #word>
<category: 'Language-C interface'>
<comment: nil>
CUnion class >> declaration: array [
"Compile methods that implement the declaration in array."
<category: 'subclass creation'>
self
declaration: array
inject: 0
into: [:oldOffset :alignment | 0]
]
]
Integer extend [
alignTo: anInteger [
"Answer the receiver, truncated to the first higher or equal
multiple of anInteger (which must be a power of two)"
<category: 'extension'>
^self + anInteger - 1 bitClear: anInteger - 1
]
]
Eval [
CCompound initialize
]
|