source: src/Parser/DeclarationNode.cc@ 0bcc2b7

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 no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since 0bcc2b7 was 638ac26, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

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

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