source: src/Parser/DeclarationNode.cc@ 332bd33

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 332bd33 was 312029a, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

move enum Aggregate from DeclarationNode to AggregateDecl, add control-keyword field-dereference to replace control-keyword cast

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