1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
---|
3 | //
|
---|
4 | // The contents of this file are covered under the licence agreement in the
|
---|
5 | // file "LICENCE" distributed with Cforall.
|
---|
6 | //
|
---|
7 | // DeclarationNode.cc --
|
---|
8 | //
|
---|
9 | // Author : Rodolfo G. Esteves
|
---|
10 | // Created On : Sat May 16 12:34:05 2015
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Thu Mar 16 09:10:57 2017
|
---|
13 | // Update Count : 1007
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <string>
|
---|
17 | #include <list>
|
---|
18 | #include <iterator>
|
---|
19 | #include <algorithm>
|
---|
20 | #include <cassert>
|
---|
21 | #include <strings.h> // ffs
|
---|
22 |
|
---|
23 | #include "TypeData.h"
|
---|
24 |
|
---|
25 | #include "SynTree/Attribute.h"
|
---|
26 | #include "SynTree/Declaration.h"
|
---|
27 | #include "SynTree/Expression.h"
|
---|
28 |
|
---|
29 | #include "TypedefTable.h"
|
---|
30 | extern TypedefTable typedefTable;
|
---|
31 |
|
---|
32 | using namespace std;
|
---|
33 |
|
---|
34 | // These must remain in the same order as the corresponding DeclarationNode enumerations.
|
---|
35 | const char * DeclarationNode::basicTypeNames[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "NoBasicTypeNames" };
|
---|
36 | const char * DeclarationNode::complexTypeNames[] = { "_Complex", "_Imaginary", "NoComplexTypeNames" };
|
---|
37 | const char * DeclarationNode::signednessNames[] = { "signed", "unsigned", "NoSignednessNames" };
|
---|
38 | const char * DeclarationNode::lengthNames[] = { "short", "long", "long long", "NoLengthNames" };
|
---|
39 | const char * DeclarationNode::aggregateNames[] = { "struct", "union", "context", "NoAggregateNames" };
|
---|
40 | const char * DeclarationNode::typeClassNames[] = { "otype", "dtype", "ftype", "NoTypeClassNames" };
|
---|
41 | const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", "NoBuiltinTypeNames" };
|
---|
42 |
|
---|
43 | UniqueName DeclarationNode::anonymous( "__anonymous" );
|
---|
44 |
|
---|
45 | extern LinkageSpec::Spec linkage; // defined in parser.yy
|
---|
46 |
|
---|
47 | DeclarationNode::DeclarationNode() :
|
---|
48 | type( nullptr ),
|
---|
49 | bitfieldWidth( nullptr ),
|
---|
50 | hasEllipsis( false ),
|
---|
51 | linkage( ::linkage ),
|
---|
52 | asmName( nullptr ),
|
---|
53 | initializer( nullptr ),
|
---|
54 | extension( false ),
|
---|
55 | asmStmt( nullptr ) {
|
---|
56 |
|
---|
57 | // variable.name = nullptr;
|
---|
58 | variable.tyClass = NoTypeClass;
|
---|
59 | variable.assertions = nullptr;
|
---|
60 |
|
---|
61 | // attr.name = nullptr;
|
---|
62 | attr.expr = nullptr;
|
---|
63 | attr.type = nullptr;
|
---|
64 | }
|
---|
65 |
|
---|
66 | DeclarationNode::~DeclarationNode() {
|
---|
67 | // delete attr.name;
|
---|
68 | delete attr.expr;
|
---|
69 | delete attr.type;
|
---|
70 |
|
---|
71 | // delete variable.name;
|
---|
72 | delete variable.assertions;
|
---|
73 |
|
---|
74 | delete type;
|
---|
75 | delete bitfieldWidth;
|
---|
76 |
|
---|
77 | delete asmStmt;
|
---|
78 | // asmName, no delete, passed to next stage
|
---|
79 | delete initializer;
|
---|
80 | }
|
---|
81 |
|
---|
82 | DeclarationNode * DeclarationNode::clone() const {
|
---|
83 | DeclarationNode * newnode = new DeclarationNode;
|
---|
84 | newnode->set_next( maybeClone( get_next() ) );
|
---|
85 | newnode->name = name ? new string( *name ) : nullptr;
|
---|
86 |
|
---|
87 | newnode->type = maybeClone( type );
|
---|
88 | newnode->storageClasses = storageClasses;
|
---|
89 | newnode->funcSpecs = funcSpecs;
|
---|
90 | newnode->bitfieldWidth = maybeClone( bitfieldWidth );
|
---|
91 | newnode->enumeratorValue.reset( maybeClone( enumeratorValue.get() ) );
|
---|
92 | newnode->hasEllipsis = hasEllipsis;
|
---|
93 | newnode->linkage = linkage;
|
---|
94 | newnode->asmName = maybeClone( asmName );
|
---|
95 | cloneAll( attributes, newnode->attributes );
|
---|
96 | newnode->initializer = maybeClone( initializer );
|
---|
97 | newnode->extension = extension;
|
---|
98 | newnode->asmStmt = maybeClone( asmStmt );
|
---|
99 | newnode->error = error;
|
---|
100 |
|
---|
101 | // newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr;
|
---|
102 | newnode->variable.tyClass = variable.tyClass;
|
---|
103 | newnode->variable.assertions = maybeClone( variable.assertions );
|
---|
104 |
|
---|
105 | // newnode->attr.name = attr.name ? new string( *attr.name ) : nullptr;
|
---|
106 | newnode->attr.expr = maybeClone( attr.expr );
|
---|
107 | newnode->attr.type = maybeClone( attr.type );
|
---|
108 | return newnode;
|
---|
109 | } // DeclarationNode::clone
|
---|
110 |
|
---|
111 | bool DeclarationNode::get_hasEllipsis() const {
|
---|
112 | return hasEllipsis;
|
---|
113 | }
|
---|
114 |
|
---|
115 | void DeclarationNode::print( std::ostream &os, int indent ) const {
|
---|
116 | os << string( indent, ' ' );
|
---|
117 | if ( name ) {
|
---|
118 | os << *name << ": ";
|
---|
119 | } else {
|
---|
120 | os << "unnamed: ";
|
---|
121 | } // if
|
---|
122 |
|
---|
123 | if ( linkage != LinkageSpec::Cforall ) {
|
---|
124 | os << LinkageSpec::linkageName( linkage ) << " ";
|
---|
125 | } // if
|
---|
126 |
|
---|
127 | storageClasses.print( os );
|
---|
128 | funcSpecs.print( os );
|
---|
129 |
|
---|
130 | if ( type ) {
|
---|
131 | type->print( os, indent );
|
---|
132 | } else {
|
---|
133 | os << "untyped entity ";
|
---|
134 | } // if
|
---|
135 |
|
---|
136 | if ( bitfieldWidth ) {
|
---|
137 | os << endl << string( indent + 2, ' ' ) << "with bitfield width ";
|
---|
138 | bitfieldWidth->printOneLine( os );
|
---|
139 | } // if
|
---|
140 |
|
---|
141 | if ( initializer ) {
|
---|
142 | os << endl << string( indent + 2, ' ' ) << "with initializer ";
|
---|
143 | initializer->printOneLine( os );
|
---|
144 | os << " maybe constructed? " << initializer->get_maybeConstructed();
|
---|
145 |
|
---|
146 | } // if
|
---|
147 |
|
---|
148 | os << endl;
|
---|
149 | }
|
---|
150 |
|
---|
151 | void DeclarationNode::printList( std::ostream &os, int indent ) const {
|
---|
152 | ParseNode::printList( os, indent );
|
---|
153 | if ( hasEllipsis ) {
|
---|
154 | os << string( indent, ' ' ) << "and a variable number of other arguments" << endl;
|
---|
155 | } // if
|
---|
156 | }
|
---|
157 |
|
---|
158 | DeclarationNode * DeclarationNode::newFunction( string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body, bool newStyle ) {
|
---|
159 | DeclarationNode * newnode = new DeclarationNode;
|
---|
160 | newnode->name = name;
|
---|
161 | newnode->type = new TypeData( TypeData::Function );
|
---|
162 | newnode->type->function.params = param;
|
---|
163 | newnode->type->function.newStyle = newStyle;
|
---|
164 | newnode->type->function.body = body;
|
---|
165 |
|
---|
166 | // ignore unnamed routine declarations: void p( int (*)(int) );
|
---|
167 | if ( newnode->name ) {
|
---|
168 | typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID );
|
---|
169 | } // if
|
---|
170 |
|
---|
171 | if ( ret ) {
|
---|
172 | newnode->type->base = ret->type;
|
---|
173 | ret->type = nullptr;
|
---|
174 | delete ret;
|
---|
175 | } // if
|
---|
176 |
|
---|
177 | return newnode;
|
---|
178 | } // DeclarationNode::newFunction
|
---|
179 |
|
---|
180 |
|
---|
181 | DeclarationNode * DeclarationNode::newStorageClass( Type::StorageClasses sc ) {
|
---|
182 | DeclarationNode * newnode = new DeclarationNode;
|
---|
183 | newnode->storageClasses = sc;
|
---|
184 | return newnode;
|
---|
185 | } // DeclarationNode::newStorageClass
|
---|
186 |
|
---|
187 | DeclarationNode * DeclarationNode::newFuncSpecifier( Type::FuncSpecifiers fs ) {
|
---|
188 | DeclarationNode * newnode = new DeclarationNode;
|
---|
189 | newnode->funcSpecs = fs;
|
---|
190 | return newnode;
|
---|
191 | } // DeclarationNode::newFuncSpecifier
|
---|
192 |
|
---|
193 | DeclarationNode * DeclarationNode::newTypeQualifier( Type::Qualifiers tq ) {
|
---|
194 | DeclarationNode * newnode = new DeclarationNode;
|
---|
195 | newnode->type = new TypeData();
|
---|
196 | newnode->type->qualifiers = tq;
|
---|
197 | return newnode;
|
---|
198 | } // DeclarationNode::newQualifier
|
---|
199 |
|
---|
200 | DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
|
---|
201 | DeclarationNode * newnode = new DeclarationNode;
|
---|
202 | newnode->type = new TypeData( TypeData::Basic );
|
---|
203 | newnode->type->basictype = bt;
|
---|
204 | return newnode;
|
---|
205 | } // DeclarationNode::newBasicType
|
---|
206 |
|
---|
207 | DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {
|
---|
208 | DeclarationNode * newnode = new DeclarationNode;
|
---|
209 | newnode->type = new TypeData( TypeData::Basic );
|
---|
210 | newnode->type->complextype = ct;
|
---|
211 | return newnode;
|
---|
212 | } // DeclarationNode::newComplexType
|
---|
213 |
|
---|
214 | DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {
|
---|
215 | DeclarationNode * newnode = new DeclarationNode;
|
---|
216 | newnode->type = new TypeData( TypeData::Basic );
|
---|
217 | newnode->type->signedness = sn;
|
---|
218 | return newnode;
|
---|
219 | } // DeclarationNode::newSignedNess
|
---|
220 |
|
---|
221 | DeclarationNode * DeclarationNode::newLength( Length lnth ) {
|
---|
222 | DeclarationNode * newnode = new DeclarationNode;
|
---|
223 | newnode->type = new TypeData( TypeData::Basic );
|
---|
224 | newnode->type->length = lnth;
|
---|
225 | return newnode;
|
---|
226 | } // DeclarationNode::newLength
|
---|
227 |
|
---|
228 | DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {
|
---|
229 | DeclarationNode * newnode = new DeclarationNode;
|
---|
230 | newnode->type = new TypeData( TypeData::Unknown );
|
---|
231 | newnode->type->forall = forall;
|
---|
232 | return newnode;
|
---|
233 | } // DeclarationNode::newForall
|
---|
234 |
|
---|
235 | DeclarationNode * DeclarationNode::newFromTypedef( string * name ) {
|
---|
236 | DeclarationNode * newnode = new DeclarationNode;
|
---|
237 | newnode->type = new TypeData( TypeData::SymbolicInst );
|
---|
238 | newnode->type->symbolic.name = name;
|
---|
239 | newnode->type->symbolic.isTypedef = true;
|
---|
240 | newnode->type->symbolic.params = nullptr;
|
---|
241 | return newnode;
|
---|
242 | } // DeclarationNode::newFromTypedef
|
---|
243 |
|
---|
244 | DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
|
---|
245 | DeclarationNode * newnode = new DeclarationNode;
|
---|
246 | newnode->type = new TypeData( TypeData::Aggregate );
|
---|
247 | newnode->type->aggregate.kind = kind;
|
---|
248 | if ( name ) {
|
---|
249 | newnode->type->aggregate.name = name;
|
---|
250 | } else { // anonymous aggregate ?
|
---|
251 | newnode->type->aggregate.name = new string( anonymous.newName() );
|
---|
252 | } // if
|
---|
253 | newnode->type->aggregate.actuals = actuals;
|
---|
254 | newnode->type->aggregate.fields = fields;
|
---|
255 | newnode->type->aggregate.body = body;
|
---|
256 | return newnode;
|
---|
257 | } // DeclarationNode::newAggregate
|
---|
258 |
|
---|
259 | DeclarationNode * DeclarationNode::newEnum( string * name, DeclarationNode * constants, bool body ) {
|
---|
260 | DeclarationNode * newnode = new DeclarationNode;
|
---|
261 | newnode->type = new TypeData( TypeData::Enum );
|
---|
262 | if ( name ) {
|
---|
263 | newnode->type->enumeration.name = name;
|
---|
264 | } else { // anonymous aggregate ?
|
---|
265 | newnode->type->enumeration.name = new string( anonymous.newName() );
|
---|
266 | } // if
|
---|
267 | newnode->type->enumeration.constants = constants;
|
---|
268 | newnode->type->enumeration.body = body;
|
---|
269 | return newnode;
|
---|
270 | } // DeclarationNode::newEnum
|
---|
271 |
|
---|
272 | DeclarationNode * DeclarationNode::newEnumConstant( string * name, ExpressionNode * constant ) {
|
---|
273 | DeclarationNode * newnode = new DeclarationNode;
|
---|
274 | newnode->name = name;
|
---|
275 | newnode->enumeratorValue.reset( constant );
|
---|
276 | typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID );
|
---|
277 | return newnode;
|
---|
278 | } // DeclarationNode::newEnumConstant
|
---|
279 |
|
---|
280 | DeclarationNode * DeclarationNode::newName( string * name ) {
|
---|
281 | DeclarationNode * newnode = new DeclarationNode;
|
---|
282 | newnode->name = name;
|
---|
283 | return newnode;
|
---|
284 | } // DeclarationNode::newName
|
---|
285 |
|
---|
286 | DeclarationNode * DeclarationNode::newFromTypeGen( string * name, ExpressionNode * params ) {
|
---|
287 | DeclarationNode * newnode = new DeclarationNode;
|
---|
288 | newnode->type = new TypeData( TypeData::SymbolicInst );
|
---|
289 | newnode->type->symbolic.name = name;
|
---|
290 | newnode->type->symbolic.isTypedef = false;
|
---|
291 | newnode->type->symbolic.actuals = params;
|
---|
292 | return newnode;
|
---|
293 | } // DeclarationNode::newFromTypeGen
|
---|
294 |
|
---|
295 | DeclarationNode * DeclarationNode::newTypeParam( TypeClass tc, string * name ) {
|
---|
296 | DeclarationNode * newnode = new DeclarationNode;
|
---|
297 | newnode->type = nullptr;
|
---|
298 | assert( ! newnode->name );
|
---|
299 | // newnode->variable.name = name;
|
---|
300 | newnode->name = name;
|
---|
301 | newnode->variable.tyClass = tc;
|
---|
302 | newnode->variable.assertions = nullptr;
|
---|
303 | return newnode;
|
---|
304 | } // DeclarationNode::newTypeParam
|
---|
305 |
|
---|
306 | DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode * asserts ) {
|
---|
307 | DeclarationNode * newnode = new DeclarationNode;
|
---|
308 | newnode->type = new TypeData( TypeData::Aggregate );
|
---|
309 | newnode->type->aggregate.name = name;
|
---|
310 | newnode->type->aggregate.kind = Trait;
|
---|
311 | newnode->type->aggregate.params = params;
|
---|
312 | newnode->type->aggregate.fields = asserts;
|
---|
313 | return newnode;
|
---|
314 | } // DeclarationNode::newTrait
|
---|
315 |
|
---|
316 | DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode * params ) {
|
---|
317 | DeclarationNode * newnode = new DeclarationNode;
|
---|
318 | newnode->type = new TypeData( TypeData::AggregateInst );
|
---|
319 | newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate );
|
---|
320 | newnode->type->aggInst.aggregate->aggregate.kind = Trait;
|
---|
321 | newnode->type->aggInst.aggregate->aggregate.name = name;
|
---|
322 | newnode->type->aggInst.params = params;
|
---|
323 | return newnode;
|
---|
324 | } // DeclarationNode::newTraitUse
|
---|
325 |
|
---|
326 | DeclarationNode * DeclarationNode::newTypeDecl( string * name, DeclarationNode * typeParams ) {
|
---|
327 | DeclarationNode * newnode = new DeclarationNode;
|
---|
328 | newnode->type = new TypeData( TypeData::Symbolic );
|
---|
329 | newnode->type->symbolic.isTypedef = false;
|
---|
330 | newnode->type->symbolic.params = typeParams;
|
---|
331 | newnode->type->symbolic.name = name;
|
---|
332 | return newnode;
|
---|
333 | } // DeclarationNode::newTypeDecl
|
---|
334 |
|
---|
335 | DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers ) {
|
---|
336 | DeclarationNode * newnode = new DeclarationNode;
|
---|
337 | newnode->type = new TypeData( TypeData::Pointer );
|
---|
338 | if ( qualifiers ) {
|
---|
339 | return newnode->addQualifiers( qualifiers );
|
---|
340 | } else {
|
---|
341 | return newnode;
|
---|
342 | } // if
|
---|
343 | } // DeclarationNode::newPointer
|
---|
344 |
|
---|
345 | DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) {
|
---|
346 | DeclarationNode * newnode = new DeclarationNode;
|
---|
347 | newnode->type = new TypeData( TypeData::Array );
|
---|
348 | newnode->type->array.dimension = size;
|
---|
349 | newnode->type->array.isStatic = isStatic;
|
---|
350 | if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ConstantExpr * >() ) {
|
---|
351 | newnode->type->array.isVarLen = false;
|
---|
352 | } else {
|
---|
353 | newnode->type->array.isVarLen = true;
|
---|
354 | } // if
|
---|
355 | return newnode->addQualifiers( qualifiers );
|
---|
356 | } // DeclarationNode::newArray
|
---|
357 |
|
---|
358 | DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) {
|
---|
359 | DeclarationNode * newnode = new DeclarationNode;
|
---|
360 | newnode->type = new TypeData( TypeData::Array );
|
---|
361 | newnode->type->array.dimension = nullptr;
|
---|
362 | newnode->type->array.isStatic = false;
|
---|
363 | newnode->type->array.isVarLen = true;
|
---|
364 | return newnode->addQualifiers( qualifiers );
|
---|
365 | }
|
---|
366 |
|
---|
367 | DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) {
|
---|
368 | DeclarationNode * newnode = new DeclarationNode;
|
---|
369 | newnode->bitfieldWidth = size;
|
---|
370 | return newnode;
|
---|
371 | }
|
---|
372 |
|
---|
373 | DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) {
|
---|
374 | DeclarationNode * newnode = new DeclarationNode;
|
---|
375 | newnode->type = new TypeData( TypeData::Tuple );
|
---|
376 | newnode->type->tuple = members;
|
---|
377 | return newnode;
|
---|
378 | }
|
---|
379 |
|
---|
380 | DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) {
|
---|
381 | DeclarationNode * newnode = new DeclarationNode;
|
---|
382 | newnode->type = new TypeData( TypeData::Typeof );
|
---|
383 | newnode->type->typeexpr = expr;
|
---|
384 | return newnode;
|
---|
385 | }
|
---|
386 |
|
---|
387 | DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
|
---|
388 | DeclarationNode * newnode = new DeclarationNode;
|
---|
389 | newnode->type = new TypeData( TypeData::Builtin );
|
---|
390 | newnode->builtin = bt;
|
---|
391 | newnode->type->builtintype = newnode->builtin;
|
---|
392 | return newnode;
|
---|
393 | } // DeclarationNode::newBuiltinType
|
---|
394 |
|
---|
395 | DeclarationNode * DeclarationNode::newAttr( string * name, ExpressionNode * expr ) {
|
---|
396 | DeclarationNode * newnode = new DeclarationNode;
|
---|
397 | newnode->type = nullptr;
|
---|
398 | // newnode->attr.name = name;
|
---|
399 | newnode->name = name;
|
---|
400 | newnode->attr.expr = expr;
|
---|
401 | return newnode;
|
---|
402 | }
|
---|
403 |
|
---|
404 | DeclarationNode * DeclarationNode::newAttr( string * name, DeclarationNode * type ) {
|
---|
405 | DeclarationNode * newnode = new DeclarationNode;
|
---|
406 | newnode->type = nullptr;
|
---|
407 | // newnode->attr.name = name;
|
---|
408 | newnode->name = name;
|
---|
409 | newnode->attr.type = type;
|
---|
410 | return newnode;
|
---|
411 | }
|
---|
412 |
|
---|
413 | DeclarationNode * DeclarationNode::newAttribute( string * name, ExpressionNode * expr ) {
|
---|
414 | DeclarationNode * newnode = new DeclarationNode;
|
---|
415 | newnode->type = nullptr;
|
---|
416 | std::list< Expression * > exprs;
|
---|
417 | buildList( expr, exprs );
|
---|
418 | newnode->attributes.push_back( new Attribute( *name, exprs ) );
|
---|
419 | delete name;
|
---|
420 | return newnode;
|
---|
421 | }
|
---|
422 |
|
---|
423 | DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) {
|
---|
424 | DeclarationNode * newnode = new DeclarationNode;
|
---|
425 | newnode->asmStmt = stmt;
|
---|
426 | return newnode;
|
---|
427 | }
|
---|
428 |
|
---|
429 | void appendError( string & dst, const string & src ) {
|
---|
430 | if ( src.empty() ) return;
|
---|
431 | if ( dst.empty() ) { dst = src; return; }
|
---|
432 | dst += ", " + src;
|
---|
433 | } // appendError
|
---|
434 |
|
---|
435 | void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
|
---|
436 | const Type::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
|
---|
437 |
|
---|
438 | if ( (qsrc.val & qdst.val) != 0 ) { // duplicates ?
|
---|
439 | for ( unsigned int i = 0; i < Type::NumTypeQualifier; i += 1 ) { // find duplicates
|
---|
440 | if ( qsrc[i] && qdst[i] ) {
|
---|
441 | appendError( error, string( "duplicate " ) + Type::Qualifiers::Names[i] );
|
---|
442 | } // if
|
---|
443 | } // for
|
---|
444 | } // for
|
---|
445 | } // DeclarationNode::checkQualifiers
|
---|
446 |
|
---|
447 | void DeclarationNode::checkSpecifiers( DeclarationNode * src ) {
|
---|
448 | if ( (funcSpecs.val & src->funcSpecs.val) != 0 ) { // duplicates ?
|
---|
449 | for ( unsigned int i = 0; i < Type::NumFuncSpecifier; i += 1 ) { // find duplicates
|
---|
450 | if ( funcSpecs[i] && src->funcSpecs[i] ) {
|
---|
451 | appendError( error, string( "duplicate " ) + Type::FuncSpecifiers::Names[i] );
|
---|
452 | } // if
|
---|
453 | } // for
|
---|
454 | } // if
|
---|
455 |
|
---|
456 | if ( storageClasses.any() && src->storageClasses.any() ) { // any reason to check ?
|
---|
457 | if ( (storageClasses.val & src->storageClasses.val ) != 0 ) { // duplicates ?
|
---|
458 | for ( unsigned int i = 0; i < Type::NumStorageClass; i += 1 ) { // find duplicates
|
---|
459 | if ( storageClasses[i] && src->storageClasses[i] ) {
|
---|
460 | appendError( error, string( "duplicate " ) + Type::StorageClasses::Names[i] );
|
---|
461 | } // if
|
---|
462 | } // for
|
---|
463 | // src is the new item being added and has a single bit
|
---|
464 | } else if ( ! src->storageClasses.is_threadlocal ) { // conflict ?
|
---|
465 | appendError( error, string( "conflicting " ) + Type::StorageClasses::Names[ffs( storageClasses.val ) - 1] +
|
---|
466 | " & " + Type::StorageClasses::Names[ffs( src->storageClasses.val ) - 1] );
|
---|
467 | src->storageClasses.val = 0; // FIX to preserve invariant of one basic storage specifier
|
---|
468 | } // if
|
---|
469 | } // if
|
---|
470 |
|
---|
471 | appendError( error, src->error );
|
---|
472 | } // DeclarationNode::checkSpecifiers
|
---|
473 |
|
---|
474 | DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q ) {
|
---|
475 | funcSpecs.val |= q->funcSpecs.val;
|
---|
476 | storageClasses.val |= q->storageClasses.val;
|
---|
477 |
|
---|
478 | for ( Attribute *attr: reverseIterate( q->attributes ) ) {
|
---|
479 | attributes.push_front( attr->clone() );
|
---|
480 | } // for
|
---|
481 | return this;
|
---|
482 | } // DeclarationNode::copySpecifiers
|
---|
483 |
|
---|
484 | static void addQualifiersToType( TypeData *&src, TypeData * dst ) {
|
---|
485 | if ( src->forall && dst->kind == TypeData::Function ) {
|
---|
486 | if ( dst->forall ) {
|
---|
487 | dst->forall->appendList( src->forall );
|
---|
488 | } else {
|
---|
489 | dst->forall = src->forall;
|
---|
490 | } // if
|
---|
491 | src->forall = nullptr;
|
---|
492 | } // if
|
---|
493 | if ( dst->base ) {
|
---|
494 | addQualifiersToType( src, dst->base );
|
---|
495 | } else if ( dst->kind == TypeData::Function ) {
|
---|
496 | dst->base = src;
|
---|
497 | src = nullptr;
|
---|
498 | } else {
|
---|
499 | dst->qualifiers += src->qualifiers;
|
---|
500 | } // if
|
---|
501 | } // addQualifiersToType
|
---|
502 |
|
---|
503 | DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
|
---|
504 | if ( ! q ) { delete q; return this; }
|
---|
505 |
|
---|
506 | checkSpecifiers( q );
|
---|
507 | copySpecifiers( q );
|
---|
508 |
|
---|
509 | if ( ! q->type ) {
|
---|
510 | delete q;
|
---|
511 | return this;
|
---|
512 | } // if
|
---|
513 |
|
---|
514 | if ( ! type ) {
|
---|
515 | type = q->type; // reuse this structure
|
---|
516 | q->type = nullptr;
|
---|
517 | delete q;
|
---|
518 | return this;
|
---|
519 | } // if
|
---|
520 |
|
---|
521 | if ( q->type->forall ) {
|
---|
522 | if ( type->forall ) {
|
---|
523 | type->forall->appendList( q->type->forall );
|
---|
524 | } else {
|
---|
525 | if ( type->kind == TypeData::Aggregate ) {
|
---|
526 | type->aggregate.params = q->type->forall;
|
---|
527 | // change implicit typedef from TYPEDEFname to TYPEGENname
|
---|
528 | typedefTable.changeKind( *type->aggregate.name, TypedefTable::TG );
|
---|
529 | } else {
|
---|
530 | type->forall = q->type->forall;
|
---|
531 | } // if
|
---|
532 | } // if
|
---|
533 | q->type->forall = nullptr;
|
---|
534 | } // if
|
---|
535 |
|
---|
536 | checkQualifiers( type, q->type );
|
---|
537 | addQualifiersToType( q->type, type );
|
---|
538 |
|
---|
539 | delete q;
|
---|
540 | return this;
|
---|
541 | } // addQualifiers
|
---|
542 |
|
---|
543 | static void addTypeToType( TypeData *&src, TypeData *&dst ) {
|
---|
544 | if ( src->forall && dst->kind == TypeData::Function ) {
|
---|
545 | if ( dst->forall ) {
|
---|
546 | dst->forall->appendList( src->forall );
|
---|
547 | } else {
|
---|
548 | dst->forall = src->forall;
|
---|
549 | } // if
|
---|
550 | src->forall = nullptr;
|
---|
551 | } // if
|
---|
552 | if ( dst->base ) {
|
---|
553 | addTypeToType( src, dst->base );
|
---|
554 | } else {
|
---|
555 | switch ( dst->kind ) {
|
---|
556 | case TypeData::Unknown:
|
---|
557 | src->qualifiers += dst->qualifiers;
|
---|
558 | dst = src;
|
---|
559 | src = nullptr;
|
---|
560 | break;
|
---|
561 | case TypeData::Basic:
|
---|
562 | dst->qualifiers += src->qualifiers;
|
---|
563 | if ( src->kind != TypeData::Unknown ) {
|
---|
564 | assert( src->kind == TypeData::Basic );
|
---|
565 |
|
---|
566 | if ( dst->basictype == DeclarationNode::NoBasicType ) {
|
---|
567 | dst->basictype = src->basictype;
|
---|
568 | } else if ( src->basictype != DeclarationNode::NoBasicType )
|
---|
569 | throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: ", src );
|
---|
570 |
|
---|
571 | if ( dst->complextype == DeclarationNode::NoComplexType ) {
|
---|
572 | dst->complextype = src->complextype;
|
---|
573 | } else if ( src->complextype != DeclarationNode::NoComplexType )
|
---|
574 | throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: ", src );
|
---|
575 |
|
---|
576 | if ( dst->signedness == DeclarationNode::NoSignedness ) {
|
---|
577 | dst->signedness = src->signedness;
|
---|
578 | } else if ( src->signedness != DeclarationNode::NoSignedness )
|
---|
579 | throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: ", src );
|
---|
580 |
|
---|
581 | if ( dst->length == DeclarationNode::NoLength ) {
|
---|
582 | dst->length = src->length;
|
---|
583 | } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
|
---|
584 | dst->length = DeclarationNode::LongLong;
|
---|
585 | } else if ( src->length != DeclarationNode::NoLength )
|
---|
586 | throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: ", src );
|
---|
587 | } // if
|
---|
588 | break;
|
---|
589 | default:
|
---|
590 | switch ( src->kind ) {
|
---|
591 | case TypeData::Aggregate:
|
---|
592 | case TypeData::Enum:
|
---|
593 | dst->base = new TypeData( TypeData::AggregateInst );
|
---|
594 | dst->base->aggInst.aggregate = src;
|
---|
595 | if ( src->kind == TypeData::Aggregate ) {
|
---|
596 | dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
|
---|
597 | } // if
|
---|
598 | dst->base->qualifiers += src->qualifiers;
|
---|
599 | src = nullptr;
|
---|
600 | break;
|
---|
601 | default:
|
---|
602 | if ( dst->forall ) {
|
---|
603 | dst->forall->appendList( src->forall );
|
---|
604 | } else {
|
---|
605 | dst->forall = src->forall;
|
---|
606 | } // if
|
---|
607 | src->forall = nullptr;
|
---|
608 | dst->base = src;
|
---|
609 | src = nullptr;
|
---|
610 | } // switch
|
---|
611 | } // switch
|
---|
612 | } // if
|
---|
613 | }
|
---|
614 |
|
---|
615 | DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
|
---|
616 | if ( o ) {
|
---|
617 | checkSpecifiers( o );
|
---|
618 | copySpecifiers( o );
|
---|
619 | if ( o->type ) {
|
---|
620 | if ( ! type ) {
|
---|
621 | if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
|
---|
622 | type = new TypeData( TypeData::AggregateInst );
|
---|
623 | type->aggInst.aggregate = o->type;
|
---|
624 | if ( o->type->kind == TypeData::Aggregate ) {
|
---|
625 | type->aggInst.hoistType = o->type->aggregate.body;
|
---|
626 | type->aggInst.params = maybeClone( o->type->aggregate.actuals );
|
---|
627 | } else {
|
---|
628 | type->aggInst.hoistType = o->type->enumeration.body;
|
---|
629 | } // if
|
---|
630 | type->qualifiers += o->type->qualifiers;
|
---|
631 | } else {
|
---|
632 | type = o->type;
|
---|
633 | } // if
|
---|
634 | o->type = nullptr;
|
---|
635 | } else {
|
---|
636 | addTypeToType( o->type, type );
|
---|
637 | } // if
|
---|
638 | } // if
|
---|
639 | if ( o->bitfieldWidth ) {
|
---|
640 | bitfieldWidth = o->bitfieldWidth;
|
---|
641 | } // if
|
---|
642 |
|
---|
643 | // there may be typedefs chained onto the type
|
---|
644 | if ( o->get_next() ) {
|
---|
645 | set_last( o->get_next()->clone() );
|
---|
646 | } // if
|
---|
647 | } // if
|
---|
648 | delete o;
|
---|
649 | return this;
|
---|
650 | }
|
---|
651 |
|
---|
652 | DeclarationNode * DeclarationNode::addTypedef() {
|
---|
653 | TypeData * newtype = new TypeData( TypeData::Symbolic );
|
---|
654 | newtype->symbolic.params = nullptr;
|
---|
655 | newtype->symbolic.isTypedef = true;
|
---|
656 | newtype->symbolic.name = name ? new string( *name ) : nullptr;
|
---|
657 | newtype->base = type;
|
---|
658 | type = newtype;
|
---|
659 | return this;
|
---|
660 | }
|
---|
661 |
|
---|
662 | DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
|
---|
663 | if ( variable.tyClass != NoTypeClass ) {
|
---|
664 | if ( variable.assertions ) {
|
---|
665 | variable.assertions->appendList( assertions );
|
---|
666 | } else {
|
---|
667 | variable.assertions = assertions;
|
---|
668 | } // if
|
---|
669 | return this;
|
---|
670 | } // if
|
---|
671 |
|
---|
672 | assert( type );
|
---|
673 | switch ( type->kind ) {
|
---|
674 | case TypeData::Symbolic:
|
---|
675 | if ( type->symbolic.assertions ) {
|
---|
676 | type->symbolic.assertions->appendList( assertions );
|
---|
677 | } else {
|
---|
678 | type->symbolic.assertions = assertions;
|
---|
679 | } // if
|
---|
680 | break;
|
---|
681 | default:
|
---|
682 | assert( false );
|
---|
683 | } // switch
|
---|
684 |
|
---|
685 | return this;
|
---|
686 | }
|
---|
687 |
|
---|
688 | DeclarationNode * DeclarationNode::addName( string * newname ) {
|
---|
689 | assert( ! name );
|
---|
690 | name = newname;
|
---|
691 | return this;
|
---|
692 | }
|
---|
693 |
|
---|
694 | DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
|
---|
695 | assert( ! asmName );
|
---|
696 | asmName = newname ? newname->asmName : nullptr;
|
---|
697 | return this->addQualifiers( newname );
|
---|
698 | }
|
---|
699 |
|
---|
700 | DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
|
---|
701 | bitfieldWidth = size;
|
---|
702 | return this;
|
---|
703 | }
|
---|
704 |
|
---|
705 | DeclarationNode * DeclarationNode::addVarArgs() {
|
---|
706 | assert( type );
|
---|
707 | hasEllipsis = true;
|
---|
708 | return this;
|
---|
709 | }
|
---|
710 |
|
---|
711 | DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body ) {
|
---|
712 | assert( type );
|
---|
713 | assert( type->kind == TypeData::Function );
|
---|
714 | assert( ! type->function.body );
|
---|
715 | type->function.body = body;
|
---|
716 | return this;
|
---|
717 | }
|
---|
718 |
|
---|
719 | DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
|
---|
720 | assert( type );
|
---|
721 | assert( type->kind == TypeData::Function );
|
---|
722 | assert( ! type->function.oldDeclList );
|
---|
723 | type->function.oldDeclList = list;
|
---|
724 | return this;
|
---|
725 | }
|
---|
726 |
|
---|
727 | DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
|
---|
728 | if ( type ) {
|
---|
729 | TypeData * prevBase = type;
|
---|
730 | TypeData * curBase = type->base;
|
---|
731 | while ( curBase != nullptr ) {
|
---|
732 | prevBase = curBase;
|
---|
733 | curBase = curBase->base;
|
---|
734 | } // while
|
---|
735 | prevBase->base = newType;
|
---|
736 | } else {
|
---|
737 | type = newType;
|
---|
738 | } // if
|
---|
739 | return this;
|
---|
740 | }
|
---|
741 |
|
---|
742 | DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
|
---|
743 | if ( a ) {
|
---|
744 | for ( Attribute *attr: reverseIterate( a->attributes ) ) {
|
---|
745 | attributes.push_front( attr );
|
---|
746 | } // for
|
---|
747 | a->attributes.clear();
|
---|
748 | } // if
|
---|
749 | return this;
|
---|
750 | } // copyAttribute
|
---|
751 |
|
---|
752 | DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
|
---|
753 | if ( p ) {
|
---|
754 | assert( p->type->kind == TypeData::Pointer );
|
---|
755 | setBase( p->type );
|
---|
756 | p->type = nullptr;
|
---|
757 | copyAttribute( p );
|
---|
758 | delete p;
|
---|
759 | } // if
|
---|
760 | return this;
|
---|
761 | }
|
---|
762 |
|
---|
763 | DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
|
---|
764 | if ( a ) {
|
---|
765 | assert( a->type->kind == TypeData::Array );
|
---|
766 | setBase( a->type );
|
---|
767 | a->type = nullptr;
|
---|
768 | copyAttribute( a );
|
---|
769 | delete a;
|
---|
770 | } // if
|
---|
771 | return this;
|
---|
772 | }
|
---|
773 |
|
---|
774 | DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
|
---|
775 | if ( p ) {
|
---|
776 | assert( p->type->kind == TypeData::Pointer );
|
---|
777 | if ( type ) {
|
---|
778 | switch ( type->kind ) {
|
---|
779 | case TypeData::Aggregate:
|
---|
780 | case TypeData::Enum:
|
---|
781 | p->type->base = new TypeData( TypeData::AggregateInst );
|
---|
782 | p->type->base->aggInst.aggregate = type;
|
---|
783 | if ( type->kind == TypeData::Aggregate ) {
|
---|
784 | p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
|
---|
785 | } // if
|
---|
786 | p->type->base->qualifiers += type->qualifiers;
|
---|
787 | break;
|
---|
788 |
|
---|
789 | default:
|
---|
790 | p->type->base = type;
|
---|
791 | } // switch
|
---|
792 | type = nullptr;
|
---|
793 | } // if
|
---|
794 | delete this;
|
---|
795 | return p;
|
---|
796 | } else {
|
---|
797 | return this;
|
---|
798 | } // if
|
---|
799 | }
|
---|
800 |
|
---|
801 | static TypeData * findLast( TypeData * a ) {
|
---|
802 | assert( a );
|
---|
803 | TypeData * cur = a;
|
---|
804 | while ( cur->base ) {
|
---|
805 | cur = cur->base;
|
---|
806 | } // while
|
---|
807 | return cur;
|
---|
808 | }
|
---|
809 |
|
---|
810 | DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
|
---|
811 | if ( ! a ) return this;
|
---|
812 | assert( a->type->kind == TypeData::Array );
|
---|
813 | TypeData * lastArray = findLast( a->type );
|
---|
814 | if ( type ) {
|
---|
815 | switch ( type->kind ) {
|
---|
816 | case TypeData::Aggregate:
|
---|
817 | case TypeData::Enum:
|
---|
818 | lastArray->base = new TypeData( TypeData::AggregateInst );
|
---|
819 | lastArray->base->aggInst.aggregate = type;
|
---|
820 | if ( type->kind == TypeData::Aggregate ) {
|
---|
821 | lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
|
---|
822 | } // if
|
---|
823 | lastArray->base->qualifiers += type->qualifiers;
|
---|
824 | break;
|
---|
825 | default:
|
---|
826 | lastArray->base = type;
|
---|
827 | } // switch
|
---|
828 | type = nullptr;
|
---|
829 | } // if
|
---|
830 | delete this;
|
---|
831 | return a;
|
---|
832 | }
|
---|
833 |
|
---|
834 | DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
|
---|
835 | TypeData * ftype = new TypeData( TypeData::Function );
|
---|
836 | ftype->function.params = params;
|
---|
837 | setBase( ftype );
|
---|
838 | return this;
|
---|
839 | }
|
---|
840 |
|
---|
841 | static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
|
---|
842 | if ( type ) {
|
---|
843 | if ( type->kind != TypeData::Function ) {
|
---|
844 | type->base = addIdListToType( type->base, ids );
|
---|
845 | } else {
|
---|
846 | type->function.idList = ids;
|
---|
847 | } // if
|
---|
848 | return type;
|
---|
849 | } else {
|
---|
850 | TypeData * newtype = new TypeData( TypeData::Function );
|
---|
851 | newtype->function.idList = ids;
|
---|
852 | return newtype;
|
---|
853 | } // if
|
---|
854 | } // addIdListToType
|
---|
855 |
|
---|
856 | DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
|
---|
857 | type = addIdListToType( type, ids );
|
---|
858 | return this;
|
---|
859 | }
|
---|
860 |
|
---|
861 | DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
|
---|
862 | initializer = init;
|
---|
863 | return this;
|
---|
864 | }
|
---|
865 |
|
---|
866 | DeclarationNode * DeclarationNode::cloneType( string * newName ) {
|
---|
867 | DeclarationNode * newnode = new DeclarationNode;
|
---|
868 | newnode->type = maybeClone( type );
|
---|
869 | newnode->copySpecifiers( this );
|
---|
870 | assert( newName );
|
---|
871 | newnode->name = newName;
|
---|
872 | return newnode;
|
---|
873 | }
|
---|
874 |
|
---|
875 | DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
|
---|
876 | if ( ! o ) return nullptr;
|
---|
877 |
|
---|
878 | o->copySpecifiers( this );
|
---|
879 | if ( type ) {
|
---|
880 | TypeData * srcType = type;
|
---|
881 |
|
---|
882 | // search for the base type by scanning off pointers and array designators
|
---|
883 | while ( srcType->base ) {
|
---|
884 | srcType = srcType->base;
|
---|
885 | } // while
|
---|
886 |
|
---|
887 | TypeData * newType = srcType->clone();
|
---|
888 | if ( newType->kind == TypeData::AggregateInst ) {
|
---|
889 | // don't duplicate members
|
---|
890 | if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
|
---|
891 | delete newType->aggInst.aggregate->enumeration.constants;
|
---|
892 | newType->aggInst.aggregate->enumeration.constants = nullptr;
|
---|
893 | } else {
|
---|
894 | assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
|
---|
895 | delete newType->aggInst.aggregate->aggregate.fields;
|
---|
896 | newType->aggInst.aggregate->aggregate.fields = nullptr;
|
---|
897 | } // if
|
---|
898 | // don't hoist twice
|
---|
899 | newType->aggInst.hoistType = false;
|
---|
900 | } // if
|
---|
901 |
|
---|
902 | newType->forall = maybeClone( type->forall );
|
---|
903 | if ( ! o->type ) {
|
---|
904 | o->type = newType;
|
---|
905 | } else {
|
---|
906 | addTypeToType( newType, o->type );
|
---|
907 | delete newType;
|
---|
908 | } // if
|
---|
909 | } // if
|
---|
910 | return o;
|
---|
911 | }
|
---|
912 |
|
---|
913 | DeclarationNode * DeclarationNode::extractAggregate() const {
|
---|
914 | if ( type ) {
|
---|
915 | TypeData * ret = typeextractAggregate( type );
|
---|
916 | if ( ret ) {
|
---|
917 | DeclarationNode * newnode = new DeclarationNode;
|
---|
918 | newnode->type = ret;
|
---|
919 | return newnode;
|
---|
920 | } // if
|
---|
921 | } // if
|
---|
922 | return nullptr;
|
---|
923 | }
|
---|
924 |
|
---|
925 | void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
|
---|
926 | SemanticError errors;
|
---|
927 | std::back_insert_iterator< std::list< Declaration * > > out( outputList );
|
---|
928 |
|
---|
929 | for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
|
---|
930 | try {
|
---|
931 | if ( DeclarationNode * extr = cur->extractAggregate() ) {
|
---|
932 | // handle the case where a structure declaration is contained within an object or type declaration
|
---|
933 | Declaration * decl = extr->build();
|
---|
934 | if ( decl ) {
|
---|
935 | decl->location = cur->location;
|
---|
936 | * out++ = decl;
|
---|
937 | } // if
|
---|
938 | delete extr;
|
---|
939 | } // if
|
---|
940 |
|
---|
941 | Declaration * decl = cur->build();
|
---|
942 | if ( decl ) {
|
---|
943 | decl->location = cur->location;
|
---|
944 | * out++ = decl;
|
---|
945 | } // if
|
---|
946 | } catch( SemanticError &e ) {
|
---|
947 | e.set_location( cur->location );
|
---|
948 | errors.append( e );
|
---|
949 | } // try
|
---|
950 | } // while
|
---|
951 |
|
---|
952 | if ( ! errors.isEmpty() ) {
|
---|
953 | throw errors;
|
---|
954 | } // if
|
---|
955 | } // buildList
|
---|
956 |
|
---|
957 | void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
|
---|
958 | SemanticError errors;
|
---|
959 | std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
|
---|
960 |
|
---|
961 | for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
|
---|
962 | try {
|
---|
963 | Declaration * decl = cur->build();
|
---|
964 | if ( decl ) {
|
---|
965 | if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
|
---|
966 | dwt->location = cur->location;
|
---|
967 | * out++ = dwt;
|
---|
968 | } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
|
---|
969 | StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
|
---|
970 | auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
|
---|
971 | obj->location = cur->location;
|
---|
972 | * out++ = obj;
|
---|
973 | delete agg;
|
---|
974 | } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
|
---|
975 | UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
|
---|
976 | auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
|
---|
977 | obj->location = cur->location;
|
---|
978 | * out++ = obj;
|
---|
979 | } // if
|
---|
980 | } // if
|
---|
981 | } catch( SemanticError &e ) {
|
---|
982 | e.set_location( cur->location );
|
---|
983 | errors.append( e );
|
---|
984 | } // try
|
---|
985 | } // for
|
---|
986 |
|
---|
987 | if ( ! errors.isEmpty() ) {
|
---|
988 | throw errors;
|
---|
989 | } // if
|
---|
990 | } // buildList
|
---|
991 |
|
---|
992 | void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
|
---|
993 | SemanticError errors;
|
---|
994 | std::back_insert_iterator< std::list< Type * > > out( outputList );
|
---|
995 | const DeclarationNode * cur = firstNode;
|
---|
996 |
|
---|
997 | while ( cur ) {
|
---|
998 | try {
|
---|
999 | * out++ = cur->buildType();
|
---|
1000 | } catch( SemanticError &e ) {
|
---|
1001 | e.set_location( cur->location );
|
---|
1002 | errors.append( e );
|
---|
1003 | } // try
|
---|
1004 | cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
|
---|
1005 | } // while
|
---|
1006 |
|
---|
1007 | if ( ! errors.isEmpty() ) {
|
---|
1008 | throw errors;
|
---|
1009 | } // if
|
---|
1010 | } // buildTypeList
|
---|
1011 |
|
---|
1012 | Declaration * DeclarationNode::build() const {
|
---|
1013 | if ( ! error.empty() ) throw SemanticError( error + " in declaration of ", this );
|
---|
1014 |
|
---|
1015 | if ( asmStmt ) {
|
---|
1016 | return new AsmDecl( safe_dynamic_cast<AsmStmt *>( asmStmt->build() ) );
|
---|
1017 | } // if
|
---|
1018 |
|
---|
1019 | if ( variable.tyClass != NoTypeClass ) {
|
---|
1020 | static const TypeDecl::Kind kindMap[] = { TypeDecl::Any, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype };
|
---|
1021 | assertf( sizeof(kindMap)/sizeof(kindMap[0] == NoTypeClass-1), "DeclarationNode::build: kindMap is out of sync." );
|
---|
1022 | assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
|
---|
1023 | TypeDecl * ret = new TypeDecl( *name, Type::StorageClasses(), nullptr, kindMap[ variable.tyClass ] );
|
---|
1024 | buildList( variable.assertions, ret->get_assertions() );
|
---|
1025 | return ret;
|
---|
1026 | } // if
|
---|
1027 |
|
---|
1028 | if ( type ) {
|
---|
1029 | // Function specifiers can only appear on a function definition/declaration.
|
---|
1030 | //
|
---|
1031 | // inline _Noreturn int f(); // allowed
|
---|
1032 | // inline _Noreturn int g( int i ); // allowed
|
---|
1033 | // inline _Noreturn int i; // disallowed
|
---|
1034 | if ( type->kind != TypeData::Function && funcSpecs.any() ) {
|
---|
1035 | throw SemanticError( "invalid function specifier for ", this );
|
---|
1036 | } // if
|
---|
1037 | return buildDecl( type, name ? *name : string( "" ), storageClasses, maybeBuild< Expression >( bitfieldWidth ), funcSpecs, linkage, asmName, maybeBuild< Initializer >(initializer), attributes )->set_extension( extension );
|
---|
1038 | } // if
|
---|
1039 |
|
---|
1040 | // SUE's cannot have function specifiers, either
|
---|
1041 | //
|
---|
1042 | // inlne _Noreturn struct S { ... }; // disallowed
|
---|
1043 | // inlne _Noreturn enum E { ... }; // disallowed
|
---|
1044 | if ( funcSpecs.any() ) {
|
---|
1045 | throw SemanticError( "invalid function specifier for ", this );
|
---|
1046 | } // if
|
---|
1047 | assertf( name, "ObjectDecl must a have name\n" );
|
---|
1048 | return (new ObjectDecl( *name, storageClasses, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension );
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | Type * DeclarationNode::buildType() const {
|
---|
1052 | assert( type );
|
---|
1053 |
|
---|
1054 | if ( attr.expr ) {
|
---|
1055 | return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes );
|
---|
1056 | } else if ( attr.type ) {
|
---|
1057 | return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes );
|
---|
1058 | } // if
|
---|
1059 |
|
---|
1060 | switch ( type->kind ) {
|
---|
1061 | case TypeData::Enum:
|
---|
1062 | case TypeData::Aggregate: {
|
---|
1063 | ReferenceToType * ret = buildComAggInst( type, attributes );
|
---|
1064 | buildList( type->aggregate.actuals, ret->get_parameters() );
|
---|
1065 | return ret;
|
---|
1066 | }
|
---|
1067 | case TypeData::Symbolic: {
|
---|
1068 | TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false, attributes );
|
---|
1069 | buildList( type->symbolic.actuals, ret->get_parameters() );
|
---|
1070 | return ret;
|
---|
1071 | }
|
---|
1072 | default:
|
---|
1073 | Type * simpletypes = typebuild( type );
|
---|
1074 | simpletypes->get_attributes() = attributes; // copy because member is const
|
---|
1075 | return simpletypes;
|
---|
1076 | } // switch
|
---|
1077 | }
|
---|
1078 |
|
---|
1079 | // Local Variables: //
|
---|
1080 | // tab-width: 4 //
|
---|
1081 | // mode: c++ //
|
---|
1082 | // compile-command: "make install" //
|
---|
1083 | // End: //
|
---|