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