source: src/Parser/DeclarationNode.cc@ 4d6d62e

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 4d6d62e was 099e202, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Merge branch 'master' of plg.uwaterloo.ca:/u/cforall/software/cfa/cfa-cc

  • Property mode set to 100644
File size: 37.0 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
[af9da5f]12// Last Modified On : Wed May 16 09:37:17 2018
13// Update Count : 1070
[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 ) {
[af9da5f]531 if ( ! 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 );
[af9da5f]565 if ( (builtin == Zero || builtin == One) && q->type->qualifiers.val != 0 && error.length() == 0 ) {
[f14d956]566 SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, Type::QualifiersNames[ilog2( q->type->qualifiers.val )], builtinTypeNames[builtin] );
[9dc31c10]567 } // if
[6ef2d81]568 addQualifiersToType( q->type, type );
569
[b87a5ed]570 delete q;
571 return this;
[101e0bd]572} // addQualifiers
[3848e0e]573
574static void addTypeToType( TypeData *&src, TypeData *&dst ) {
[101e0bd]575 if ( src->forall && dst->kind == TypeData::Function ) {
576 if ( dst->forall ) {
577 dst->forall->appendList( src->forall );
[b87a5ed]578 } else {
[101e0bd]579 dst->forall = src->forall;
580 } // if
[2298f728]581 src->forall = nullptr;
[101e0bd]582 } // if
583 if ( dst->base ) {
584 addTypeToType( src, dst->base );
585 } else {
586 switch ( dst->kind ) {
587 case TypeData::Unknown:
[6f95000]588 src->qualifiers |= dst->qualifiers;
[101e0bd]589 dst = src;
[2298f728]590 src = nullptr;
[101e0bd]591 break;
592 case TypeData::Basic:
[6f95000]593 dst->qualifiers |= src->qualifiers;
[101e0bd]594 if ( src->kind != TypeData::Unknown ) {
595 assert( src->kind == TypeData::Basic );
596
597 if ( dst->basictype == DeclarationNode::NoBasicType ) {
598 dst->basictype = src->basictype;
599 } else if ( src->basictype != DeclarationNode::NoBasicType )
[a16764a6]600 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: " );
[101e0bd]601
602 if ( dst->complextype == DeclarationNode::NoComplexType ) {
603 dst->complextype = src->complextype;
604 } else if ( src->complextype != DeclarationNode::NoComplexType )
[a16764a6]605 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: " );
[101e0bd]606
607 if ( dst->signedness == DeclarationNode::NoSignedness ) {
608 dst->signedness = src->signedness;
609 } else if ( src->signedness != DeclarationNode::NoSignedness )
[a16764a6]610 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: " );
[101e0bd]611
612 if ( dst->length == DeclarationNode::NoLength ) {
613 dst->length = src->length;
614 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
615 dst->length = DeclarationNode::LongLong;
616 } else if ( src->length != DeclarationNode::NoLength )
[a16764a6]617 SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: " );
[101e0bd]618 } // if
619 break;
620 default:
621 switch ( src->kind ) {
622 case TypeData::Aggregate:
623 case TypeData::Enum:
624 dst->base = new TypeData( TypeData::AggregateInst );
625 dst->base->aggInst.aggregate = src;
626 if ( src->kind == TypeData::Aggregate ) {
627 dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
[68cd1ce]628 } // if
[6f95000]629 dst->base->qualifiers |= src->qualifiers;
[2298f728]630 src = nullptr;
[b87a5ed]631 break;
632 default:
[101e0bd]633 if ( dst->forall ) {
634 dst->forall->appendList( src->forall );
635 } else {
636 dst->forall = src->forall;
637 } // if
[2298f728]638 src->forall = nullptr;
[101e0bd]639 dst->base = src;
[2298f728]640 src = nullptr;
[68cd1ce]641 } // switch
[101e0bd]642 } // switch
[68cd1ce]643 } // if
[3848e0e]644}
645
[ba7aa2d]646DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
[b87a5ed]647 if ( o ) {
[a7c90d4]648 checkSpecifiers( o );
649 copySpecifiers( o );
[b87a5ed]650 if ( o->type ) {
651 if ( ! type ) {
652 if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
653 type = new TypeData( TypeData::AggregateInst );
[8f6f47d7]654 type->aggInst.aggregate = o->type;
[b87a5ed]655 if ( o->type->kind == TypeData::Aggregate ) {
[43c89a7]656 type->aggInst.hoistType = o->type->aggregate.body;
[8f6f47d7]657 type->aggInst.params = maybeClone( o->type->aggregate.actuals );
[43c89a7]658 } else {
659 type->aggInst.hoistType = o->type->enumeration.body;
[68cd1ce]660 } // if
[6f95000]661 type->qualifiers |= o->type->qualifiers;
[b87a5ed]662 } else {
663 type = o->type;
[68cd1ce]664 } // if
[2298f728]665 o->type = nullptr;
[b87a5ed]666 } else {
667 addTypeToType( o->type, type );
[68cd1ce]668 } // if
669 } // if
[b87a5ed]670 if ( o->bitfieldWidth ) {
671 bitfieldWidth = o->bitfieldWidth;
[68cd1ce]672 } // if
[71bd8c6]673
674 // there may be typedefs chained onto the type
[1d4580a]675 if ( o->get_next() ) {
676 set_last( o->get_next()->clone() );
[1db21619]677 } // if
[68cd1ce]678 } // if
[b87a5ed]679 delete o;
680 return this;
[3848e0e]681}
682
[ba7aa2d]683DeclarationNode * DeclarationNode::addTypedef() {
684 TypeData * newtype = new TypeData( TypeData::Symbolic );
[2298f728]685 newtype->symbolic.params = nullptr;
[8f6f47d7]686 newtype->symbolic.isTypedef = true;
[2298f728]687 newtype->symbolic.name = name ? new string( *name ) : nullptr;
[b87a5ed]688 newtype->base = type;
689 type = newtype;
690 return this;
[3848e0e]691}
692
[ba7aa2d]693DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
[faddbd8]694 if ( variable.tyClass != NoTypeClass ) {
[2298f728]695 if ( variable.assertions ) {
696 variable.assertions->appendList( assertions );
697 } else {
698 variable.assertions = assertions;
699 } // if
700 return this;
701 } // if
702
[b87a5ed]703 assert( type );
704 switch ( type->kind ) {
705 case TypeData::Symbolic:
[8f6f47d7]706 if ( type->symbolic.assertions ) {
707 type->symbolic.assertions->appendList( assertions );
[b87a5ed]708 } else {
[8f6f47d7]709 type->symbolic.assertions = assertions;
[68cd1ce]710 } // if
[b87a5ed]711 break;
712 default:
713 assert( false );
[68cd1ce]714 } // switch
[974906e2]715
[b87a5ed]716 return this;
[51b73452]717}
718
[fb114fa1]719DeclarationNode * DeclarationNode::addName( string * newname ) {
[2298f728]720 assert( ! name );
721 name = newname;
[b87a5ed]722 return this;
[51b73452]723}
724
[c0aa336]725DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
[58dd019]726 assert( ! asmName );
[c0aa336]727 asmName = newname ? newname->asmName : nullptr;
728 return this->addQualifiers( newname );
[58dd019]729}
730
[ba7aa2d]731DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
[b87a5ed]732 bitfieldWidth = size;
733 return this;
[51b73452]734}
735
[ba7aa2d]736DeclarationNode * DeclarationNode::addVarArgs() {
[b87a5ed]737 assert( type );
738 hasEllipsis = true;
739 return this;
[51b73452]740}
741
[c453ac4]742DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) {
[b87a5ed]743 assert( type );
744 assert( type->kind == TypeData::Function );
[2298f728]745 assert( ! type->function.body );
[8f6f47d7]746 type->function.body = body;
[c453ac4]747 type->function.withExprs = withExprs;
[b87a5ed]748 return this;
[51b73452]749}
750
[ba7aa2d]751DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
[b87a5ed]752 assert( type );
753 assert( type->kind == TypeData::Function );
[2298f728]754 assert( ! type->function.oldDeclList );
[8f6f47d7]755 type->function.oldDeclList = list;
[b87a5ed]756 return this;
[51b73452]757}
758
[c0aa336]759DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
[b87a5ed]760 if ( type ) {
[ba7aa2d]761 TypeData * prevBase = type;
762 TypeData * curBase = type->base;
[2298f728]763 while ( curBase != nullptr ) {
[b87a5ed]764 prevBase = curBase;
765 curBase = curBase->base;
[68cd1ce]766 } // while
[b87a5ed]767 prevBase->base = newType;
768 } else {
769 type = newType;
[68cd1ce]770 } // if
[c0aa336]771 return this;
[3848e0e]772}
773
[c0aa336]774DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
775 if ( a ) {
776 for ( Attribute *attr: reverseIterate( a->attributes ) ) {
777 attributes.push_front( attr );
778 } // for
779 a->attributes.clear();
780 } // if
781 return this;
782} // copyAttribute
783
[ba7aa2d]784DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
[b87a5ed]785 if ( p ) {
[6926a6d]786 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
[c0aa336]787 setBase( p->type );
[2298f728]788 p->type = nullptr;
[c0aa336]789 copyAttribute( p );
[b87a5ed]790 delete p;
[68cd1ce]791 } // if
[b87a5ed]792 return this;
[3848e0e]793}
794
[ba7aa2d]795DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
[b87a5ed]796 if ( a ) {
797 assert( a->type->kind == TypeData::Array );
[c0aa336]798 setBase( a->type );
[2298f728]799 a->type = nullptr;
[c0aa336]800 copyAttribute( a );
[b87a5ed]801 delete a;
[68cd1ce]802 } // if
[b87a5ed]803 return this;
[51b73452]804}
805
[ba7aa2d]806DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
[b87a5ed]807 if ( p ) {
[e6cee92]808 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
[b87a5ed]809 if ( type ) {
810 switch ( type->kind ) {
811 case TypeData::Aggregate:
812 case TypeData::Enum:
813 p->type->base = new TypeData( TypeData::AggregateInst );
[8f6f47d7]814 p->type->base->aggInst.aggregate = type;
[b87a5ed]815 if ( type->kind == TypeData::Aggregate ) {
[8f6f47d7]816 p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
[68cd1ce]817 } // if
[6f95000]818 p->type->base->qualifiers |= type->qualifiers;
[b87a5ed]819 break;
820
821 default:
822 p->type->base = type;
[68cd1ce]823 } // switch
[2298f728]824 type = nullptr;
[68cd1ce]825 } // if
[b87a5ed]826 delete this;
827 return p;
828 } else {
829 return this;
[68cd1ce]830 } // if
[51b73452]831}
832
[ba7aa2d]833static TypeData * findLast( TypeData * a ) {
[b87a5ed]834 assert( a );
[ba7aa2d]835 TypeData * cur = a;
[a32b204]836 while ( cur->base ) {
[b87a5ed]837 cur = cur->base;
[68cd1ce]838 } // while
[b87a5ed]839 return cur;
[3848e0e]840}
841
[ba7aa2d]842DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
[738e304]843 if ( ! a ) return this;
844 assert( a->type->kind == TypeData::Array );
845 TypeData * lastArray = findLast( a->type );
846 if ( type ) {
847 switch ( type->kind ) {
848 case TypeData::Aggregate:
849 case TypeData::Enum:
850 lastArray->base = new TypeData( TypeData::AggregateInst );
851 lastArray->base->aggInst.aggregate = type;
852 if ( type->kind == TypeData::Aggregate ) {
853 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
854 } // if
[6f95000]855 lastArray->base->qualifiers |= type->qualifiers;
[738e304]856 break;
857 default:
858 lastArray->base = type;
859 } // switch
860 type = nullptr;
[68cd1ce]861 } // if
[738e304]862 delete this;
863 return a;
[51b73452]864}
[3848e0e]865
[ba7aa2d]866DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
867 TypeData * ftype = new TypeData( TypeData::Function );
[8f6f47d7]868 ftype->function.params = params;
[c0aa336]869 setBase( ftype );
[b87a5ed]870 return this;
[3848e0e]871}
872
[ba7aa2d]873static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
[b87a5ed]874 if ( type ) {
875 if ( type->kind != TypeData::Function ) {
876 type->base = addIdListToType( type->base, ids );
877 } else {
[8f6f47d7]878 type->function.idList = ids;
[68cd1ce]879 } // if
[b87a5ed]880 return type;
[3848e0e]881 } else {
[ba7aa2d]882 TypeData * newtype = new TypeData( TypeData::Function );
[8f6f47d7]883 newtype->function.idList = ids;
[b87a5ed]884 return newtype;
[68cd1ce]885 } // if
[2298f728]886} // addIdListToType
[974906e2]887
[ba7aa2d]888DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
[b87a5ed]889 type = addIdListToType( type, ids );
890 return this;
[3848e0e]891}
892
[ba7aa2d]893DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
[b87a5ed]894 initializer = init;
895 return this;
[3848e0e]896}
897
[67cf18c]898DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) {
899 assertf( variable.tyClass != NoTypeClass, "Called addTypeInitializer on something that isn't a type variable." );
900 variable.initializer = init;
901 return this;
902}
903
[ba7aa2d]904DeclarationNode * DeclarationNode::cloneType( string * newName ) {
905 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]906 newnode->type = maybeClone( type );
[a7c90d4]907 newnode->copySpecifiers( this );
[ba7aa2d]908 assert( newName );
[2298f728]909 newnode->name = newName;
[b87a5ed]910 return newnode;
[3848e0e]911}
912
[2298f728]913DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
914 if ( ! o ) return nullptr;
915
[a7c90d4]916 o->copySpecifiers( this );
[2298f728]917 if ( type ) {
918 TypeData * srcType = type;
919
[c0aa336]920 // search for the base type by scanning off pointers and array designators
[2298f728]921 while ( srcType->base ) {
922 srcType = srcType->base;
923 } // while
924
925 TypeData * newType = srcType->clone();
926 if ( newType->kind == TypeData::AggregateInst ) {
927 // don't duplicate members
928 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
929 delete newType->aggInst.aggregate->enumeration.constants;
930 newType->aggInst.aggregate->enumeration.constants = nullptr;
[b2da0574]931 newType->aggInst.aggregate->enumeration.body = false;
[b87a5ed]932 } else {
[2298f728]933 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
934 delete newType->aggInst.aggregate->aggregate.fields;
935 newType->aggInst.aggregate->aggregate.fields = nullptr;
[b2da0574]936 newType->aggInst.aggregate->aggregate.body = false;
[68cd1ce]937 } // if
[43c89a7]938 // don't hoist twice
939 newType->aggInst.hoistType = false;
[68cd1ce]940 } // if
[2298f728]941
942 newType->forall = maybeClone( type->forall );
943 if ( ! o->type ) {
944 o->type = newType;
945 } else {
946 addTypeToType( newType, o->type );
947 delete newType;
948 } // if
[68cd1ce]949 } // if
[b87a5ed]950 return o;
[51b73452]951}
952
[ba7aa2d]953DeclarationNode * DeclarationNode::extractAggregate() const {
[b87a5ed]954 if ( type ) {
[ba7aa2d]955 TypeData * ret = typeextractAggregate( type );
[b87a5ed]956 if ( ret ) {
[ba7aa2d]957 DeclarationNode * newnode = new DeclarationNode;
[b87a5ed]958 newnode->type = ret;
959 return newnode;
[843054c2]960 } // if
961 } // if
[2298f728]962 return nullptr;
[3848e0e]963}
964
[ba7aa2d]965void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
[a16764a6]966 SemanticErrorException errors;
[7880579]967 std::back_insert_iterator< std::list< Declaration * > > out( outputList );
[2298f728]968
[3a5131ed]969 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
[b87a5ed]970 try {
[ba7aa2d]971 if ( DeclarationNode * extr = cur->extractAggregate() ) {
[b87a5ed]972 // handle the case where a structure declaration is contained within an object or type declaration
[ba7aa2d]973 Declaration * decl = extr->build();
[b87a5ed]974 if ( decl ) {
[294647b]975 decl->location = cur->location;
[ba7aa2d]976 * out++ = decl;
[843054c2]977 } // if
[f39096c]978 delete extr;
[843054c2]979 } // if
[2298f728]980
[ba7aa2d]981 Declaration * decl = cur->build();
[b87a5ed]982 if ( decl ) {
[294647b]983 decl->location = cur->location;
[ba7aa2d]984 * out++ = decl;
[843054c2]985 } // if
[a16764a6]986 } catch( SemanticErrorException &e ) {
[b87a5ed]987 errors.append( e );
[843054c2]988 } // try
989 } // while
[2298f728]990
[b87a5ed]991 if ( ! errors.isEmpty() ) {
992 throw errors;
[843054c2]993 } // if
[2298f728]994} // buildList
[3848e0e]995
[ba7aa2d]996void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
[a16764a6]997 SemanticErrorException errors;
[7880579]998 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
[43c89a7]999
[3a5131ed]1000 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
[b87a5ed]1001 try {
[ba7aa2d]1002 Declaration * decl = cur->build();
[b87a5ed]1003 if ( decl ) {
[ba7aa2d]1004 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
[294647b]1005 dwt->location = cur->location;
[ba7aa2d]1006 * out++ = dwt;
1007 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
1008 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
[68fe077a]1009 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
[294647b]1010 obj->location = cur->location;
[43c89a7]1011 * out++ = obj;
[b87a5ed]1012 delete agg;
[ba7aa2d]1013 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
1014 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
[68fe077a]1015 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
[294647b]1016 obj->location = cur->location;
1017 * out++ = obj;
[843054c2]1018 } // if
1019 } // if
[a16764a6]1020 } catch( SemanticErrorException &e ) {
[b87a5ed]1021 errors.append( e );
[843054c2]1022 } // try
[3a5131ed]1023 } // for
1024
[b87a5ed]1025 if ( ! errors.isEmpty() ) {
1026 throw errors;
[843054c2]1027 } // if
[2298f728]1028} // buildList
[3848e0e]1029
[ba7aa2d]1030void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
[a16764a6]1031 SemanticErrorException errors;
[7880579]1032 std::back_insert_iterator< std::list< Type * > > out( outputList );
[ba7aa2d]1033 const DeclarationNode * cur = firstNode;
[2298f728]1034
[a32b204]1035 while ( cur ) {
[b87a5ed]1036 try {
[ba7aa2d]1037 * out++ = cur->buildType();
[a16764a6]1038 } catch( SemanticErrorException &e ) {
[b87a5ed]1039 errors.append( e );
[843054c2]1040 } // try
[7880579]1041 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
[843054c2]1042 } // while
[2298f728]1043
[b87a5ed]1044 if ( ! errors.isEmpty() ) {
1045 throw errors;
[843054c2]1046 } // if
[2298f728]1047} // buildTypeList
[51b73452]1048
[ba7aa2d]1049Declaration * DeclarationNode::build() const {
[a16764a6]1050 if ( ! error.empty() ) SemanticError( this, error + " in declaration of " );
[2298f728]1051
[e994912]1052 if ( asmStmt ) {
[e3e16bc]1053 return new AsmDecl( strict_dynamic_cast<AsmStmt *>( asmStmt->build() ) );
[e994912]1054 } // if
1055
[faddbd8]1056 if ( variable.tyClass != NoTypeClass ) {
[f0ecf9b]1057 // otype is internally converted to dtype + otype parameters
1058 static const TypeDecl::Kind kindMap[] = { TypeDecl::Dtype, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype };
1059 assertf( sizeof(kindMap)/sizeof(kindMap[0]) == NoTypeClass, "DeclarationNode::build: kindMap is out of sync." );
[8f60f0b]1060 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
[f0ecf9b]1061 TypeDecl * ret = new TypeDecl( *name, Type::StorageClasses(), nullptr, kindMap[ variable.tyClass ], variable.tyClass == Otype, variable.initializer ? variable.initializer->buildType() : nullptr );
[2298f728]1062 buildList( variable.assertions, ret->get_assertions() );
1063 return ret;
1064 } // if
1065
[843054c2]1066 if ( type ) {
[dd020c0]1067 // Function specifiers can only appear on a function definition/declaration.
1068 //
1069 // inline _Noreturn int f(); // allowed
1070 // inline _Noreturn int g( int i ); // allowed
1071 // inline _Noreturn int i; // disallowed
[fb04321]1072 if ( type->kind != TypeData::Function && funcSpecs.any() ) {
[a16764a6]1073 SemanticError( this, "invalid function specifier for " );
[dd020c0]1074 } // if
[a7c90d4]1075 return buildDecl( type, name ? *name : string( "" ), storageClasses, maybeBuild< Expression >( bitfieldWidth ), funcSpecs, linkage, asmName, maybeBuild< Initializer >(initializer), attributes )->set_extension( extension );
[843054c2]1076 } // if
[2298f728]1077
[f6e3e34]1078 if ( assert.condition ) {
1079 return new StaticAssertDecl( maybeBuild< Expression >( assert.condition ), strict_dynamic_cast< ConstantExpr * >( maybeClone( assert.message ) ) );
1080 }
1081
[dd020c0]1082 // SUE's cannot have function specifiers, either
1083 //
1084 // inlne _Noreturn struct S { ... }; // disallowed
1085 // inlne _Noreturn enum E { ... }; // disallowed
[fb04321]1086 if ( funcSpecs.any() ) {
[a16764a6]1087 SemanticError( this, "invalid function specifier for " );
[843054c2]1088 } // if
[dd020c0]1089 assertf( name, "ObjectDecl must a have name\n" );
[a7c90d4]1090 return (new ObjectDecl( *name, storageClasses, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension );
[51b73452]1091}
1092
[ba7aa2d]1093Type * DeclarationNode::buildType() const {
[b87a5ed]1094 assert( type );
[974906e2]1095
[faddbd8]1096 if ( attr.expr ) {
[c0aa336]1097 return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes );
[faddbd8]1098 } else if ( attr.type ) {
[c0aa336]1099 return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes );
[2298f728]1100 } // if
1101
[b87a5ed]1102 switch ( type->kind ) {
[43c89a7]1103 case TypeData::Enum:
[b87a5ed]1104 case TypeData::Aggregate: {
[fa4805f]1105 ReferenceToType * ret = buildComAggInst( type, attributes, linkage );
[8f6f47d7]1106 buildList( type->aggregate.actuals, ret->get_parameters() );
[b87a5ed]1107 return ret;
1108 }
1109 case TypeData::Symbolic: {
[c0aa336]1110 TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false, attributes );
[8f6f47d7]1111 buildList( type->symbolic.actuals, ret->get_parameters() );
[b87a5ed]1112 return ret;
1113 }
1114 default:
[c0aa336]1115 Type * simpletypes = typebuild( type );
1116 simpletypes->get_attributes() = attributes; // copy because member is const
1117 return simpletypes;
[b87a5ed]1118 } // switch
[3848e0e]1119}
1120
[b87a5ed]1121// Local Variables: //
1122// tab-width: 4 //
1123// mode: c++ //
1124// compile-command: "make install" //
1125// End: //
Note: See TracBrowser for help on using the repository browser.