source: src/Parser/DeclarationNode.cc@ f6e8c67

stuck-waitfor-destruct
Last change on this file since f6e8c67 was af60383, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Moved a field and functions from DeclarationNode to TypeData. Trying to make the line between them cleaner.

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