source: src/Parser/DeclarationNode.cc@ af75a87

ADT ast-experimental pthread-emulation qualifiedEnum
Last change on this file since af75a87 was 9e7236f4, checked in by JiadaL <j82liang@…>, 3 years ago

Resolution of struct enum. The codegen of struct enum will be in the next commit

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