source: src/Parser/DeclarationNode.cc@ f38e7d7

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since f38e7d7 was f6e3e34, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Add StaticAssertDecl node

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