source: src/Parser/DeclarationNode.cc@ e9c5db2

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

Replace the interface for EnumDecl node construction to support generic enum types

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