source: src/Parser/DeclarationNode.cc @ f6f0cca3

new-envwith_gc
Last change on this file since f6f0cca3 was 42107b4, checked in by Aaron Moss <a3moss@…>, 6 years ago

Leftover cleanup from merge

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