source: src/Parser/DeclarationNode.cc@ bd6e226

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since bd6e226 was 2e5fa345, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Fix uninitialized DeclarationNode member

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