source: src/Parser/DeclarationNode.cc @ c468150

ADTast-experimental
Last change on this file since c468150 was c468150, checked in by Andrew Beach <ajbeach@…>, 16 months ago

Split up ParseNode?.h so that headers match implementation. May have a bit less to include total because of it.

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