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

Last change on this file since 3f9a8d0 was 4eb3a7c5, checked in by Peter A. Buhr <pabuhr@…>, 19 months ago

first attempt at correct distribution of attributes for aggregates

  • Property mode set to 100644
File size: 44.8 KB
Line 
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//
7// DeclarationNode.cc --
8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 12:34:05 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Feb 23 18:25:57 2024
13// Update Count : 1533
14//
15
16#include "DeclarationNode.h"
17
18#include <cassert> // for assert, assertf, strict_dynamic_cast
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...
24
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
35#include "Common/SemanticError.h" // for SemanticError
36#include "Common/UniqueName.h" // for UniqueName
37#include "Common/utility.h" // for copy, spliceBegin
38#include "Parser/ExpressionNode.h" // for ExpressionNode
39#include "Parser/InitializerNode.h"// for InitializerNode
40#include "Parser/StatementNode.h" // for StatementNode
41#include "TypeData.h" // for TypeData, TypeData::Aggregate_t
42#include "TypedefTable.h" // for TypedefTable
43
44extern TypedefTable typedefTable;
45
46using namespace std;
47
48// These must harmonize with the corresponding DeclarationNode enumerations.
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};
66
67UniqueName DeclarationNode::anonymous( "__anonymous" );
68
69extern ast::Linkage::Spec linkage; // defined in parser.yy
70
71DeclarationNode::DeclarationNode() :
72 linkage( ::linkage ) {
73
74// variable.name = nullptr;
75 variable.tyClass = ast::TypeDecl::NUMBER_OF_KINDS;
76 variable.assertions = nullptr;
77 variable.initializer = nullptr;
78
79 assert.condition = nullptr;
80 assert.message = nullptr;
81}
82
83DeclarationNode::~DeclarationNode() {
84// delete variable.name;
85 delete variable.assertions;
86 delete variable.initializer;
87
88// delete type;
89 delete bitfieldWidth;
90
91 delete asmStmt;
92 // asmName, no delete, passed to next stage
93 delete initializer;
94
95 delete assert.condition;
96 delete assert.message;
97}
98
99DeclarationNode * DeclarationNode::clone() const {
100 DeclarationNode * newnode = new DeclarationNode;
101 newnode->set_next( maybeCopy( get_next() ) );
102 newnode->name = name ? new string( *name ) : nullptr;
103
104 newnode->builtin = NoBuiltinType;
105 newnode->type = maybeCopy( type );
106 newnode->inLine = inLine;
107 newnode->storageClasses = storageClasses;
108 newnode->funcSpecs = funcSpecs;
109 newnode->bitfieldWidth = maybeCopy( bitfieldWidth );
110 newnode->enumeratorValue.reset( maybeCopy( enumeratorValue.get() ) );
111 newnode->hasEllipsis = hasEllipsis;
112 newnode->linkage = linkage;
113 newnode->asmName = maybeCopy( asmName );
114 newnode->attributes = attributes;
115 newnode->initializer = maybeCopy( initializer );
116 newnode->extension = extension;
117 newnode->asmStmt = maybeCopy( asmStmt );
118 newnode->error = error;
119
120// newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr;
121 newnode->variable.tyClass = variable.tyClass;
122 newnode->variable.assertions = maybeCopy( variable.assertions );
123 newnode->variable.initializer = maybeCopy( variable.initializer );
124
125 newnode->assert.condition = maybeCopy( assert.condition );
126 newnode->assert.message = maybeCopy( assert.message );
127 return newnode;
128} // DeclarationNode::clone
129
130void DeclarationNode::print( std::ostream & os, int indent ) const {
131 os << string( indent, ' ' );
132 if ( name ) {
133 os << *name << ": ";
134 } // if
135
136 if ( linkage != ast::Linkage::Cforall ) {
137 os << ast::Linkage::name( linkage ) << " ";
138 } // if
139
140 ast::print( os, storageClasses );
141 ast::print( os, funcSpecs );
142
143 if ( type ) {
144 type->print( os, indent );
145 } else {
146 os << "untyped entity ";
147 } // if
148
149 if ( bitfieldWidth ) {
150 os << endl << string( indent + 2, ' ' ) << "with bitfield width ";
151 bitfieldWidth->printOneLine( os );
152 } // if
153
154 if ( initializer ) {
155 os << endl << string( indent + 2, ' ' ) << "with initializer ";
156 initializer->printOneLine( os );
157 os << " maybe constructed? " << initializer->get_maybeConstructed();
158 } // if
159
160 if ( ! attributes.empty() ) {
161 os << string( indent + 2, ' ' ) << "with attributes" << endl;
162 for ( ast::ptr<ast::Attribute> const & attr : reverseIterate( attributes ) ) {
163 os << string( indent + 4, ' ' );
164 ast::print( os, attr, indent + 2 );
165 } // for
166 } // if
167
168 os << endl;
169}
170
171void DeclarationNode::printList( std::ostream & os, int indent ) const {
172 ParseNode::printList( os, indent );
173 if ( hasEllipsis ) {
174 os << string( indent, ' ' ) << "and a variable number of other arguments" << endl;
175 } // if
176}
177
178DeclarationNode * DeclarationNode::newStorageClass( ast::Storage::Classes sc ) {
179 DeclarationNode * newnode = new DeclarationNode;
180 newnode->storageClasses = sc;
181 return newnode;
182} // DeclarationNode::newStorageClass
183
184DeclarationNode * DeclarationNode::newFuncSpecifier( ast::Function::Specs fs ) {
185 DeclarationNode * newnode = new DeclarationNode;
186 newnode->funcSpecs = fs;
187 return newnode;
188} // DeclarationNode::newFuncSpecifier
189
190DeclarationNode * DeclarationNode::newTypeQualifier( ast::CV::Qualifiers tq ) {
191 DeclarationNode * newnode = new DeclarationNode;
192 newnode->type = new TypeData();
193 newnode->type->qualifiers = tq;
194 return newnode;
195} // DeclarationNode::newQualifier
196
197DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
198 DeclarationNode * newnode = new DeclarationNode;
199 newnode->type = new TypeData( TypeData::Basic );
200 newnode->type->basictype = bt;
201 return newnode;
202} // DeclarationNode::newBasicType
203
204DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {
205 DeclarationNode * newnode = new DeclarationNode;
206 newnode->type = new TypeData( TypeData::Basic );
207 newnode->type->complextype = ct;
208 return newnode;
209} // DeclarationNode::newComplexType
210
211DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {
212 DeclarationNode * newnode = new DeclarationNode;
213 newnode->type = new TypeData( TypeData::Basic );
214 newnode->type->signedness = sn;
215 return newnode;
216} // DeclarationNode::newSignedNess
217
218DeclarationNode * DeclarationNode::newLength( Length lnth ) {
219 DeclarationNode * newnode = new DeclarationNode;
220 newnode->type = new TypeData( TypeData::Basic );
221 newnode->type->length = lnth;
222 return newnode;
223} // DeclarationNode::newLength
224
225DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {
226 DeclarationNode * newnode = new DeclarationNode;
227 newnode->type = new TypeData( TypeData::Unknown );
228 newnode->type->forall = forall;
229 return newnode;
230} // DeclarationNode::newForall
231
232DeclarationNode * DeclarationNode::newFromGlobalScope() {
233 DeclarationNode * newnode = new DeclarationNode;
234 newnode->type = new TypeData( TypeData::GlobalScope );
235 return newnode;
236}
237
238DeclarationNode * DeclarationNode::newQualifiedType( DeclarationNode * parent, DeclarationNode * child) {
239 DeclarationNode * newnode = new DeclarationNode;
240 newnode->type = new TypeData( TypeData::Qualified );
241 newnode->type->qualified.parent = parent->type;
242 newnode->type->qualified.child = child->type;
243 parent->type = nullptr;
244 child->type = nullptr;
245 delete parent;
246 delete child;
247 return newnode;
248}
249
250DeclarationNode * DeclarationNode::newAggregate( ast::AggregateDecl::Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
251 DeclarationNode * newnode = new DeclarationNode;
252 newnode->type = new TypeData( TypeData::Aggregate );
253 newnode->type->aggregate.kind = kind;
254 newnode->type->aggregate.anon = name == nullptr;
255 newnode->type->aggregate.name = newnode->type->aggregate.anon ? new string( DeclarationNode::anonymous.newName() ) : name;
256 newnode->type->aggregate.actuals = actuals;
257 newnode->type->aggregate.fields = fields;
258 newnode->type->aggregate.body = body;
259 newnode->type->aggregate.tagged = false;
260 newnode->type->aggregate.parent = nullptr;
261 return newnode;
262} // DeclarationNode::newAggregate
263
264DeclarationNode * DeclarationNode::newEnum( const string * name, DeclarationNode * constants, bool body, bool typed, DeclarationNode * base, EnumHiding hiding ) {
265 DeclarationNode * newnode = new DeclarationNode;
266 newnode->type = new TypeData( TypeData::Enum );
267 newnode->type->enumeration.anon = name == nullptr;
268 newnode->type->enumeration.name = newnode->type->enumeration.anon ? new string( DeclarationNode::anonymous.newName() ) : name;
269 newnode->type->enumeration.constants = constants;
270 newnode->type->enumeration.body = body;
271 newnode->type->enumeration.typed = typed;
272 newnode->type->enumeration.hiding = hiding;
273 if ( base && base->type ) {
274 newnode->type->base = base->type;
275 } // if
276
277 return newnode;
278} // DeclarationNode::newEnum
279
280DeclarationNode * DeclarationNode::newName( const string * name ) {
281 DeclarationNode * newnode = new DeclarationNode;
282 assert( ! newnode->name );
283 newnode->name = name;
284 return newnode;
285} // DeclarationNode::newName
286
287DeclarationNode * DeclarationNode::newEnumConstant( const string * name, ExpressionNode * constant ) {
288 DeclarationNode * newnode = newName( name );
289 newnode->enumeratorValue.reset( constant );
290 return newnode;
291} // DeclarationNode::newEnumConstant
292
293DeclarationNode * DeclarationNode::newEnumValueGeneric( const string * name, InitializerNode * init ) {
294 if ( init ) {
295 if ( init->get_expression() ) {
296 return newEnumConstant( name, init->get_expression() );
297 } else {
298 DeclarationNode * newnode = newName( name );
299 newnode->initializer = init;
300 return newnode;
301 } // if
302 } else {
303 return newName( name );
304 } // if
305} // DeclarationNode::newEnumValueGeneric
306
307DeclarationNode * DeclarationNode::newEnumInLine( const string name ) {
308 DeclarationNode * newnode = newName( new std::string(name) );
309 newnode->enumInLine = true;
310 return newnode;
311}
312
313DeclarationNode * DeclarationNode::newFromTypedef( const string * name ) {
314 DeclarationNode * newnode = new DeclarationNode;
315 newnode->type = new TypeData( TypeData::SymbolicInst );
316 newnode->type->symbolic.name = name;
317 newnode->type->symbolic.isTypedef = true;
318 newnode->type->symbolic.params = nullptr;
319 return newnode;
320} // DeclarationNode::newFromTypedef
321
322DeclarationNode * DeclarationNode::newFromTypeGen( const string * name, ExpressionNode * params ) {
323 DeclarationNode * newnode = new DeclarationNode;
324 newnode->type = new TypeData( TypeData::SymbolicInst );
325 newnode->type->symbolic.name = name;
326 newnode->type->symbolic.isTypedef = false;
327 newnode->type->symbolic.actuals = params;
328 return newnode;
329} // DeclarationNode::newFromTypeGen
330
331DeclarationNode * DeclarationNode::newTypeParam( ast::TypeDecl::Kind tc, const string * name ) {
332 DeclarationNode * newnode = newName( name );
333 newnode->type = nullptr;
334 newnode->variable.tyClass = tc;
335 newnode->variable.assertions = nullptr;
336 return newnode;
337} // DeclarationNode::newTypeParam
338
339DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode * asserts ) {
340 DeclarationNode * newnode = new DeclarationNode;
341 newnode->type = new TypeData( TypeData::Aggregate );
342 newnode->type->aggregate.name = name;
343 newnode->type->aggregate.kind = ast::AggregateDecl::Trait;
344 newnode->type->aggregate.params = params;
345 newnode->type->aggregate.fields = asserts;
346 return newnode;
347} // DeclarationNode::newTrait
348
349DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode * params ) {
350 DeclarationNode * newnode = new DeclarationNode;
351 newnode->type = new TypeData( TypeData::AggregateInst );
352 newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate );
353 newnode->type->aggInst.aggregate->aggregate.kind = ast::AggregateDecl::Trait;
354 newnode->type->aggInst.aggregate->aggregate.name = name;
355 newnode->type->aggInst.params = params;
356 return newnode;
357} // DeclarationNode::newTraitUse
358
359DeclarationNode * DeclarationNode::newTypeDecl( const string * name, DeclarationNode * typeParams ) {
360 DeclarationNode * newnode = newName( name );
361 newnode->type = new TypeData( TypeData::Symbolic );
362 newnode->type->symbolic.isTypedef = false;
363 newnode->type->symbolic.params = typeParams;
364 return newnode;
365} // DeclarationNode::newTypeDecl
366
367DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers, OperKinds kind ) {
368 DeclarationNode * newnode = new DeclarationNode;
369 newnode->type = new TypeData( kind == OperKinds::PointTo ? TypeData::Pointer : TypeData::Reference );
370 if ( kind == OperKinds::And ) {
371 // T && is parsed as 'And' operator rather than two references => add a second reference type
372 TypeData * td = new TypeData( TypeData::Reference );
373 td->base = newnode->type;
374 newnode->type = td;
375 }
376 if ( qualifiers ) {
377 return newnode->addQualifiers( qualifiers );
378 } else {
379 return newnode;
380 } // if
381} // DeclarationNode::newPointer
382
383DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) {
384 DeclarationNode * newnode = new DeclarationNode;
385 newnode->type = new TypeData( TypeData::Array );
386 newnode->type->array.dimension = size;
387 newnode->type->array.isStatic = isStatic;
388 if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ast::ConstantExpr *>() ) {
389 newnode->type->array.isVarLen = false;
390 } else {
391 newnode->type->array.isVarLen = true;
392 } // if
393 return newnode->addQualifiers( qualifiers );
394} // DeclarationNode::newArray
395
396DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) {
397 DeclarationNode * newnode = new DeclarationNode;
398 newnode->type = new TypeData( TypeData::Array );
399 newnode->type->array.dimension = nullptr;
400 newnode->type->array.isStatic = false;
401 newnode->type->array.isVarLen = true;
402 return newnode->addQualifiers( qualifiers );
403}
404
405DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) {
406 DeclarationNode * newnode = new DeclarationNode;
407 newnode->bitfieldWidth = size;
408 return newnode;
409}
410
411DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) {
412 DeclarationNode * newnode = new DeclarationNode;
413 newnode->type = new TypeData( TypeData::Tuple );
414 newnode->type->tuple = members;
415 return newnode;
416}
417
418DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr, bool basetypeof ) {
419 DeclarationNode * newnode = new DeclarationNode;
420 newnode->type = new TypeData( basetypeof ? TypeData::Basetypeof : TypeData::Typeof );
421 newnode->type->typeexpr = expr;
422 return newnode;
423}
424
425DeclarationNode * DeclarationNode::newVtableType( DeclarationNode * decl ) {
426 DeclarationNode * newnode = new DeclarationNode;
427 newnode->type = new TypeData( TypeData::Vtable );
428 newnode->setBase( decl->type );
429 return newnode;
430}
431
432DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
433 DeclarationNode * newnode = new DeclarationNode;
434 newnode->type = new TypeData( TypeData::Builtin );
435 newnode->builtin = bt;
436 newnode->type->builtintype = newnode->builtin;
437 return newnode;
438} // DeclarationNode::newBuiltinType
439
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
455DeclarationNode * DeclarationNode::newAttribute( const string * name, ExpressionNode * expr ) {
456 DeclarationNode * newnode = new DeclarationNode;
457 newnode->type = nullptr;
458 std::vector<ast::ptr<ast::Expr>> exprs;
459 buildList( expr, exprs );
460 newnode->attributes.push_back( new ast::Attribute( *name, std::move( exprs ) ) );
461 delete name;
462 return newnode;
463}
464
465DeclarationNode * DeclarationNode::newDirectiveStmt( StatementNode * stmt ) {
466 DeclarationNode * newnode = new DeclarationNode;
467 newnode->directiveStmt = stmt;
468 return newnode;
469}
470
471DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) {
472 DeclarationNode * newnode = new DeclarationNode;
473 newnode->asmStmt = stmt;
474 return newnode;
475}
476
477DeclarationNode * DeclarationNode::newStaticAssert( ExpressionNode * condition, ast::Expr * message ) {
478 DeclarationNode * newnode = new DeclarationNode;
479 newnode->assert.condition = condition;
480 newnode->assert.message = message;
481 return newnode;
482}
483
484static void appendError( string & dst, const string & src ) {
485 if ( src.empty() ) return;
486 if ( dst.empty() ) { dst = src; return; }
487 dst += ", " + src;
488} // appendError
489
490void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
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() );
500 } // for
501} // DeclarationNode::checkQualifiers
502
503void DeclarationNode::checkSpecifiers( DeclarationNode * src ) {
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() );
511 } // if
512
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 }
535 } // if
536
537 appendError( error, src->error );
538} // DeclarationNode::checkSpecifiers
539
540DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q, bool copyattr ) {
541 funcSpecs |= q->funcSpecs;
542 storageClasses |= q->storageClasses;
543
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
552
553 return this;
554} // DeclarationNode::copySpecifiers
555
556static void addQualifiersToType( TypeData *& src, TypeData * dst ) {
557 if ( dst->base ) {
558 addQualifiersToType( src, dst->base );
559 } else if ( dst->kind == TypeData::Function ) {
560 dst->base = src;
561 src = nullptr;
562 } else {
563 dst->qualifiers |= src->qualifiers;
564 } // if
565} // addQualifiersToType
566
567DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
568 if ( ! q ) { return this; } // empty qualifier
569
570 checkSpecifiers( q );
571 copySpecifiers( q );
572
573 if ( ! q->type ) { delete q; return this; }
574
575 if ( ! type ) {
576 type = q->type; // reuse structure
577 q->type = nullptr;
578 delete q;
579 return this;
580 } // if
581
582 if ( q->type->forall ) { // forall qualifier ?
583 if ( type->forall ) { // polymorphic routine ?
584 type->forall->appendList( q->type->forall ); // augment forall qualifier
585 } else {
586 if ( type->kind == TypeData::Aggregate ) { // struct/union ?
587 if ( type->aggregate.params ) { // polymorphic ?
588 type->aggregate.params->appendList( q->type->forall ); // augment forall qualifier
589 } else { // not polymorphic
590 type->aggregate.params = q->type->forall; // set forall qualifier
591 } // if
592 } else { // not polymorphic
593 type->forall = q->type->forall; // make polymorphic routine
594 } // if
595 } // if
596 q->type->forall = nullptr; // forall qualifier moved
597 } // if
598
599 checkQualifiers( type, q->type );
600 if ( (builtin == Zero || builtin == One) && q->type->qualifiers.any() && error.length() == 0 ) {
601 SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, builtinTypeNames[builtin] );
602 } // if
603 addQualifiersToType( q->type, type );
604
605 delete q;
606 return this;
607} // addQualifiers
608
609static void addTypeToType( TypeData *& src, TypeData *& dst ) {
610 if ( src->forall && dst->kind == TypeData::Function ) {
611 if ( dst->forall ) {
612 dst->forall->appendList( src->forall );
613 } else {
614 dst->forall = src->forall;
615 } // if
616 src->forall = nullptr;
617 } // if
618 if ( dst->base ) {
619 addTypeToType( src, dst->base );
620 } else {
621 switch ( dst->kind ) {
622 case TypeData::Unknown:
623 src->qualifiers |= dst->qualifiers;
624 dst = src;
625 src = nullptr;
626 break;
627 case TypeData::Basic:
628 dst->qualifiers |= src->qualifiers;
629 if ( src->kind != TypeData::Unknown ) {
630 assert( src->kind == TypeData::Basic );
631
632 if ( dst->basictype == DeclarationNode::NoBasicType ) {
633 dst->basictype = src->basictype;
634 } else if ( src->basictype != DeclarationNode::NoBasicType )
635 SemanticError( yylloc, "multiple declaration types \"%s\" and \"%s\".",
636 DeclarationNode::basicTypeNames[ dst->basictype ],
637 DeclarationNode::basicTypeNames[ src->basictype ] );
638 if ( dst->complextype == DeclarationNode::NoComplexType ) {
639 dst->complextype = src->complextype;
640 } else if ( src->complextype != DeclarationNode::NoComplexType )
641 SemanticError( yylloc, "multiple declaration types \"%s\" and \"%s\".",
642 DeclarationNode::complexTypeNames[ src->complextype ],
643 DeclarationNode::complexTypeNames[ src->complextype ] );
644 if ( dst->signedness == DeclarationNode::NoSignedness ) {
645 dst->signedness = src->signedness;
646 } else if ( src->signedness != DeclarationNode::NoSignedness )
647 SemanticError( yylloc, "conflicting type specifier \"%s\" and \"%s\".",
648 DeclarationNode::signednessNames[ dst->signedness ],
649 DeclarationNode::signednessNames[ src->signedness ] );
650 if ( dst->length == DeclarationNode::NoLength ) {
651 dst->length = src->length;
652 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
653 dst->length = DeclarationNode::LongLong;
654 } else if ( src->length != DeclarationNode::NoLength )
655 SemanticError( yylloc, "conflicting type specifier \"%s\" and \"%s\".",
656 DeclarationNode::lengthNames[ dst->length ],
657 DeclarationNode::lengthNames[ src->length ] );
658 } // if
659 break;
660 default:
661 switch ( src->kind ) {
662 case TypeData::Aggregate:
663 case TypeData::Enum:
664 dst->base = new TypeData( TypeData::AggregateInst );
665 dst->base->aggInst.aggregate = src;
666 if ( src->kind == TypeData::Aggregate ) {
667 dst->base->aggInst.params = maybeCopy( src->aggregate.actuals );
668 } // if
669 dst->base->qualifiers |= src->qualifiers;
670 src = nullptr;
671 break;
672 default:
673 if ( dst->forall ) {
674 dst->forall->appendList( src->forall );
675 } else {
676 dst->forall = src->forall;
677 } // if
678 src->forall = nullptr;
679 dst->base = src;
680 src = nullptr;
681 } // switch
682 } // switch
683 } // if
684}
685
686DeclarationNode * DeclarationNode::addType( DeclarationNode * o, bool copyattr ) {
687 if ( o ) {
688 checkSpecifiers( o );
689 copySpecifiers( o, copyattr );
690 if ( o->type ) {
691 if ( ! type ) {
692 if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
693 // Hide type information aggregate instances.
694 type = new TypeData( TypeData::AggregateInst );
695 type->aggInst.aggregate = o->type; // change ownership
696 type->aggInst.aggregate->aggregate.attributes.swap( o->attributes ); // change ownership
697 if ( o->type->kind == TypeData::Aggregate ) {
698 type->aggInst.hoistType = o->type->aggregate.body;
699 type->aggInst.params = maybeCopy( o->type->aggregate.actuals );
700 } else {
701 type->aggInst.hoistType = o->type->enumeration.body;
702 } // if
703 type->qualifiers |= o->type->qualifiers;
704 } else {
705 type = o->type;
706 } // if
707 o->type = nullptr; // change ownership
708 } else {
709 addTypeToType( o->type, type );
710 } // if
711 } // if
712 if ( o->bitfieldWidth ) {
713 bitfieldWidth = o->bitfieldWidth;
714 } // if
715
716 // there may be typedefs chained onto the type
717 if ( o->get_next() ) {
718 set_last( o->get_next()->clone() );
719 } // if
720 } // if
721 delete o;
722
723 return this;
724}
725
726DeclarationNode * DeclarationNode::addEnumBase( DeclarationNode * o ) {
727 if ( o && o->type) {
728 type->base= o->type;
729 } // if
730 delete o;
731 return this;
732}
733
734DeclarationNode * DeclarationNode::addTypedef() {
735 TypeData * newtype = new TypeData( TypeData::Symbolic );
736 newtype->symbolic.params = nullptr;
737 newtype->symbolic.isTypedef = true;
738 newtype->symbolic.name = name ? new string( *name ) : nullptr;
739 newtype->base = type;
740 type = newtype;
741 return this;
742}
743
744DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
745 if ( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS ) {
746 if ( variable.assertions ) {
747 variable.assertions->appendList( assertions );
748 } else {
749 variable.assertions = assertions;
750 } // if
751 return this;
752 } // if
753
754 assert( type );
755 switch ( type->kind ) {
756 case TypeData::Symbolic:
757 if ( type->symbolic.assertions ) {
758 type->symbolic.assertions->appendList( assertions );
759 } else {
760 type->symbolic.assertions = assertions;
761 } // if
762 break;
763 default:
764 assert( false );
765 } // switch
766
767 return this;
768}
769
770DeclarationNode * DeclarationNode::addName( string * newname ) {
771 assert( ! name );
772 name = newname;
773 return this;
774}
775
776DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
777 assert( ! asmName );
778 asmName = newname ? newname->asmName : nullptr;
779 return this->addQualifiers( newname );
780}
781
782DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
783 bitfieldWidth = size;
784 return this;
785}
786
787DeclarationNode * DeclarationNode::addVarArgs() {
788 assert( type );
789 hasEllipsis = true;
790 return this;
791}
792
793DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) {
794 assert( type );
795 assert( type->kind == TypeData::Function );
796 assert( ! type->function.body );
797 type->function.body = body;
798 type->function.withExprs = withExprs;
799 return this;
800}
801
802DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
803 assert( type );
804 assert( type->kind == TypeData::Function );
805 assert( ! type->function.oldDeclList );
806 type->function.oldDeclList = list;
807 return this;
808}
809
810DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
811 if ( type ) {
812 TypeData * prevBase = type;
813 TypeData * curBase = type->base;
814 while ( curBase != nullptr ) {
815 prevBase = curBase;
816 curBase = curBase->base;
817 } // while
818 prevBase->base = newType;
819 } else {
820 type = newType;
821 } // if
822 return this;
823}
824
825DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
826 if ( a ) {
827 spliceBegin( attributes, a->attributes );
828 a->attributes.clear();
829 } // if
830 return this;
831} // copyAttribute
832
833DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
834 if ( p ) {
835 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
836 setBase( p->type );
837 p->type = nullptr;
838 copyAttribute( p );
839 delete p;
840 } // if
841 return this;
842}
843
844DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
845 if ( a ) {
846 assert( a->type->kind == TypeData::Array );
847 setBase( a->type );
848 a->type = nullptr;
849 copyAttribute( a );
850 delete a;
851 } // if
852 return this;
853}
854
855DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
856 if ( p ) {
857 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
858 if ( type ) {
859 switch ( type->kind ) {
860 case TypeData::Aggregate:
861 case TypeData::Enum:
862 p->type->base = new TypeData( TypeData::AggregateInst );
863 p->type->base->aggInst.aggregate = type;
864 if ( type->kind == TypeData::Aggregate ) {
865 p->type->base->aggInst.params = maybeCopy( type->aggregate.actuals );
866 } // if
867 p->type->base->qualifiers |= type->qualifiers;
868 break;
869
870 default:
871 p->type->base = type;
872 } // switch
873 type = nullptr;
874 } // if
875 delete this;
876 return p;
877 } else {
878 return this;
879 } // if
880}
881
882static TypeData * findLast( TypeData * a ) {
883 assert( a );
884 TypeData * cur = a;
885 while ( cur->base ) {
886 cur = cur->base;
887 } // while
888 return cur;
889}
890
891DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
892 if ( ! a ) return this;
893 assert( a->type->kind == TypeData::Array );
894 TypeData * lastArray = findLast( a->type );
895 if ( type ) {
896 switch ( type->kind ) {
897 case TypeData::Aggregate:
898 case TypeData::Enum:
899 lastArray->base = new TypeData( TypeData::AggregateInst );
900 lastArray->base->aggInst.aggregate = type;
901 if ( type->kind == TypeData::Aggregate ) {
902 lastArray->base->aggInst.params = maybeCopy( type->aggregate.actuals );
903 } // if
904 lastArray->base->qualifiers |= type->qualifiers;
905 break;
906 default:
907 lastArray->base = type;
908 } // switch
909 type = nullptr;
910 } // if
911 delete this;
912 return a;
913}
914
915DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
916 TypeData * ftype = new TypeData( TypeData::Function );
917 ftype->function.params = params;
918 setBase( ftype );
919 return this;
920}
921
922static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
923 if ( type ) {
924 if ( type->kind != TypeData::Function ) {
925 type->base = addIdListToType( type->base, ids );
926 } else {
927 type->function.idList = ids;
928 } // if
929 return type;
930 } else {
931 TypeData * newtype = new TypeData( TypeData::Function );
932 newtype->function.idList = ids;
933 return newtype;
934 } // if
935} // addIdListToType
936
937DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
938 type = addIdListToType( type, ids );
939 return this;
940}
941
942DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
943 initializer = init;
944 return this;
945}
946
947DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) {
948 assertf( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS, "Called addTypeInitializer on something that isn't a type variable." );
949 variable.initializer = init;
950 return this;
951}
952
953DeclarationNode * DeclarationNode::cloneType( string * name ) {
954 DeclarationNode * newnode = newName( name );
955 newnode->type = maybeCopy( type );
956 newnode->copySpecifiers( this );
957 return newnode;
958}
959
960DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o, bool copyattr ) {
961 if ( ! o ) return nullptr;
962
963 o->copySpecifiers( this, copyattr );
964 if ( type ) {
965 TypeData * srcType = type;
966
967 // search for the base type by scanning off pointers and array designators
968 while ( srcType->base ) {
969 srcType = srcType->base;
970 } // while
971
972 TypeData * newType = srcType->clone();
973 if ( newType->kind == TypeData::AggregateInst ) {
974 // don't duplicate members
975 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
976 delete newType->aggInst.aggregate->enumeration.constants;
977 newType->aggInst.aggregate->enumeration.constants = nullptr;
978 newType->aggInst.aggregate->enumeration.body = false;
979 } else {
980 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
981 delete newType->aggInst.aggregate->aggregate.fields;
982 newType->aggInst.aggregate->aggregate.fields = nullptr;
983 newType->aggInst.aggregate->aggregate.body = false;
984 } // if
985 // don't hoist twice
986 newType->aggInst.hoistType = false;
987 } // if
988
989 newType->forall = maybeCopy( type->forall );
990 if ( ! o->type ) {
991 o->type = newType;
992 } else {
993 addTypeToType( newType, o->type );
994 delete newType;
995 } // if
996 } // if
997 return o;
998}
999
1000DeclarationNode * DeclarationNode::extractAggregate() const {
1001 if ( type ) {
1002 TypeData * ret = typeextractAggregate( type );
1003 if ( ret ) {
1004 DeclarationNode * newnode = new DeclarationNode;
1005 newnode->type = ret;
1006 if ( ret->kind == TypeData::Aggregate ) {
1007 newnode->attributes.swap( ret->aggregate.attributes );
1008 } // if
1009 return newnode;
1010 } // if
1011 } // if
1012 return nullptr;
1013}
1014
1015// If a typedef wraps an anonymous declaration, name the inner declaration so it has a consistent name across
1016// translation units.
1017static void nameTypedefedDecl(
1018 DeclarationNode * innerDecl,
1019 const DeclarationNode * outerDecl ) {
1020 TypeData * outer = outerDecl->type;
1021 assert( outer );
1022 // First make sure this is a typedef:
1023 if ( outer->kind != TypeData::Symbolic || !outer->symbolic.isTypedef ) {
1024 return;
1025 }
1026 TypeData * inner = innerDecl->type;
1027 assert( inner );
1028 // Always clear any CVs associated with the aggregate:
1029 inner->qualifiers.reset();
1030 // Handle anonymous aggregates: typedef struct { int i; } foo
1031 if ( inner->kind == TypeData::Aggregate && inner->aggregate.anon ) {
1032 delete inner->aggregate.name;
1033 inner->aggregate.name = new string( "__anonymous_" + *outerDecl->name );
1034 inner->aggregate.anon = false;
1035 assert( outer->base );
1036 delete outer->base->aggInst.aggregate->aggregate.name;
1037 outer->base->aggInst.aggregate->aggregate.name = new string( "__anonymous_" + *outerDecl->name );
1038 outer->base->aggInst.aggregate->aggregate.anon = false;
1039 outer->base->aggInst.aggregate->qualifiers.reset();
1040 // Handle anonymous enumeration: typedef enum { A, B, C } foo
1041 } else if ( inner->kind == TypeData::Enum && inner->enumeration.anon ) {
1042 delete inner->enumeration.name;
1043 inner->enumeration.name = new string( "__anonymous_" + *outerDecl->name );
1044 inner->enumeration.anon = false;
1045 assert( outer->base );
1046 delete outer->base->aggInst.aggregate->enumeration.name;
1047 outer->base->aggInst.aggregate->enumeration.name = new string( "__anonymous_" + *outerDecl->name );
1048 outer->base->aggInst.aggregate->enumeration.anon = false;
1049 // No qualifiers.reset() here.
1050 }
1051}
1052
1053// This code handles a special issue with the attribute transparent_union.
1054//
1055// typedef union U { int i; } typedef_name __attribute__(( aligned(16) )) __attribute__(( transparent_union ))
1056//
1057// Here the attribute aligned goes with the typedef_name, so variables declared of this type are
1058// aligned. However, the attribute transparent_union must be moved from the typedef_name to
1059// alias union U. Currently, this is the only know attribute that must be moved from typedef to
1060// alias.
1061static void moveUnionAttribute( ast::Decl * decl, ast::UnionDecl * unionDecl ) {
1062 if ( auto typedefDecl = dynamic_cast<ast::TypedefDecl *>( decl ) ) {
1063 // Is the typedef alias a union aggregate?
1064 if ( nullptr == unionDecl ) return;
1065
1066 // If typedef is an alias for a union, then its alias type was hoisted above and remembered.
1067 if ( auto unionInstType = typedefDecl->base.as<ast::UnionInstType>() ) {
1068 auto instType = ast::mutate( unionInstType );
1069 // Remove all transparent_union attributes from typedef and move to alias union.
1070 for ( auto attr = instType->attributes.begin() ; attr != instType->attributes.end() ; ) {
1071 assert( *attr );
1072 if ( (*attr)->name == "transparent_union" || (*attr)->name == "__transparent_union__" ) {
1073 unionDecl->attributes.emplace_back( attr->release() );
1074 attr = instType->attributes.erase( attr );
1075 } else {
1076 attr++;
1077 }
1078 }
1079 typedefDecl->base = instType;
1080 }
1081 }
1082}
1083
1084// Get the non-anonymous name of the instance type of the declaration,
1085// if one exists.
1086static const std::string * getInstTypeOfName( ast::Decl * decl ) {
1087 if ( auto dwt = dynamic_cast<ast::DeclWithType *>( decl ) ) {
1088 if ( auto aggr = dynamic_cast<ast::BaseInstType const *>( dwt->get_type() ) ) {
1089 if ( aggr->name.find("anonymous") == std::string::npos ) {
1090 return &aggr->name;
1091 }
1092 }
1093 }
1094 return nullptr;
1095}
1096
1097void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::Decl>> & outputList ) {
1098 SemanticErrorException errors;
1099 std::back_insert_iterator<std::vector<ast::ptr<ast::Decl>>> out( outputList );
1100
1101 for ( const DeclarationNode * cur = firstNode ; cur ; cur = strict_next( cur ) ) {
1102 try {
1103 bool extracted_named = false;
1104 ast::UnionDecl * unionDecl = nullptr;
1105
1106 if ( DeclarationNode * extr = cur->extractAggregate() ) {
1107 assert( cur->type );
1108 nameTypedefedDecl( extr, cur );
1109
1110 if ( ast::Decl * decl = extr->build() ) {
1111 // Remember the declaration if it is a union aggregate ?
1112 unionDecl = dynamic_cast<ast::UnionDecl *>( decl );
1113
1114 *out++ = decl;
1115
1116 // need to remember the cases where a declaration contains an anonymous aggregate definition
1117 assert( extr->type );
1118 if ( extr->type->kind == TypeData::Aggregate ) {
1119 // typedef struct { int A } B is the only case?
1120 extracted_named = ! extr->type->aggregate.anon;
1121 } else if ( extr->type->kind == TypeData::Enum ) {
1122 // typedef enum { A } B is the only case?
1123 extracted_named = ! extr->type->enumeration.anon;
1124 } else {
1125 extracted_named = true;
1126 }
1127 } // if
1128 delete extr;
1129 } // if
1130
1131 if ( ast::Decl * decl = cur->build() ) {
1132 moveUnionAttribute( decl, unionDecl );
1133
1134 if ( "" == decl->name && !cur->get_inLine() ) {
1135 // Don't include anonymous declaration for named aggregates,
1136 // but do include them for anonymous aggregates, e.g.:
1137 // struct S {
1138 // struct T { int x; }; // no anonymous member
1139 // struct { int y; }; // anonymous member
1140 // struct T; // anonymous member
1141 // };
1142 if ( extracted_named ) {
1143 continue;
1144 }
1145
1146 if ( auto name = getInstTypeOfName( decl ) ) {
1147 // Temporary: warn about anonymous member declarations of named types, since
1148 // this conflicts with the syntax for the forward declaration of an anonymous type.
1149 SemanticWarning( cur->location, Warning::AggrForwardDecl, name->c_str() );
1150 }
1151 } // if
1152 *out++ = decl;
1153 } // if
1154 } catch ( SemanticErrorException & e ) {
1155 errors.append( e );
1156 } // try
1157 } // for
1158
1159 if ( ! errors.isEmpty() ) {
1160 throw errors;
1161 } // if
1162} // buildList
1163
1164// currently only builds assertions, function parameters, and return values
1165void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::DeclWithType>> & outputList ) {
1166 SemanticErrorException errors;
1167 std::back_insert_iterator<std::vector<ast::ptr<ast::DeclWithType>>> out( outputList );
1168
1169 for ( const DeclarationNode * cur = firstNode; cur; cur = strict_next( cur ) ) {
1170 try {
1171 ast::Decl * decl = cur->build();
1172 assertf( decl, "buildList: build for ast::DeclWithType." );
1173 if ( ast::DeclWithType * dwt = dynamic_cast<ast::DeclWithType *>( decl ) ) {
1174 dwt->location = cur->location;
1175 *out++ = dwt;
1176 } else if ( ast::StructDecl * agg = dynamic_cast<ast::StructDecl *>( decl ) ) {
1177 // e.g., int foo(struct S) {}
1178 auto inst = new ast::StructInstType( agg->name );
1179 auto obj = new ast::ObjectDecl( cur->location, "", inst );
1180 obj->linkage = linkage;
1181 *out++ = obj;
1182 delete agg;
1183 } else if ( ast::UnionDecl * agg = dynamic_cast<ast::UnionDecl *>( decl ) ) {
1184 // e.g., int foo(union U) {}
1185 auto inst = new ast::UnionInstType( agg->name );
1186 auto obj = new ast::ObjectDecl( cur->location,
1187 "", inst, nullptr, ast::Storage::Classes(),
1188 linkage );
1189 *out++ = obj;
1190 } else if ( ast::EnumDecl * agg = dynamic_cast<ast::EnumDecl *>( decl ) ) {
1191 // e.g., int foo(enum E) {}
1192 auto inst = new ast::EnumInstType( agg->name );
1193 auto obj = new ast::ObjectDecl( cur->location,
1194 "",
1195 inst,
1196 nullptr,
1197 ast::Storage::Classes(),
1198 linkage
1199 );
1200 *out++ = obj;
1201 } else {
1202 assertf( false, "buildList: Could not convert to ast::DeclWithType." );
1203 } // if
1204 } catch ( SemanticErrorException & e ) {
1205 errors.append( e );
1206 } // try
1207 } // for
1208
1209 if ( ! errors.isEmpty() ) {
1210 throw errors;
1211 } // if
1212} // buildList
1213
1214void buildTypeList( const DeclarationNode * firstNode,
1215 std::vector<ast::ptr<ast::Type>> & outputList ) {
1216 SemanticErrorException errors;
1217 std::back_insert_iterator<std::vector<ast::ptr<ast::Type>>> out( outputList );
1218
1219 for ( const DeclarationNode * cur = firstNode ; cur ; cur = strict_next( cur ) ) {
1220 try {
1221 * out++ = cur->buildType();
1222 } catch ( SemanticErrorException & e ) {
1223 errors.append( e );
1224 } // try
1225 } // for
1226
1227 if ( ! errors.isEmpty() ) {
1228 throw errors;
1229 } // if
1230} // buildTypeList
1231
1232ast::Decl * DeclarationNode::build() const {
1233 if ( ! error.empty() ) SemanticError( this, error + " in declaration of " );
1234
1235 if ( asmStmt ) {
1236 auto stmt = strict_dynamic_cast<ast::AsmStmt *>( asmStmt->build() );
1237 return new ast::AsmDecl( stmt->location, stmt );
1238 } // if
1239 if ( directiveStmt ) {
1240 auto stmt = strict_dynamic_cast<ast::DirectiveStmt *>( directiveStmt->build() );
1241 return new ast::DirectiveDecl( stmt->location, stmt );
1242 } // if
1243
1244 if ( variable.tyClass != ast::TypeDecl::NUMBER_OF_KINDS ) {
1245 // otype is internally converted to dtype + otype parameters
1246 static const ast::TypeDecl::Kind kindMap[] = { ast::TypeDecl::Dtype, ast::TypeDecl::Dtype, ast::TypeDecl::Dtype, ast::TypeDecl::Ftype, ast::TypeDecl::Ttype, ast::TypeDecl::Dimension };
1247 static_assert( sizeof(kindMap) / sizeof(kindMap[0]) == ast::TypeDecl::NUMBER_OF_KINDS, "DeclarationNode::build: kindMap is out of sync." );
1248 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
1249 ast::TypeDecl * ret = new ast::TypeDecl( location,
1250 *name,
1251 ast::Storage::Classes(),
1252 (ast::Type *)nullptr,
1253 kindMap[ variable.tyClass ],
1254 variable.tyClass == ast::TypeDecl::Otype || variable.tyClass == ast::TypeDecl::DStype,
1255 variable.initializer ? variable.initializer->buildType() : nullptr
1256 );
1257 buildList( variable.assertions, ret->assertions );
1258 return ret;
1259 } // if
1260
1261 if ( type ) {
1262 // Function specifiers can only appear on a function definition/declaration.
1263 //
1264 // inline _Noreturn int f(); // allowed
1265 // inline _Noreturn int g( int i ); // allowed
1266 // inline _Noreturn int i; // disallowed
1267 if ( type->kind != TypeData::Function && funcSpecs.any() ) {
1268 SemanticError( this, "invalid function specifier for " );
1269 } // if
1270 // Forall qualifier can only appear on a function/aggregate definition/declaration.
1271 //
1272 // forall int f(); // allowed
1273 // forall int g( int i ); // allowed
1274 // forall int i; // disallowed
1275 if ( type->kind != TypeData::Function && type->forall ) {
1276 SemanticError( this, "invalid type qualifier for " );
1277 } // if
1278 bool isDelete = initializer && initializer->get_isDelete();
1279 ast::Decl * decl = buildDecl(
1280 type,
1281 name ? *name : string( "" ),
1282 storageClasses,
1283 maybeBuild( bitfieldWidth ),
1284 funcSpecs,
1285 linkage,
1286 asmName,
1287 isDelete ? nullptr : maybeBuild( initializer ),
1288 copy( attributes )
1289 )->set_extension( extension );
1290 if ( isDelete ) {
1291 auto dwt = strict_dynamic_cast<ast::DeclWithType *>( decl );
1292 dwt->isDeleted = true;
1293 }
1294 return decl;
1295 } // if
1296
1297 if ( assert.condition ) {
1298 auto cond = maybeBuild( assert.condition );
1299 auto msg = strict_dynamic_cast<ast::ConstantExpr *>( maybeCopy( assert.message ) );
1300 return new ast::StaticAssertDecl( location, cond, msg );
1301 }
1302
1303 // SUE's cannot have function specifiers, either
1304 //
1305 // inline _Noreturn struct S { ... }; // disallowed
1306 // inline _Noreturn enum E { ... }; // disallowed
1307 if ( funcSpecs.any() ) {
1308 SemanticError( this, "invalid function specifier for " );
1309 } // if
1310 if ( enumInLine ) {
1311 return new ast::InlineMemberDecl( location,
1312 *name, (ast::Type*)nullptr, storageClasses, linkage );
1313 } // if
1314 assertf( name, "ObjectDecl must a have name\n" );
1315 auto ret = new ast::ObjectDecl( location,
1316 *name,
1317 (ast::Type*)nullptr,
1318 maybeBuild( initializer ),
1319 storageClasses,
1320 linkage,
1321 maybeBuild( bitfieldWidth )
1322 );
1323 ret->asmName = asmName;
1324 ret->extension = extension;
1325 return ret;
1326}
1327
1328ast::Type * DeclarationNode::buildType() const {
1329 assert( type );
1330
1331 switch ( type->kind ) {
1332 case TypeData::Enum:
1333 case TypeData::Aggregate: {
1334 ast::BaseInstType * ret =
1335 buildComAggInst( type, copy( attributes ), linkage );
1336 buildList( type->aggregate.actuals, ret->params );
1337 return ret;
1338 }
1339 case TypeData::Symbolic: {
1340 ast::TypeInstType * ret = new ast::TypeInstType(
1341 *type->symbolic.name,
1342 // This is just a default, the true value is not known yet.
1343 ast::TypeDecl::Dtype,
1344 buildQualifiers( type ),
1345 copy( attributes ) );
1346 buildList( type->symbolic.actuals, ret->params );
1347 return ret;
1348 }
1349 default:
1350 ast::Type * simpletypes = typebuild( type );
1351 // copy because member is const
1352 simpletypes->attributes = attributes;
1353 return simpletypes;
1354 } // switch
1355}
1356
1357// Local Variables: //
1358// tab-width: 4 //
1359// mode: c++ //
1360// compile-command: "make install" //
1361// End: //
Note: See TracBrowser for help on using the repository browser.