source: src/Parser/DeclarationNode.cc @ bdad6eb7

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

Build AST nodes for '&&' reference correctly

  • Property mode set to 100644
File size: 36.4 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 : Mon Nov 20 09:21:52 2017
13// Update Count     : 1031
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", "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
75DeclarationNode::~DeclarationNode() {
76//      delete attr.name;
77        delete attr.expr;
78        delete attr.type;
79
80//      delete variable.name;
81        delete variable.assertions;
82        delete variable.initializer;
83
84        delete type;
85        delete bitfieldWidth;
86
87        delete asmStmt;
88        // asmName, no delete, passed to next stage
89        delete initializer;
90}
91
92DeclarationNode * DeclarationNode::clone() const {
93        DeclarationNode * newnode = new DeclarationNode;
94        newnode->set_next( maybeClone( get_next() ) );
95        newnode->name = name ? new string( *name ) : nullptr;
96
97        newnode->type = maybeClone( type );
98        newnode->storageClasses = storageClasses;
99        newnode->funcSpecs = funcSpecs;
100        newnode->bitfieldWidth = maybeClone( bitfieldWidth );
101        newnode->enumeratorValue.reset( maybeClone( enumeratorValue.get() ) );
102        newnode->hasEllipsis = hasEllipsis;
103        newnode->linkage = linkage;
104        newnode->asmName = maybeClone( asmName );
105        cloneAll( attributes, newnode->attributes );
106        newnode->initializer = maybeClone( initializer );
107        newnode->extension = extension;
108        newnode->asmStmt = maybeClone( asmStmt );
109        newnode->error = error;
110
111//      newnode->variable.name = variable.name ? new string( *variable.name ) : nullptr;
112        newnode->variable.tyClass = variable.tyClass;
113        newnode->variable.assertions = maybeClone( variable.assertions );
114        newnode->variable.initializer = maybeClone( variable.initializer );
115
116//      newnode->attr.name = attr.name ? new string( *attr.name ) : nullptr;
117        newnode->attr.expr = maybeClone( attr.expr );
118        newnode->attr.type = maybeClone( attr.type );
119        return newnode;
120} // DeclarationNode::clone
121
122bool DeclarationNode::get_hasEllipsis() const {
123        return hasEllipsis;
124}
125
126void DeclarationNode::print( std::ostream &os, int indent ) const {
127        os << string( indent, ' ' );
128        if ( name ) {
129                os << *name << ": ";
130        } else {
131                os << "unnamed: ";
132        } // if
133
134        if ( linkage != LinkageSpec::Cforall ) {
135                os << LinkageSpec::linkageName( linkage ) << " ";
136        } // if
137
138        storageClasses.print( os );
139        funcSpecs.print( os );
140
141        if ( type ) {
142                type->print( os, indent );
143        } else {
144                os << "untyped entity ";
145        } // if
146
147        if ( bitfieldWidth ) {
148                os << endl << string( indent + 2, ' ' ) << "with bitfield width ";
149                bitfieldWidth->printOneLine( os );
150        } // if
151
152        if ( initializer ) {
153                os << endl << string( indent + 2, ' ' ) << "with initializer ";
154                initializer->printOneLine( os );
155                os << " maybe constructed? " << initializer->get_maybeConstructed();
156
157        } // if
158
159        os << endl;
160}
161
162void DeclarationNode::printList( std::ostream &os, int indent ) const {
163        ParseNode::printList( os, indent );
164        if ( hasEllipsis ) {
165                os << string( indent, ' ' )  << "and a variable number of other arguments" << endl;
166        } // if
167}
168
169DeclarationNode * DeclarationNode::newFunction( string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body, bool newStyle ) {
170        DeclarationNode * newnode = new DeclarationNode;
171        newnode->name = name;
172        newnode->type = new TypeData( TypeData::Function );
173        newnode->type->function.params = param;
174        newnode->type->function.newStyle = newStyle;
175        newnode->type->function.body = body;
176
177        // ignore unnamed routine declarations: void p( int (*)(int) );
178        if ( newnode->name ) {
179                typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID );
180        } // if
181
182        if ( ret ) {
183                newnode->type->base = ret->type;
184                ret->type = nullptr;
185                delete ret;
186        } // if
187
188        return newnode;
189} // DeclarationNode::newFunction
190
191
192DeclarationNode * DeclarationNode::newStorageClass( Type::StorageClasses sc ) {
193        DeclarationNode * newnode = new DeclarationNode;
194        newnode->storageClasses = sc;
195        return newnode;
196} // DeclarationNode::newStorageClass
197
198DeclarationNode * DeclarationNode::newFuncSpecifier( Type::FuncSpecifiers fs ) {
199        DeclarationNode * newnode = new DeclarationNode;
200        newnode->funcSpecs = fs;
201        return newnode;
202} // DeclarationNode::newFuncSpecifier
203
204DeclarationNode * DeclarationNode::newTypeQualifier( Type::Qualifiers tq ) {
205        DeclarationNode * newnode = new DeclarationNode;
206        newnode->type = new TypeData();
207        newnode->type->qualifiers = tq;
208        return newnode;
209} // DeclarationNode::newQualifier
210
211DeclarationNode * DeclarationNode::newBasicType( BasicType bt ) {
212        DeclarationNode * newnode = new DeclarationNode;
213        newnode->type = new TypeData( TypeData::Basic );
214        newnode->type->basictype = bt;
215        return newnode;
216} // DeclarationNode::newBasicType
217
218DeclarationNode * DeclarationNode::newComplexType( ComplexType ct ) {
219        DeclarationNode * newnode = new DeclarationNode;
220        newnode->type = new TypeData( TypeData::Basic );
221        newnode->type->complextype = ct;
222        return newnode;
223} // DeclarationNode::newComplexType
224
225DeclarationNode * DeclarationNode::newSignedNess( Signedness sn ) {
226        DeclarationNode * newnode = new DeclarationNode;
227        newnode->type = new TypeData( TypeData::Basic );
228        newnode->type->signedness = sn;
229        return newnode;
230} // DeclarationNode::newSignedNess
231
232DeclarationNode * DeclarationNode::newLength( Length lnth ) {
233        DeclarationNode * newnode = new DeclarationNode;
234        newnode->type = new TypeData( TypeData::Basic );
235        newnode->type->length = lnth;
236        return newnode;
237} // DeclarationNode::newLength
238
239DeclarationNode * DeclarationNode::newForall( DeclarationNode * forall ) {
240        DeclarationNode * newnode = new DeclarationNode;
241        newnode->type = new TypeData( TypeData::Unknown );
242        newnode->type->forall = forall;
243        return newnode;
244} // DeclarationNode::newForall
245
246DeclarationNode * DeclarationNode::newFromTypedef( string * name ) {
247        DeclarationNode * newnode = new DeclarationNode;
248        newnode->type = new TypeData( TypeData::SymbolicInst );
249        newnode->type->symbolic.name = name;
250        newnode->type->symbolic.isTypedef = true;
251        newnode->type->symbolic.params = nullptr;
252        return newnode;
253} // DeclarationNode::newFromTypedef
254
255DeclarationNode * DeclarationNode::newAggregate( Aggregate kind, const string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body ) {
256        assert( name );
257        DeclarationNode * newnode = new DeclarationNode;
258        newnode->type = new TypeData( TypeData::Aggregate );
259        newnode->type->aggregate.kind = kind;
260        newnode->type->aggregate.name = name;
261        newnode->type->aggregate.actuals = actuals;
262        newnode->type->aggregate.fields = fields;
263        newnode->type->aggregate.body = body;
264        newnode->type->aggregate.tagged = false;
265        newnode->type->aggregate.parent = nullptr;
266        return newnode;
267} // DeclarationNode::newAggregate
268
269DeclarationNode * DeclarationNode::newEnum( string * name, DeclarationNode * constants, bool body ) {
270        assert( name );
271        DeclarationNode * newnode = new DeclarationNode;
272        newnode->type = new TypeData( TypeData::Enum );
273        newnode->type->enumeration.name = name;
274        newnode->type->enumeration.constants = constants;
275        newnode->type->enumeration.body = body;
276        return newnode;
277} // DeclarationNode::newEnum
278
279DeclarationNode * DeclarationNode::newEnumConstant( string * name, ExpressionNode * constant ) {
280        DeclarationNode * newnode = new DeclarationNode;
281        newnode->name = name;
282        newnode->enumeratorValue.reset( constant );
283        typedefTable.addToEnclosingScope( *newnode->name, TypedefTable::ID );
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
442void appendError( string & dst, const string & src ) {
443        if ( src.empty() ) return;
444        if ( dst.empty() ) { dst = src; return; }
445        dst += ", " + src;
446} // appendError
447
448void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
449        const Type::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
450
451        if ( (qsrc & qdst).any() ) {                                            // duplicates ?
452                for ( unsigned int i = 0; i < Type::NumTypeQualifier; i += 1 ) { // find duplicates
453                        if ( qsrc[i] && qdst[i] ) {
454                                appendError( error, string( "duplicate " ) + Type::QualifiersNames[i] );
455                        } // if
456                } // for
457        } // for
458} // DeclarationNode::checkQualifiers
459
460void DeclarationNode::checkSpecifiers( DeclarationNode * src ) {
461        if ( (funcSpecs & src->funcSpecs).any() ) {                     // duplicates ?
462                for ( unsigned int i = 0; i < Type::NumFuncSpecifier; i += 1 ) { // find duplicates
463                        if ( funcSpecs[i] && src->funcSpecs[i] ) {
464                                appendError( error, string( "duplicate " ) + Type::FuncSpecifiersNames[i] );
465                        } // if
466                } // for
467        } // if
468
469        if ( storageClasses.any() && src->storageClasses.any() ) { // any reason to check ?
470                if ( (storageClasses & src->storageClasses ).any() ) { // duplicates ?
471                        for ( unsigned int i = 0; i < Type::NumStorageClass; i += 1 ) { // find duplicates
472                                if ( storageClasses[i] && src->storageClasses[i] ) {
473                                        appendError( error, string( "duplicate " ) + Type::StorageClassesNames[i] );
474                                } // if
475                        } // for
476                        // src is the new item being added and has a single bit
477                } else if ( ! src->storageClasses.is_threadlocal ) { // conflict ?
478                        appendError( error, string( "conflicting " ) + Type::StorageClassesNames[storageClasses.ffs()] +
479                                                 " & " + Type::StorageClassesNames[src->storageClasses.ffs()] );
480                        src->storageClasses.reset();                            // FIX to preserve invariant of one basic storage specifier
481                } // if
482        } // if
483
484        appendError( error, src->error );
485} // DeclarationNode::checkSpecifiers
486
487DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q ) {
488        funcSpecs |= q->funcSpecs;
489        storageClasses |= q->storageClasses;
490
491        for ( Attribute *attr: reverseIterate( q->attributes ) ) {
492                attributes.push_front( attr->clone() );
493        } // for
494        return this;
495} // DeclarationNode::copySpecifiers
496
497static void addQualifiersToType( TypeData *&src, TypeData * dst ) {
498        if ( src->forall && dst->kind == TypeData::Function ) {
499                if ( dst->forall ) {
500                        dst->forall->appendList( src->forall );
501                } else {
502                        dst->forall = src->forall;
503                } // if
504                src->forall = nullptr;
505        } // if
506        if ( dst->base ) {
507                addQualifiersToType( src, dst->base );
508        } else if ( dst->kind == TypeData::Function ) {
509                dst->base = src;
510                src = nullptr;
511        } else {
512                dst->qualifiers |= src->qualifiers;
513        } // if
514} // addQualifiersToType
515
516DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
517        if ( ! q ) { delete q; return this; }                           // empty qualifier
518
519        checkSpecifiers( q );
520        copySpecifiers( q );
521
522        if ( ! q->type ) { delete q; return this; }
523
524        if ( ! type ) {
525                type = q->type;                                                                 // reuse structure
526                q->type = nullptr;
527                delete q;
528                return this;
529        } // if
530
531        if ( q->type->forall ) {                                                        // forall qualifier ?
532                if ( type->forall ) {                                                   // polymorphic routine ?
533                        type->forall->appendList( q->type->forall ); // augment forall qualifier
534                } else {
535                        if ( type->kind == TypeData::Aggregate ) {      // struct/union ?
536                                if ( type->aggregate.params ) {                 // polymorphic ?
537                                        type->aggregate.params->appendList( q->type->forall ); // augment forall qualifier
538                                } else {                                                                // not polymorphic
539                                        type->aggregate.params = q->type->forall; // make polymorphic type
540                                        // change implicit typedef from TYPEDEFname to TYPEGENname
541                                        typedefTable.changeKind( *type->aggregate.name, TypedefTable::TG );
542                                } // if
543                        } else {                                                                        // not polymorphic
544                                type->forall = q->type->forall;                 // make polymorphic routine
545                        } // if
546                } // if
547                q->type->forall = nullptr;                                              // forall qualifier moved
548        } // if
549
550        checkQualifiers( type, q->type );
551        addQualifiersToType( q->type, type );
552
553        delete q;
554        return this;
555} // addQualifiers
556
557static void addTypeToType( TypeData *&src, TypeData *&dst ) {
558        if ( src->forall && dst->kind == TypeData::Function ) {
559                if ( dst->forall ) {
560                        dst->forall->appendList( src->forall );
561                } else {
562                        dst->forall = src->forall;
563                } // if
564                src->forall = nullptr;
565        } // if
566        if ( dst->base ) {
567                addTypeToType( src, dst->base );
568        } else {
569                switch ( dst->kind ) {
570                  case TypeData::Unknown:
571                        src->qualifiers |= dst->qualifiers;
572                        dst = src;
573                        src = nullptr;
574                        break;
575                  case TypeData::Basic:
576                        dst->qualifiers |= src->qualifiers;
577                        if ( src->kind != TypeData::Unknown ) {
578                                assert( src->kind == TypeData::Basic );
579
580                                if ( dst->basictype == DeclarationNode::NoBasicType ) {
581                                        dst->basictype = src->basictype;
582                                } else if ( src->basictype != DeclarationNode::NoBasicType )
583                                        throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: ", src );
584
585                                if ( dst->complextype == DeclarationNode::NoComplexType ) {
586                                        dst->complextype = src->complextype;
587                                } else if ( src->complextype != DeclarationNode::NoComplexType )
588                                        throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: ", src );
589
590                                if ( dst->signedness == DeclarationNode::NoSignedness ) {
591                                        dst->signedness = src->signedness;
592                                } else if ( src->signedness != DeclarationNode::NoSignedness )
593                                        throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: ", src );
594
595                                if ( dst->length == DeclarationNode::NoLength ) {
596                                        dst->length = src->length;
597                                } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
598                                        dst->length = DeclarationNode::LongLong;
599                                } else if ( src->length != DeclarationNode::NoLength )
600                                        throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: ", src );
601                        } // if
602                        break;
603                  default:
604                        switch ( src->kind ) {
605                          case TypeData::Aggregate:
606                          case TypeData::Enum:
607                                dst->base = new TypeData( TypeData::AggregateInst );
608                                dst->base->aggInst.aggregate = src;
609                                if ( src->kind == TypeData::Aggregate ) {
610                                        dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
611                                } // if
612                                dst->base->qualifiers |= src->qualifiers;
613                                src = nullptr;
614                                break;
615                          default:
616                                if ( dst->forall ) {
617                                        dst->forall->appendList( src->forall );
618                                } else {
619                                        dst->forall = src->forall;
620                                } // if
621                                src->forall = nullptr;
622                                dst->base = src;
623                                src = nullptr;
624                        } // switch
625                } // switch
626        } // if
627}
628
629DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
630        if ( o ) {
631                checkSpecifiers( o );
632                copySpecifiers( o );
633                if ( o->type ) {
634                        if ( ! type ) {
635                                if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
636                                        type = new TypeData( TypeData::AggregateInst );
637                                        type->aggInst.aggregate = o->type;
638                                        if ( o->type->kind == TypeData::Aggregate ) {
639                                                type->aggInst.hoistType = o->type->aggregate.body;
640                                                type->aggInst.params = maybeClone( o->type->aggregate.actuals );
641                                        } else {
642                                                type->aggInst.hoistType = o->type->enumeration.body;
643                                        } // if
644                                        type->qualifiers |= o->type->qualifiers;
645                                } else {
646                                        type = o->type;
647                                } // if
648                                o->type = nullptr;
649                        } else {
650                                addTypeToType( o->type, type );
651                        } // if
652                } // if
653                if ( o->bitfieldWidth ) {
654                        bitfieldWidth = o->bitfieldWidth;
655                } // if
656
657                // there may be typedefs chained onto the type
658                if ( o->get_next() ) {
659                        set_last( o->get_next()->clone() );
660                } // if
661        } // if
662        delete o;
663        return this;
664}
665
666DeclarationNode * DeclarationNode::addTypedef() {
667        TypeData * newtype = new TypeData( TypeData::Symbolic );
668        newtype->symbolic.params = nullptr;
669        newtype->symbolic.isTypedef = true;
670        newtype->symbolic.name = name ? new string( *name ) : nullptr;
671        newtype->base = type;
672        type = newtype;
673        return this;
674}
675
676DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
677        if ( variable.tyClass != NoTypeClass ) {
678                if ( variable.assertions ) {
679                        variable.assertions->appendList( assertions );
680                } else {
681                        variable.assertions = assertions;
682                } // if
683                return this;
684        } // if
685
686        assert( type );
687        switch ( type->kind ) {
688          case TypeData::Symbolic:
689                if ( type->symbolic.assertions ) {
690                        type->symbolic.assertions->appendList( assertions );
691                } else {
692                        type->symbolic.assertions = assertions;
693                } // if
694                break;
695          default:
696                assert( false );
697        } // switch
698
699        return this;
700}
701
702DeclarationNode * DeclarationNode::addName( string * newname ) {
703        assert( ! name );
704        name = newname;
705        return this;
706}
707
708DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
709        assert( ! asmName );
710        asmName = newname ? newname->asmName : nullptr;
711        return this->addQualifiers( newname );
712}
713
714DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
715        bitfieldWidth = size;
716        return this;
717}
718
719DeclarationNode * DeclarationNode::addVarArgs() {
720        assert( type );
721        hasEllipsis = true;
722        return this;
723}
724
725DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, StatementNode * with ) {
726        assert( type );
727        assert( type->kind == TypeData::Function );
728        assert( ! type->function.body );
729        if ( with ) {
730                // convert
731                //  void f(S s) with (s) { x = 0; }
732                // to
733                //  void f(S s) { with(s) { x = 0; } }
734                WithStmt * withStmt = strict_dynamic_cast< WithStmt * >( with->build() );
735                withStmt->stmt = body->build();
736                delete body;
737                delete with;
738                body = new StatementNode( new CompoundStmt( { withStmt } ) );
739        }
740        type->function.body = body;
741        return this;
742}
743
744DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
745        assert( type );
746        assert( type->kind == TypeData::Function );
747        assert( ! type->function.oldDeclList );
748        type->function.oldDeclList = list;
749        return this;
750}
751
752DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
753        if ( type ) {
754                TypeData * prevBase = type;
755                TypeData * curBase = type->base;
756                while ( curBase != nullptr ) {
757                        prevBase = curBase;
758                        curBase = curBase->base;
759                } // while
760                prevBase->base = newType;
761        } else {
762                type = newType;
763        } // if
764        return this;
765}
766
767DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
768        if ( a ) {
769                for ( Attribute *attr: reverseIterate( a->attributes ) ) {
770                        attributes.push_front( attr );
771                } // for
772                a->attributes.clear();
773        } // if
774        return this;
775} // copyAttribute
776
777DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
778        if ( p ) {
779                assert( p->type->kind == TypeData::Pointer || TypeData::Reference );
780                setBase( p->type );
781                p->type = nullptr;
782                copyAttribute( p );
783                delete p;
784        } // if
785        return this;
786}
787
788DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
789        if ( a ) {
790                assert( a->type->kind == TypeData::Array );
791                setBase( a->type );
792                a->type = nullptr;
793                copyAttribute( a );
794                delete a;
795        } // if
796        return this;
797}
798
799DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
800        if ( p ) {
801                assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
802                if ( type ) {
803                        switch ( type->kind ) {
804                          case TypeData::Aggregate:
805                          case TypeData::Enum:
806                                p->type->base = new TypeData( TypeData::AggregateInst );
807                                p->type->base->aggInst.aggregate = type;
808                                if ( type->kind == TypeData::Aggregate ) {
809                                        p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
810                                } // if
811                                p->type->base->qualifiers |= type->qualifiers;
812                                break;
813
814                          default:
815                                p->type->base = type;
816                        } // switch
817                        type = nullptr;
818                } // if
819                delete this;
820                return p;
821        } else {
822                return this;
823        } // if
824}
825
826static TypeData * findLast( TypeData * a ) {
827        assert( a );
828        TypeData * cur = a;
829        while ( cur->base ) {
830                cur = cur->base;
831        } // while
832        return cur;
833}
834
835DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
836  if ( ! a ) return this;
837        assert( a->type->kind == TypeData::Array );
838        TypeData * lastArray = findLast( a->type );
839        if ( type ) {
840                switch ( type->kind ) {
841                  case TypeData::Aggregate:
842                  case TypeData::Enum:
843                        lastArray->base = new TypeData( TypeData::AggregateInst );
844                        lastArray->base->aggInst.aggregate = type;
845                        if ( type->kind == TypeData::Aggregate ) {
846                                lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
847                        } // if
848                        lastArray->base->qualifiers |= type->qualifiers;
849                        break;
850                  default:
851                        lastArray->base = type;
852                } // switch
853                type = nullptr;
854        } // if
855        delete this;
856        return a;
857}
858
859DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
860        TypeData * ftype = new TypeData( TypeData::Function );
861        ftype->function.params = params;
862        setBase( ftype );
863        return this;
864}
865
866static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
867        if ( type ) {
868                if ( type->kind != TypeData::Function ) {
869                        type->base = addIdListToType( type->base, ids );
870                } else {
871                        type->function.idList = ids;
872                } // if
873                return type;
874        } else {
875                TypeData * newtype = new TypeData( TypeData::Function );
876                newtype->function.idList = ids;
877                return newtype;
878        } // if
879} // addIdListToType
880
881DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
882        type = addIdListToType( type, ids );
883        return this;
884}
885
886DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
887        initializer = init;
888        return this;
889}
890
891DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) {
892        assertf( variable.tyClass != NoTypeClass, "Called addTypeInitializer on something that isn't a type variable." );
893        variable.initializer = init;
894        return this;
895}
896
897DeclarationNode * DeclarationNode::cloneType( string * newName ) {
898        DeclarationNode * newnode = new DeclarationNode;
899        newnode->type = maybeClone( type );
900        newnode->copySpecifiers( this );
901        assert( newName );
902        newnode->name = newName;
903        return newnode;
904}
905
906DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
907        if ( ! o ) return nullptr;
908
909        o->copySpecifiers( this );
910        if ( type ) {
911                TypeData * srcType = type;
912
913                // search for the base type by scanning off pointers and array designators
914                while ( srcType->base ) {
915                        srcType = srcType->base;
916                } // while
917
918                TypeData * newType = srcType->clone();
919                if ( newType->kind == TypeData::AggregateInst ) {
920                        // don't duplicate members
921                        if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
922                                delete newType->aggInst.aggregate->enumeration.constants;
923                                newType->aggInst.aggregate->enumeration.constants = nullptr;
924                        } else {
925                                assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
926                                delete newType->aggInst.aggregate->aggregate.fields;
927                                newType->aggInst.aggregate->aggregate.fields = nullptr;
928                        } // if
929                        // don't hoist twice
930                        newType->aggInst.hoistType = false;
931                } // if
932
933                newType->forall = maybeClone( type->forall );
934                if ( ! o->type ) {
935                        o->type = newType;
936                } else {
937                        addTypeToType( newType, o->type );
938                        delete newType;
939                } // if
940        } // if
941        return o;
942}
943
944DeclarationNode * DeclarationNode::extractAggregate() const {
945        if ( type ) {
946                TypeData * ret = typeextractAggregate( type );
947                if ( ret ) {
948                        DeclarationNode * newnode = new DeclarationNode;
949                        newnode->type = ret;
950                        return newnode;
951                } // if
952        } // if
953        return nullptr;
954}
955
956void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
957        SemanticError errors;
958        std::back_insert_iterator< std::list< Declaration * > > out( outputList );
959
960        for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
961                try {
962                        if ( DeclarationNode * extr = cur->extractAggregate() ) {
963                                // handle the case where a structure declaration is contained within an object or type declaration
964                                Declaration * decl = extr->build();
965                                if ( decl ) {
966                                        decl->location = cur->location;
967                                        * out++ = decl;
968                                } // if
969                                delete extr;
970                        } // if
971
972                        Declaration * decl = cur->build();
973                        if ( decl ) {
974                                decl->location = cur->location;
975                                * out++ = decl;
976                        } // if
977                } catch( SemanticError &e ) {
978                        e.set_location( cur->location );
979                        errors.append( e );
980                } // try
981        } // while
982
983        if ( ! errors.isEmpty() ) {
984                throw errors;
985        } // if
986} // buildList
987
988void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
989        SemanticError errors;
990        std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
991
992        for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
993                try {
994                        Declaration * decl = cur->build();
995                        if ( decl ) {
996                                if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
997                                        dwt->location = cur->location;
998                                        * out++ = dwt;
999                                } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
1000                                        StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
1001                                        auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
1002                                        obj->location = cur->location;
1003                                        * out++ = obj;
1004                                        delete agg;
1005                                } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
1006                                        UnionInstType * inst = new UnionInstType( 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                                } // if
1011                        } // if
1012                } catch( SemanticError &e ) {
1013                        e.set_location( cur->location );
1014                        errors.append( e );
1015                } // try
1016        } // for
1017
1018        if ( ! errors.isEmpty() ) {
1019                throw errors;
1020        } // if
1021} // buildList
1022
1023void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
1024        SemanticError errors;
1025        std::back_insert_iterator< std::list< Type * > > out( outputList );
1026        const DeclarationNode * cur = firstNode;
1027
1028        while ( cur ) {
1029                try {
1030                        * out++ = cur->buildType();
1031                } catch( SemanticError &e ) {
1032                        e.set_location( cur->location );
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() ) throw SemanticError( error + " in declaration of ", this );
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                        throw SemanticError( "invalid function specifier for ", this );
1068                } // if
1069                return buildDecl( type, name ? *name : string( "" ), storageClasses, maybeBuild< Expression >( bitfieldWidth ), funcSpecs, linkage, asmName, maybeBuild< Initializer >(initializer), attributes )->set_extension( extension );
1070        } // if
1071
1072        // SUE's cannot have function specifiers, either
1073        //
1074        //    inlne _Noreturn struct S { ... };         // disallowed
1075        //    inlne _Noreturn enum   E { ... };         // disallowed
1076        if ( funcSpecs.any() ) {
1077                throw SemanticError( "invalid function specifier for ", this );
1078        } // if
1079        assertf( name, "ObjectDecl must a have name\n" );
1080        return (new ObjectDecl( *name, storageClasses, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension );
1081}
1082
1083Type * DeclarationNode::buildType() const {
1084        assert( type );
1085
1086        if ( attr.expr ) {
1087                return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes );
1088        } else if ( attr.type ) {
1089                return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes );
1090        } // if
1091
1092        switch ( type->kind ) {
1093          case TypeData::Enum:
1094          case TypeData::Aggregate: {
1095                  ReferenceToType * ret = buildComAggInst( type, attributes, linkage );
1096                  buildList( type->aggregate.actuals, ret->get_parameters() );
1097                  return ret;
1098          }
1099          case TypeData::Symbolic: {
1100                  TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false, attributes );
1101                  buildList( type->symbolic.actuals, ret->get_parameters() );
1102                  return ret;
1103          }
1104          default:
1105                Type * simpletypes = typebuild( type );
1106                simpletypes->get_attributes() = attributes;             // copy because member is const
1107                return simpletypes;
1108        } // switch
1109}
1110
1111// Local Variables: //
1112// tab-width: 4 //
1113// mode: c++ //
1114// compile-command: "make install" //
1115// End: //
Note: See TracBrowser for help on using the repository browser.