source: src/Parser/DeclarationNode.cc@ 2097cd4

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 resolv-new with_gc
Last change on this file since 2097cd4 was a16764a6, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Changed warning system to prepare for toggling warnings

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