source: src/Parser/DeclarationNode.cc@ 8a1289f

new-env with_gc
Last change on this file since 8a1289f was eba74ba, checked in by Aaron Moss <a3moss@…>, 7 years ago

Merge remote-tracking branch 'origin/master' into with_gc

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