source: src/Parser/DeclarationNode.cc@ b787cad

ADT ast-experimental enum pthread-emulation qualifiedEnum
Last change on this file since b787cad was f135b50, checked in by JiadaL <j82liang@…>, 4 years ago

The compiler is now trying to pass the value of enum const to code gen; but it won't work cause we must be able to evaluate the const_expr in compiler time. It is not currently passed as a Expression but won't be able to evaluate at compile time

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