source: src/Parser/DeclarationNode.cc@ bdc8591

Last change on this file since bdc8591 was 057608a, checked in by Andrew Beach <ajbeach@…>, 18 months ago

Parser clean-up: Removed an unused field, added a comment, fixed a memory leak and reformated a function.

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