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