source: src/Parser/DeclarationNode.cc@ c5d7701

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 c5d7701 was c5d7701, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Add QualifiedType node

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