source: src/Parser/DeclarationNode.cc@ 3b0bc16

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since 3b0bc16 was 93bbbc4, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

update parser for vtable declarations

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