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

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 3ed994e was 3ed994e, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Push deleted decls through the system

  • Property mode set to 100644
File size: 37.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 : Peter A. Buhr
12// Last Modified On : Tue May 22 08:39:29 2018
13// Update Count     : 1074
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 "Common/SemanticError.h"  // for SemanticError
24#include "Common/UniqueName.h"     // for UniqueName
25#include "Common/utility.h"        // for maybeClone, maybeBuild, CodeLocation
26#include "Parser/LinkageSpec.h"    // for Spec, linkageName, Cforall
27#include "Parser/ParseNode.h"      // for DeclarationNode, ExpressionNode
28#include "SynTree/Attribute.h"     // for Attribute
29#include "SynTree/Declaration.h"   // for TypeDecl, ObjectDecl, Declaration
30#include "SynTree/Expression.h"    // for Expression, ConstantExpr
31#include "SynTree/Statement.h"     // for AsmStmt
32#include "SynTree/Type.h"          // for Type, Type::StorageClasses, Type::...
33#include "TypeData.h"              // for TypeData, TypeData::Aggregate_t
34#include "TypedefTable.h"          // for TypedefTable
35
36class Initializer;
37
38extern TypedefTable typedefTable;
39
40using namespace std;
41
42// These must harmonize with the corresponding DeclarationNode enumerations.
43const char * DeclarationNode::basicTypeNames[] = { "void", "_Bool", "char", "int", "float", "double", "long double", "int128", "float80", "float128", "NoBasicTypeNames" };
44const char * DeclarationNode::complexTypeNames[] = { "_Complex", "_Imaginary", "NoComplexTypeNames" };
45const char * DeclarationNode::signednessNames[] = { "signed", "unsigned", "NoSignednessNames" };
46const char * DeclarationNode::lengthNames[] = { "short", "long", "long long", "NoLengthNames" };
47const char * DeclarationNode::aggregateNames[] = { "struct", "union", "trait", "coroutine", "monitor", "thread", "NoAggregateNames" };
48const char * DeclarationNode::typeClassNames[] = { "otype", "dtype", "ftype", "NoTypeClassNames" };
49const char * DeclarationNode::builtinTypeNames[] = { "__builtin_va_list", "zero_t", "one_t", "NoBuiltinTypeNames" };
50
51UniqueName DeclarationNode::anonymous( "__anonymous" );
52
53extern LinkageSpec::Spec linkage;                                               // defined in parser.yy
54
55DeclarationNode::DeclarationNode() :
56                builtin( NoBuiltinType ),
57                type( nullptr ),
58                bitfieldWidth( nullptr ),
59                hasEllipsis( false ),
60                linkage( ::linkage ),
61                asmName( nullptr ),
62                initializer( nullptr ),
63                extension( false ),
64                asmStmt( nullptr ) {
65
66//      variable.name = nullptr;
67        variable.tyClass = NoTypeClass;
68        variable.assertions = nullptr;
69        variable.initializer = nullptr;
70
71//      attr.name = nullptr;
72        attr.expr = nullptr;
73        attr.type = nullptr;
74
75        assert.condition = nullptr;
76        assert.message = nullptr;
77}
78
79DeclarationNode::~DeclarationNode() {
80//      delete attr.name;
81        delete attr.expr;
82        delete attr.type;
83
84//      delete variable.name;
85        delete variable.assertions;
86        delete variable.initializer;
87
88        delete type;
89        delete bitfieldWidth;
90
91        delete asmStmt;
92        // asmName, no delete, passed to next stage
93        delete initializer;
94
95        delete assert.condition;
96        delete assert.message;
97}
98
99DeclarationNode * DeclarationNode::clone() const {
100        DeclarationNode * newnode = new DeclarationNode;
101        newnode->set_next( maybeClone( get_next() ) );
102        newnode->name = name ? new string( *name ) : nullptr;
103
104        newnode->builtin = NoBuiltinType;
105        newnode->type = maybeClone( type );
106        newnode->storageClasses = storageClasses;
107        newnode->funcSpecs = funcSpecs;
108        newnode->bitfieldWidth = maybeClone( bitfieldWidth );
109        newnode->enumeratorValue.reset( maybeClone( enumeratorValue.get() ) );
110        newnode->hasEllipsis = hasEllipsis;
111        newnode->linkage = linkage;
112        newnode->asmName = maybeClone( asmName );
113        cloneAll( attributes, newnode->attributes );
114        newnode->initializer = maybeClone( initializer );
115        newnode->extension = extension;
116        newnode->asmStmt = maybeClone( asmStmt );
117        newnode->error = error;
118
119//      newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr;
120        newnode->variable.tyClass = variable.tyClass;
121        newnode->variable.assertions = maybeClone( variable.assertions );
122        newnode->variable.initializer = maybeClone( variable.initializer );
123
124//      newnode->attr.name = attr.name ? new string( *attr.name ) : nullptr;
125        newnode->attr.expr = maybeClone( attr.expr );
126        newnode->attr.type = maybeClone( attr.type );
127
128        newnode->assert.condition = maybeClone( assert.condition );
129        newnode->assert.message = maybeClone( assert.message );
130        return newnode;
131} // DeclarationNode::clone
132
133void DeclarationNode::print( std::ostream &os, int indent ) const {
134        os << string( indent, ' ' );
135        if ( name ) {
136                os << *name << ": ";
137        } else {
138                os << "unnamed: ";
139        } // if
140
141        if ( linkage != LinkageSpec::Cforall ) {
142                os << LinkageSpec::linkageName( linkage ) << " ";
143        } // if
144
145        storageClasses.print( os );
146        funcSpecs.print( os );
147
148        if ( type ) {
149                type->print( os, indent );
150        } else {
151                os << "untyped entity ";
152        } // if
153
154        if ( bitfieldWidth ) {
155                os << endl << string( indent + 2, ' ' ) << "with bitfield width ";
156                bitfieldWidth->printOneLine( os );
157        } // if
158
159        if ( initializer ) {
160                os << endl << string( indent + 2, ' ' ) << "with initializer ";
161                initializer->printOneLine( os );
162                os << " maybe constructed? " << initializer->get_maybeConstructed();
163
164        } // if
165
166        os << endl;
167}
168
169void DeclarationNode::printList( std::ostream &os, int indent ) const {
170        ParseNode::printList( os, indent );
171        if ( hasEllipsis ) {
172                os << string( indent, ' ' )  << "and a variable number of other arguments" << endl;
173        } // if
174}
175
176DeclarationNode * DeclarationNode::newFunction( string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body ) {
177        DeclarationNode * newnode = new DeclarationNode;
178        newnode->name = name;
179        newnode->type = new TypeData( TypeData::Function );
180        newnode->type->function.params = param;
181        newnode->type->function.body = body;
182
183        if ( ret ) {
184                newnode->type->base = ret->type;
185                ret->type = nullptr;
186                delete ret;
187        } // if
188
189        return newnode;
190} // DeclarationNode::newFunction
191
192
193DeclarationNode * DeclarationNode::newStorageClass( Type::StorageClasses sc ) {
194        DeclarationNode * newnode = new DeclarationNode;
195        newnode->storageClasses = sc;
196        return newnode;
197} // DeclarationNode::newStorageClass
198
199DeclarationNode * DeclarationNode::newFuncSpecifier( Type::FuncSpecifiers fs ) {
200        DeclarationNode * newnode = new DeclarationNode;
201        newnode->funcSpecs = fs;
202        return newnode;
203} // DeclarationNode::newFuncSpecifier
204
205DeclarationNode * DeclarationNode::newTypeQualifier( Type::Qualifiers tq ) {
206        DeclarationNode * newnode = new DeclarationNode;
207        newnode->type = new TypeData();
208        newnode->type->qualifiers = tq;
209        return newnode;
210} // DeclarationNode::newQualifier
211
212DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
213        DeclarationNode * newnode = new DeclarationNode;
214        newnode->type = new TypeData( TypeData::Basic );
215        newnode->type->basictype = bt;
216        return newnode;
217} // DeclarationNode::newBasicType
218
219DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {
220        DeclarationNode * newnode = new DeclarationNode;
221        newnode->type = new TypeData( TypeData::Basic );
222        newnode->type->complextype = ct;
223        return newnode;
224} // DeclarationNode::newComplexType
225
226DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {
227        DeclarationNode * newnode = new DeclarationNode;
228        newnode->type = new TypeData( TypeData::Basic );
229        newnode->type->signedness = sn;
230        return newnode;
231} // DeclarationNode::newSignedNess
232
233DeclarationNode * DeclarationNode::newLength( Length lnth ) {
234        DeclarationNode * newnode = new DeclarationNode;
235        newnode->type = new TypeData( TypeData::Basic );
236        newnode->type->length = lnth;
237        return newnode;
238} // DeclarationNode::newLength
239
240DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {
241        DeclarationNode * newnode = new DeclarationNode;
242        newnode->type = new TypeData( TypeData::Unknown );
243        newnode->type->forall = forall;
244        return newnode;
245} // DeclarationNode::newForall
246
247DeclarationNode * DeclarationNode::newFromTypedef( string * name ) {
248        DeclarationNode * newnode = new DeclarationNode;
249        newnode->type = new TypeData( TypeData::SymbolicInst );
250        newnode->type->symbolic.name = name;
251        newnode->type->symbolic.isTypedef = true;
252        newnode->type->symbolic.params = nullptr;
253        return newnode;
254} // DeclarationNode::newFromTypedef
255
256DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
257        assert( name );
258        DeclarationNode * newnode = new DeclarationNode;
259        newnode->type = new TypeData( TypeData::Aggregate );
260        newnode->type->aggregate.kind = kind;
261        newnode->type->aggregate.name = name;
262        newnode->type->aggregate.actuals = actuals;
263        newnode->type->aggregate.fields = fields;
264        newnode->type->aggregate.body = body;
265        newnode->type->aggregate.tagged = false;
266        newnode->type->aggregate.parent = nullptr;
267        return newnode;
268} // DeclarationNode::newAggregate
269
270DeclarationNode * DeclarationNode::newEnum( string * name, DeclarationNode * constants, bool body ) {
271        assert( name );
272        DeclarationNode * newnode = new DeclarationNode;
273        newnode->type = new TypeData( TypeData::Enum );
274        newnode->type->enumeration.name = name;
275        newnode->type->enumeration.constants = constants;
276        newnode->type->enumeration.body = body;
277        return newnode;
278} // DeclarationNode::newEnum
279
280DeclarationNode * DeclarationNode::newEnumConstant( string * name, ExpressionNode * constant ) {
281        DeclarationNode * newnode = new DeclarationNode;
282        newnode->name = name;
283        newnode->enumeratorValue.reset( constant );
284        return newnode;
285} // DeclarationNode::newEnumConstant
286
287DeclarationNode * DeclarationNode::newName( string * name ) {
288        DeclarationNode * newnode = new DeclarationNode;
289        newnode->name = name;
290        return newnode;
291} // DeclarationNode::newName
292
293DeclarationNode * DeclarationNode::newFromTypeGen( string * name, ExpressionNode * params ) {
294        DeclarationNode * newnode = new DeclarationNode;
295        newnode->type = new TypeData( TypeData::SymbolicInst );
296        newnode->type->symbolic.name = name;
297        newnode->type->symbolic.isTypedef = false;
298        newnode->type->symbolic.actuals = params;
299        return newnode;
300} // DeclarationNode::newFromTypeGen
301
302DeclarationNode * DeclarationNode::newTypeParam( TypeClass tc, string * name ) {
303        DeclarationNode * newnode = new DeclarationNode;
304        newnode->type = nullptr;
305        assert( ! newnode->name );
306//      newnode->variable.name = name;
307        newnode->name = name;
308        newnode->variable.tyClass = tc;
309        newnode->variable.assertions = nullptr;
310        return newnode;
311} // DeclarationNode::newTypeParam
312
313DeclarationNode * DeclarationNode::newTrait( const string * name, DeclarationNode * params, DeclarationNode * asserts ) {
314        DeclarationNode * newnode = new DeclarationNode;
315        newnode->type = new TypeData( TypeData::Aggregate );
316        newnode->type->aggregate.name = name;
317        newnode->type->aggregate.kind = Trait;
318        newnode->type->aggregate.params = params;
319        newnode->type->aggregate.fields = asserts;
320        return newnode;
321} // DeclarationNode::newTrait
322
323DeclarationNode * DeclarationNode::newTraitUse( const string * name, ExpressionNode * params ) {
324        DeclarationNode * newnode = new DeclarationNode;
325        newnode->type = new TypeData( TypeData::AggregateInst );
326        newnode->type->aggInst.aggregate = new TypeData( TypeData::Aggregate );
327        newnode->type->aggInst.aggregate->aggregate.kind = Trait;
328        newnode->type->aggInst.aggregate->aggregate.name = name;
329        newnode->type->aggInst.params = params;
330        return newnode;
331} // DeclarationNode::newTraitUse
332
333DeclarationNode * DeclarationNode::newTypeDecl( string * name, DeclarationNode * typeParams ) {
334        DeclarationNode * newnode = new DeclarationNode;
335        newnode->name = name;
336        newnode->type = new TypeData( TypeData::Symbolic );
337        newnode->type->symbolic.isTypedef = false;
338        newnode->type->symbolic.params = typeParams;
339        return newnode;
340} // DeclarationNode::newTypeDecl
341
342DeclarationNode * DeclarationNode::newPointer( DeclarationNode * qualifiers, OperKinds kind ) {
343        DeclarationNode * newnode = new DeclarationNode;
344        newnode->type = new TypeData( kind == OperKinds::PointTo ? TypeData::Pointer : TypeData::Reference );
345        if ( kind == OperKinds::And ) {
346                // T && is parsed as 'And' operator rather than two references => add a second reference type
347                TypeData * td = new TypeData( TypeData::Reference );
348                td->base = newnode->type;
349                newnode->type = td;
350        }
351        if ( qualifiers ) {
352                return newnode->addQualifiers( qualifiers );
353        } else {
354                return newnode;
355        } // if
356} // DeclarationNode::newPointer
357
358DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) {
359        DeclarationNode * newnode = new DeclarationNode;
360        newnode->type = new TypeData( TypeData::Array );
361        newnode->type->array.dimension = size;
362        newnode->type->array.isStatic = isStatic;
363        if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ConstantExpr * >() ) {
364                newnode->type->array.isVarLen = false;
365        } else {
366                newnode->type->array.isVarLen = true;
367        } // if
368        return newnode->addQualifiers( qualifiers );
369} // DeclarationNode::newArray
370
371DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) {
372        DeclarationNode * newnode = new DeclarationNode;
373        newnode->type = new TypeData( TypeData::Array );
374        newnode->type->array.dimension = nullptr;
375        newnode->type->array.isStatic = false;
376        newnode->type->array.isVarLen = true;
377        return newnode->addQualifiers( qualifiers );
378}
379
380DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) {
381        DeclarationNode * newnode = new DeclarationNode;
382        newnode->bitfieldWidth = size;
383        return newnode;
384}
385
386DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) {
387        DeclarationNode * newnode = new DeclarationNode;
388        newnode->type = new TypeData( TypeData::Tuple );
389        newnode->type->tuple = members;
390        return newnode;
391}
392
393DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) {
394        DeclarationNode * newnode = new DeclarationNode;
395        newnode->type = new TypeData( TypeData::Typeof );
396        newnode->type->typeexpr = expr;
397        return newnode;
398}
399
400DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
401        DeclarationNode * newnode = new DeclarationNode;
402        newnode->type = new TypeData( TypeData::Builtin );
403        newnode->builtin = bt;
404        newnode->type->builtintype = newnode->builtin;
405        return newnode;
406} // DeclarationNode::newBuiltinType
407
408DeclarationNode * DeclarationNode::newAttr( string * name, ExpressionNode * expr ) {
409        DeclarationNode * newnode = new DeclarationNode;
410        newnode->type = nullptr;
411//      newnode->attr.name = name;
412        newnode->name = name;
413        newnode->attr.expr = expr;
414        return newnode;
415}
416
417DeclarationNode * DeclarationNode::newAttr( string * name, DeclarationNode * type ) {
418        DeclarationNode * newnode = new DeclarationNode;
419        newnode->type = nullptr;
420//      newnode->attr.name = name;
421        newnode->name = name;
422        newnode->attr.type = type;
423        return newnode;
424}
425
426DeclarationNode * DeclarationNode::newAttribute( string * name, ExpressionNode * expr ) {
427        DeclarationNode * newnode = new DeclarationNode;
428        newnode->type = nullptr;
429        std::list< Expression * > exprs;
430        buildList( expr, exprs );
431        newnode->attributes.push_back( new Attribute( *name, exprs ) );
432        delete name;
433        return newnode;
434}
435
436DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) {
437        DeclarationNode * newnode = new DeclarationNode;
438        newnode->asmStmt = stmt;
439        return newnode;
440}
441
442DeclarationNode * DeclarationNode::newStaticAssert( ExpressionNode * condition, Expression * message ) {
443        DeclarationNode * newnode = new DeclarationNode;
444        newnode->assert.condition = condition;
445        newnode->assert.message = message;
446        return newnode;
447}
448
449
450void appendError( string & dst, const string & src ) {
451        if ( src.empty() ) return;
452        if ( dst.empty() ) { dst = src; return; }
453        dst += ", " + src;
454} // appendError
455
456void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
457        const Type::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
458
459        if ( (qsrc & qdst).any() ) {                                            // duplicates ?
460                for ( unsigned int i = 0; i < Type::NumTypeQualifier; i += 1 ) { // find duplicates
461                        if ( qsrc[i] && qdst[i] ) {
462                                appendError( error, string( "duplicate " ) + Type::QualifiersNames[i] );
463                        } // if
464                } // for
465        } // for
466} // DeclarationNode::checkQualifiers
467
468void DeclarationNode::checkSpecifiers( DeclarationNode * src ) {
469        if ( (funcSpecs & src->funcSpecs).any() ) {                     // duplicates ?
470                for ( unsigned int i = 0; i < Type::NumFuncSpecifier; i += 1 ) { // find duplicates
471                        if ( funcSpecs[i] && src->funcSpecs[i] ) {
472                                appendError( error, string( "duplicate " ) + Type::FuncSpecifiersNames[i] );
473                        } // if
474                } // for
475        } // if
476
477        if ( storageClasses.any() && src->storageClasses.any() ) { // any reason to check ?
478                if ( (storageClasses & src->storageClasses ).any() ) { // duplicates ?
479                        for ( unsigned int i = 0; i < Type::NumStorageClass; i += 1 ) { // find duplicates
480                                if ( storageClasses[i] && src->storageClasses[i] ) {
481                                        appendError( error, string( "duplicate " ) + Type::StorageClassesNames[i] );
482                                } // if
483                        } // for
484                        // src is the new item being added and has a single bit
485                } else if ( ! src->storageClasses.is_threadlocal ) { // conflict ?
486                        appendError( error, string( "conflicting " ) + Type::StorageClassesNames[storageClasses.ffs()] +
487                                                 " & " + Type::StorageClassesNames[src->storageClasses.ffs()] );
488                        src->storageClasses.reset();                            // FIX to preserve invariant of one basic storage specifier
489                } // if
490        } // if
491
492        appendError( error, src->error );
493} // DeclarationNode::checkSpecifiers
494
495DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q ) {
496        funcSpecs |= q->funcSpecs;
497        storageClasses |= q->storageClasses;
498
499        for ( Attribute *attr: reverseIterate( q->attributes ) ) {
500                attributes.push_front( attr->clone() );
501        } // for
502        return this;
503} // DeclarationNode::copySpecifiers
504
505static void addQualifiersToType( TypeData *&src, TypeData * dst ) {
506        if ( src->forall && dst->kind == TypeData::Function ) {
507                if ( dst->forall ) {
508                        dst->forall->appendList( src->forall );
509                } else {
510                        dst->forall = src->forall;
511                } // if
512                src->forall = nullptr;
513        } // if
514        if ( dst->base ) {
515                addQualifiersToType( src, dst->base );
516        } else if ( dst->kind == TypeData::Function ) {
517                dst->base = src;
518                src = nullptr;
519        } else {
520                dst->qualifiers |= src->qualifiers;
521        } // if
522} // addQualifiersToType
523
524DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
525        if ( ! q ) { return this; }                                                     // empty qualifier
526
527        checkSpecifiers( q );
528        copySpecifiers( q );
529
530        if ( ! q->type ) { delete q; return this; }
531
532        if ( ! type ) {
533                type = q->type;                                                                 // reuse structure
534                q->type = nullptr;
535                delete q;
536                return this;
537        } // if
538
539        if ( q->type->forall ) {                                                        // forall qualifier ?
540                if ( type->forall ) {                                                   // polymorphic routine ?
541                        type->forall->appendList( q->type->forall ); // augment forall qualifier
542                } else {
543                        if ( type->kind == TypeData::Aggregate ) {      // struct/union ?
544                                if ( type->aggregate.params ) {                 // polymorphic ?
545                                        type->aggregate.params->appendList( q->type->forall ); // augment forall qualifier
546                                } else {                                                                // not polymorphic
547                                        type->aggregate.params = q->type->forall; // make polymorphic type
548                                        // change implicit typedef from TYPEDEFname to TYPEGENname
549                                        typedefTable.changeKind( *type->aggregate.name, TYPEGENname );
550                                } // if
551                        } else {                                                                        // not polymorphic
552                                type->forall = q->type->forall;                 // make polymorphic routine
553                        } // if
554                } // if
555                q->type->forall = nullptr;                                              // forall qualifier moved
556        } // if
557
558        checkQualifiers( type, q->type );
559        if ( (builtin == Zero || builtin == One) && q->type->qualifiers.val != 0 && error.length() == 0 ) {
560                SemanticWarning( yylloc, Warning::BadQualifiersZeroOne, Type::QualifiersNames[ilog2( q->type->qualifiers.val )], builtinTypeNames[builtin] );
561        } // if
562        addQualifiersToType( q->type, type );
563
564        delete q;
565        return this;
566} // addQualifiers
567
568static void addTypeToType( TypeData *&src, TypeData *&dst ) {
569        if ( src->forall && dst->kind == TypeData::Function ) {
570                if ( dst->forall ) {
571                        dst->forall->appendList( src->forall );
572                } else {
573                        dst->forall = src->forall;
574                } // if
575                src->forall = nullptr;
576        } // if
577        if ( dst->base ) {
578                addTypeToType( src, dst->base );
579        } else {
580                switch ( dst->kind ) {
581                  case TypeData::Unknown:
582                        src->qualifiers |= dst->qualifiers;
583                        dst = src;
584                        src = nullptr;
585                        break;
586                  case TypeData::Basic:
587                        dst->qualifiers |= src->qualifiers;
588                        if ( src->kind != TypeData::Unknown ) {
589                                assert( src->kind == TypeData::Basic );
590
591                                if ( dst->basictype == DeclarationNode::NoBasicType ) {
592                                        dst->basictype = src->basictype;
593                                } else if ( src->basictype != DeclarationNode::NoBasicType )
594                                        SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: " );
595
596                                if ( dst->complextype == DeclarationNode::NoComplexType ) {
597                                        dst->complextype = src->complextype;
598                                } else if ( src->complextype != DeclarationNode::NoComplexType )
599                                        SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: " );
600
601                                if ( dst->signedness == DeclarationNode::NoSignedness ) {
602                                        dst->signedness = src->signedness;
603                                } else if ( src->signedness != DeclarationNode::NoSignedness )
604                                        SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: " );
605
606                                if ( dst->length == DeclarationNode::NoLength ) {
607                                        dst->length = src->length;
608                                } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
609                                        dst->length = DeclarationNode::LongLong;
610                                } else if ( src->length != DeclarationNode::NoLength )
611                                        SemanticError( yylloc, src, string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: " );
612                        } // if
613                        break;
614                  default:
615                        switch ( src->kind ) {
616                          case TypeData::Aggregate:
617                          case TypeData::Enum:
618                                dst->base = new TypeData( TypeData::AggregateInst );
619                                dst->base->aggInst.aggregate = src;
620                                if ( src->kind == TypeData::Aggregate ) {
621                                        dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
622                                } // if
623                                dst->base->qualifiers |= src->qualifiers;
624                                src = nullptr;
625                                break;
626                          default:
627                                if ( dst->forall ) {
628                                        dst->forall->appendList( src->forall );
629                                } else {
630                                        dst->forall = src->forall;
631                                } // if
632                                src->forall = nullptr;
633                                dst->base = src;
634                                src = nullptr;
635                        } // switch
636                } // switch
637        } // if
638}
639
640DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
641        if ( o ) {
642                checkSpecifiers( o );
643                copySpecifiers( o );
644                if ( o->type ) {
645                        if ( ! type ) {
646                                if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
647                                        type = new TypeData( TypeData::AggregateInst );
648                                        type->aggInst.aggregate = o->type;
649                                        if ( o->type->kind == TypeData::Aggregate ) {
650                                                type->aggInst.hoistType = o->type->aggregate.body;
651                                                type->aggInst.params = maybeClone( o->type->aggregate.actuals );
652                                        } else {
653                                                type->aggInst.hoistType = o->type->enumeration.body;
654                                        } // if
655                                        type->qualifiers |= o->type->qualifiers;
656                                } else {
657                                        type = o->type;
658                                } // if
659                                o->type = nullptr;
660                        } else {
661                                addTypeToType( o->type, type );
662                        } // if
663                } // if
664                if ( o->bitfieldWidth ) {
665                        bitfieldWidth = o->bitfieldWidth;
666                } // if
667
668                // there may be typedefs chained onto the type
669                if ( o->get_next() ) {
670                        set_last( o->get_next()->clone() );
671                } // if
672        } // if
673        delete o;
674        return this;
675}
676
677DeclarationNode * DeclarationNode::addTypedef() {
678        TypeData * newtype = new TypeData( TypeData::Symbolic );
679        newtype->symbolic.params = nullptr;
680        newtype->symbolic.isTypedef = true;
681        newtype->symbolic.name = name ? new string( *name ) : nullptr;
682        newtype->base = type;
683        type = newtype;
684        return this;
685}
686
687DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
688        if ( variable.tyClass != NoTypeClass ) {
689                if ( variable.assertions ) {
690                        variable.assertions->appendList( assertions );
691                } else {
692                        variable.assertions = assertions;
693                } // if
694                return this;
695        } // if
696
697        assert( type );
698        switch ( type->kind ) {
699          case TypeData::Symbolic:
700                if ( type->symbolic.assertions ) {
701                        type->symbolic.assertions->appendList( assertions );
702                } else {
703                        type->symbolic.assertions = assertions;
704                } // if
705                break;
706          default:
707                assert( false );
708        } // switch
709
710        return this;
711}
712
713DeclarationNode * DeclarationNode::addName( string * newname ) {
714        assert( ! name );
715        name = newname;
716        return this;
717}
718
719DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
720        assert( ! asmName );
721        asmName = newname ? newname->asmName : nullptr;
722        return this->addQualifiers( newname );
723}
724
725DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
726        bitfieldWidth = size;
727        return this;
728}
729
730DeclarationNode * DeclarationNode::addVarArgs() {
731        assert( type );
732        hasEllipsis = true;
733        return this;
734}
735
736DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, ExpressionNode * withExprs ) {
737        assert( type );
738        assert( type->kind == TypeData::Function );
739        assert( ! type->function.body );
740        type->function.body = body;
741        type->function.withExprs = withExprs;
742        return this;
743}
744
745DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
746        assert( type );
747        assert( type->kind == TypeData::Function );
748        assert( ! type->function.oldDeclList );
749        type->function.oldDeclList = list;
750        return this;
751}
752
753DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
754        if ( type ) {
755                TypeData * prevBase = type;
756                TypeData * curBase = type->base;
757                while ( curBase != nullptr ) {
758                        prevBase = curBase;
759                        curBase = curBase->base;
760                } // while
761                prevBase->base = newType;
762        } else {
763                type = newType;
764        } // if
765        return this;
766}
767
768DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
769        if ( a ) {
770                for ( Attribute *attr: reverseIterate( a->attributes ) ) {
771                        attributes.push_front( attr );
772                } // for
773                a->attributes.clear();
774        } // if
775        return this;
776} // copyAttribute
777
778DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
779        if ( p ) {
780                assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
781                setBase( p->type );
782                p->type = nullptr;
783                copyAttribute( p );
784                delete p;
785        } // if
786        return this;
787}
788
789DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
790        if ( a ) {
791                assert( a->type->kind == TypeData::Array );
792                setBase( a->type );
793                a->type = nullptr;
794                copyAttribute( a );
795                delete a;
796        } // if
797        return this;
798}
799
800DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
801        if ( p ) {
802                assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
803                if ( type ) {
804                        switch ( type->kind ) {
805                          case TypeData::Aggregate:
806                          case TypeData::Enum:
807                                p->type->base = new TypeData( TypeData::AggregateInst );
808                                p->type->base->aggInst.aggregate = type;
809                                if ( type->kind == TypeData::Aggregate ) {
810                                        p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
811                                } // if
812                                p->type->base->qualifiers |= type->qualifiers;
813                                break;
814
815                          default:
816                                p->type->base = type;
817                        } // switch
818                        type = nullptr;
819                } // if
820                delete this;
821                return p;
822        } else {
823                return this;
824        } // if
825}
826
827static TypeData * findLast( TypeData * a ) {
828        assert( a );
829        TypeData * cur = a;
830        while ( cur->base ) {
831                cur = cur->base;
832        } // while
833        return cur;
834}
835
836DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
837  if ( ! a ) return this;
838        assert( a->type->kind == TypeData::Array );
839        TypeData * lastArray = findLast( a->type );
840        if ( type ) {
841                switch ( type->kind ) {
842                  case TypeData::Aggregate:
843                  case TypeData::Enum:
844                        lastArray->base = new TypeData( TypeData::AggregateInst );
845                        lastArray->base->aggInst.aggregate = type;
846                        if ( type->kind == TypeData::Aggregate ) {
847                                lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
848                        } // if
849                        lastArray->base->qualifiers |= type->qualifiers;
850                        break;
851                  default:
852                        lastArray->base = type;
853                } // switch
854                type = nullptr;
855        } // if
856        delete this;
857        return a;
858}
859
860DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
861        TypeData * ftype = new TypeData( TypeData::Function );
862        ftype->function.params = params;
863        setBase( ftype );
864        return this;
865}
866
867static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
868        if ( type ) {
869                if ( type->kind != TypeData::Function ) {
870                        type->base = addIdListToType( type->base, ids );
871                } else {
872                        type->function.idList = ids;
873                } // if
874                return type;
875        } else {
876                TypeData * newtype = new TypeData( TypeData::Function );
877                newtype->function.idList = ids;
878                return newtype;
879        } // if
880} // addIdListToType
881
882DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
883        type = addIdListToType( type, ids );
884        return this;
885}
886
887DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
888        initializer = init;
889        return this;
890}
891
892DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) {
893        assertf( variable.tyClass != NoTypeClass, "Called addTypeInitializer on something that isn't a type variable." );
894        variable.initializer = init;
895        return this;
896}
897
898DeclarationNode * DeclarationNode::cloneType( string * newName ) {
899        DeclarationNode * newnode = new DeclarationNode;
900        newnode->type = maybeClone( type );
901        newnode->copySpecifiers( this );
902        assert( newName );
903        newnode->name = newName;
904        return newnode;
905}
906
907DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
908        if ( ! o ) return nullptr;
909
910        o->copySpecifiers( this );
911        if ( type ) {
912                TypeData * srcType = type;
913
914                // search for the base type by scanning off pointers and array designators
915                while ( srcType->base ) {
916                        srcType = srcType->base;
917                } // while
918
919                TypeData * newType = srcType->clone();
920                if ( newType->kind == TypeData::AggregateInst ) {
921                        // don't duplicate members
922                        if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
923                                delete newType->aggInst.aggregate->enumeration.constants;
924                                newType->aggInst.aggregate->enumeration.constants = nullptr;
925                                newType->aggInst.aggregate->enumeration.body = false;
926                        } else {
927                                assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
928                                delete newType->aggInst.aggregate->aggregate.fields;
929                                newType->aggInst.aggregate->aggregate.fields = nullptr;
930                                newType->aggInst.aggregate->aggregate.body = false;
931                        } // if
932                        // don't hoist twice
933                        newType->aggInst.hoistType = false;
934                } // if
935
936                newType->forall = maybeClone( type->forall );
937                if ( ! o->type ) {
938                        o->type = newType;
939                } else {
940                        addTypeToType( newType, o->type );
941                        delete newType;
942                } // if
943        } // if
944        return o;
945}
946
947DeclarationNode * DeclarationNode::extractAggregate() const {
948        if ( type ) {
949                TypeData * ret = typeextractAggregate( type );
950                if ( ret ) {
951                        DeclarationNode * newnode = new DeclarationNode;
952                        newnode->type = ret;
953                        return newnode;
954                } // if
955        } // if
956        return nullptr;
957}
958
959void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
960        SemanticErrorException errors;
961        std::back_insert_iterator< std::list< Declaration * > > out( outputList );
962
963        for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
964                try {
965                        if ( DeclarationNode * extr = cur->extractAggregate() ) {
966                                // handle the case where a structure declaration is contained within an object or type declaration
967                                Declaration * decl = extr->build();
968                                if ( decl ) {
969                                        decl->location = cur->location;
970                                        * out++ = decl;
971                                } // if
972                                delete extr;
973                        } // if
974
975                        Declaration * decl = cur->build();
976                        if ( decl ) {
977                                decl->location = cur->location;
978                                * out++ = decl;
979                        } // if
980                } catch( SemanticErrorException &e ) {
981                        errors.append( e );
982                } // try
983        } // while
984
985        if ( ! errors.isEmpty() ) {
986                throw errors;
987        } // if
988} // buildList
989
990void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
991        SemanticErrorException errors;
992        std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
993
994        for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
995                try {
996                        Declaration * decl = cur->build();
997                        if ( decl ) {
998                                if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
999                                        dwt->location = cur->location;
1000                                        * out++ = dwt;
1001                                } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
1002                                        StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
1003                                        auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
1004                                        obj->location = cur->location;
1005                                        * out++ = obj;
1006                                        delete agg;
1007                                } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
1008                                        UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
1009                                        auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
1010                                        obj->location = cur->location;
1011                                        * out++ = obj;
1012                                } // if
1013                        } // if
1014                } catch( SemanticErrorException &e ) {
1015                        errors.append( e );
1016                } // try
1017        } // for
1018
1019        if ( ! errors.isEmpty() ) {
1020                throw errors;
1021        } // if
1022} // buildList
1023
1024void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
1025        SemanticErrorException errors;
1026        std::back_insert_iterator< std::list< Type * > > out( outputList );
1027        const DeclarationNode * cur = firstNode;
1028
1029        while ( cur ) {
1030                try {
1031                        * out++ = cur->buildType();
1032                } catch( SemanticErrorException &e ) {
1033                        errors.append( e );
1034                } // try
1035                cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
1036        } // while
1037
1038        if ( ! errors.isEmpty() ) {
1039                throw errors;
1040        } // if
1041} // buildTypeList
1042
1043Declaration * DeclarationNode::build() const {
1044        if ( ! error.empty() ) SemanticError( this, error + " in declaration of " );
1045
1046        if ( asmStmt ) {
1047                return new AsmDecl( strict_dynamic_cast<AsmStmt *>( asmStmt->build() ) );
1048        } // if
1049
1050        if ( variable.tyClass != NoTypeClass ) {
1051                // otype is internally converted to dtype + otype parameters
1052                static const TypeDecl::Kind kindMap[] = { TypeDecl::Dtype, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype };
1053                assertf( sizeof(kindMap)/sizeof(kindMap[0]) == NoTypeClass, "DeclarationNode::build: kindMap is out of sync." );
1054                assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
1055                TypeDecl * ret = new TypeDecl( *name, Type::StorageClasses(), nullptr, kindMap[ variable.tyClass ], variable.tyClass == Otype, variable.initializer ? variable.initializer->buildType() : nullptr );
1056                buildList( variable.assertions, ret->get_assertions() );
1057                return ret;
1058        } // if
1059
1060        if ( type ) {
1061                // Function specifiers can only appear on a function definition/declaration.
1062                //
1063                //    inline _Noreturn int f();                 // allowed
1064                //    inline _Noreturn int g( int i );  // allowed
1065                //    inline _Noreturn int i;                   // disallowed
1066                if ( type->kind != TypeData::Function && funcSpecs.any() ) {
1067                        SemanticError( this, "invalid function specifier for " );
1068                } // if
1069                bool isDelete = initializer && initializer->get_isDelete();
1070                Declaration * decl = buildDecl( type, name ? *name : string( "" ), storageClasses, maybeBuild< Expression >( bitfieldWidth ), funcSpecs, linkage, asmName, isDelete ? nullptr : maybeBuild< Initializer >(initializer), attributes )->set_extension( extension );
1071                if ( isDelete ) {
1072                        DeclarationWithType * dwt = strict_dynamic_cast<DeclarationWithType *>( decl );
1073                        dwt->isDeleted = true;
1074                }
1075                return decl;
1076        } // if
1077
1078        if ( assert.condition ) {
1079                return new StaticAssertDecl( maybeBuild< Expression >( assert.condition ), strict_dynamic_cast< ConstantExpr * >( maybeClone( assert.message ) ) );
1080        }
1081
1082        // SUE's cannot have function specifiers, either
1083        //
1084        //    inlne _Noreturn struct S { ... };         // disallowed
1085        //    inlne _Noreturn enum   E { ... };         // disallowed
1086        if ( funcSpecs.any() ) {
1087                SemanticError( this, "invalid function specifier for " );
1088        } // if
1089        assertf( name, "ObjectDecl must a have name\n" );
1090        return (new ObjectDecl( *name, storageClasses, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension );
1091}
1092
1093Type * DeclarationNode::buildType() const {
1094        assert( type );
1095
1096        if ( attr.expr ) {
1097                return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes );
1098        } else if ( attr.type ) {
1099                return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes );
1100        } // if
1101
1102        switch ( type->kind ) {
1103          case TypeData::Enum:
1104          case TypeData::Aggregate: {
1105                  ReferenceToType * ret = buildComAggInst( type, attributes, linkage );
1106                  buildList( type->aggregate.actuals, ret->get_parameters() );
1107                  return ret;
1108          }
1109          case TypeData::Symbolic: {
1110                  TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false, attributes );
1111                  buildList( type->symbolic.actuals, ret->get_parameters() );
1112                  return ret;
1113          }
1114          default:
1115                Type * simpletypes = typebuild( type );
1116                simpletypes->get_attributes() = attributes;             // copy because member is const
1117                return simpletypes;
1118        } // switch
1119}
1120
1121// Local Variables: //
1122// tab-width: 4 //
1123// mode: c++ //
1124// compile-command: "make install" //
1125// End: //
Note: See TracBrowser for help on using the repository browser.