source: src/Parser/DeclarationNode.cc@ 7fdb94e1

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 7fdb94e1 was 7fdb94e1, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

rewrite TypedefTable

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