source: src/Parser/DeclarationNode.cc @ 73bf7ddc

ADTast-experimental
Last change on this file since 73bf7ddc was bb7422a, checked in by Andrew Beach <ajbeach@…>, 15 months ago

Translated parser to the new ast. This incuded a small fix in the resolver so larger expressions can be used in with statements and some updated tests. errors/declaration just is a formatting update. attributes now actually preserves more attributes (unknown if all versions work).

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