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

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 3d5f2ef1 was c453ac4, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Change parser to build FunctionDecl withExpr rather than convert to a WithStmt

  • Property mode set to 100644
File size: 36.1 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// DeclarationNode.cc --
8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 12:34:05 2015
11// Last Modified By : 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, ExpressionNode * withExprs ) {
726 assert( type );
727 assert( type->kind == TypeData::Function );
728 assert( ! type->function.body );
729 type->function.body = body;
730 type->function.withExprs = withExprs;
731 return this;
732}
733
734DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
735 assert( type );
736 assert( type->kind == TypeData::Function );
737 assert( ! type->function.oldDeclList );
738 type->function.oldDeclList = list;
739 return this;
740}
741
742DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
743 if ( type ) {
744 TypeData * prevBase = type;
745 TypeData * curBase = type->base;
746 while ( curBase != nullptr ) {
747 prevBase = curBase;
748 curBase = curBase->base;
749 } // while
750 prevBase->base = newType;
751 } else {
752 type = newType;
753 } // if
754 return this;
755}
756
757DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
758 if ( a ) {
759 for ( Attribute *attr: reverseIterate( a->attributes ) ) {
760 attributes.push_front( attr );
761 } // for
762 a->attributes.clear();
763 } // if
764 return this;
765} // copyAttribute
766
767DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
768 if ( p ) {
769 assert( p->type->kind == TypeData::Pointer || TypeData::Reference );
770 setBase( p->type );
771 p->type = nullptr;
772 copyAttribute( p );
773 delete p;
774 } // if
775 return this;
776}
777
778DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
779 if ( a ) {
780 assert( a->type->kind == TypeData::Array );
781 setBase( a->type );
782 a->type = nullptr;
783 copyAttribute( a );
784 delete a;
785 } // if
786 return this;
787}
788
789DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
790 if ( p ) {
791 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
792 if ( type ) {
793 switch ( type->kind ) {
794 case TypeData::Aggregate:
795 case TypeData::Enum:
796 p->type->base = new TypeData( TypeData::AggregateInst );
797 p->type->base->aggInst.aggregate = type;
798 if ( type->kind == TypeData::Aggregate ) {
799 p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
800 } // if
801 p->type->base->qualifiers |= type->qualifiers;
802 break;
803
804 default:
805 p->type->base = type;
806 } // switch
807 type = nullptr;
808 } // if
809 delete this;
810 return p;
811 } else {
812 return this;
813 } // if
814}
815
816static TypeData * findLast( TypeData * a ) {
817 assert( a );
818 TypeData * cur = a;
819 while ( cur->base ) {
820 cur = cur->base;
821 } // while
822 return cur;
823}
824
825DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
826 if ( ! a ) return this;
827 assert( a->type->kind == TypeData::Array );
828 TypeData * lastArray = findLast( a->type );
829 if ( type ) {
830 switch ( type->kind ) {
831 case TypeData::Aggregate:
832 case TypeData::Enum:
833 lastArray->base = new TypeData( TypeData::AggregateInst );
834 lastArray->base->aggInst.aggregate = type;
835 if ( type->kind == TypeData::Aggregate ) {
836 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
837 } // if
838 lastArray->base->qualifiers |= type->qualifiers;
839 break;
840 default:
841 lastArray->base = type;
842 } // switch
843 type = nullptr;
844 } // if
845 delete this;
846 return a;
847}
848
849DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
850 TypeData * ftype = new TypeData( TypeData::Function );
851 ftype->function.params = params;
852 setBase( ftype );
853 return this;
854}
855
856static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
857 if ( type ) {
858 if ( type->kind != TypeData::Function ) {
859 type->base = addIdListToType( type->base, ids );
860 } else {
861 type->function.idList = ids;
862 } // if
863 return type;
864 } else {
865 TypeData * newtype = new TypeData( TypeData::Function );
866 newtype->function.idList = ids;
867 return newtype;
868 } // if
869} // addIdListToType
870
871DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
872 type = addIdListToType( type, ids );
873 return this;
874}
875
876DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
877 initializer = init;
878 return this;
879}
880
881DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) {
882 assertf( variable.tyClass != NoTypeClass, "Called addTypeInitializer on something that isn't a type variable." );
883 variable.initializer = init;
884 return this;
885}
886
887DeclarationNode * DeclarationNode::cloneType( string * newName ) {
888 DeclarationNode * newnode = new DeclarationNode;
889 newnode->type = maybeClone( type );
890 newnode->copySpecifiers( this );
891 assert( newName );
892 newnode->name = newName;
893 return newnode;
894}
895
896DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
897 if ( ! o ) return nullptr;
898
899 o->copySpecifiers( this );
900 if ( type ) {
901 TypeData * srcType = type;
902
903 // search for the base type by scanning off pointers and array designators
904 while ( srcType->base ) {
905 srcType = srcType->base;
906 } // while
907
908 TypeData * newType = srcType->clone();
909 if ( newType->kind == TypeData::AggregateInst ) {
910 // don't duplicate members
911 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
912 delete newType->aggInst.aggregate->enumeration.constants;
913 newType->aggInst.aggregate->enumeration.constants = nullptr;
914 } else {
915 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
916 delete newType->aggInst.aggregate->aggregate.fields;
917 newType->aggInst.aggregate->aggregate.fields = nullptr;
918 } // if
919 // don't hoist twice
920 newType->aggInst.hoistType = false;
921 } // if
922
923 newType->forall = maybeClone( type->forall );
924 if ( ! o->type ) {
925 o->type = newType;
926 } else {
927 addTypeToType( newType, o->type );
928 delete newType;
929 } // if
930 } // if
931 return o;
932}
933
934DeclarationNode * DeclarationNode::extractAggregate() const {
935 if ( type ) {
936 TypeData * ret = typeextractAggregate( type );
937 if ( ret ) {
938 DeclarationNode * newnode = new DeclarationNode;
939 newnode->type = ret;
940 return newnode;
941 } // if
942 } // if
943 return nullptr;
944}
945
946void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
947 SemanticError errors;
948 std::back_insert_iterator< std::list< Declaration * > > out( outputList );
949
950 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
951 try {
952 if ( DeclarationNode * extr = cur->extractAggregate() ) {
953 // handle the case where a structure declaration is contained within an object or type declaration
954 Declaration * decl = extr->build();
955 if ( decl ) {
956 decl->location = cur->location;
957 * out++ = decl;
958 } // if
959 delete extr;
960 } // if
961
962 Declaration * decl = cur->build();
963 if ( decl ) {
964 decl->location = cur->location;
965 * out++ = decl;
966 } // if
967 } catch( SemanticError &e ) {
968 e.set_location( cur->location );
969 errors.append( e );
970 } // try
971 } // while
972
973 if ( ! errors.isEmpty() ) {
974 throw errors;
975 } // if
976} // buildList
977
978void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
979 SemanticError errors;
980 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
981
982 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
983 try {
984 Declaration * decl = cur->build();
985 if ( decl ) {
986 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
987 dwt->location = cur->location;
988 * out++ = dwt;
989 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
990 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
991 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
992 obj->location = cur->location;
993 * out++ = obj;
994 delete agg;
995 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
996 UnionInstType * inst = new UnionInstType( Type::Qualifiers(), agg->get_name() );
997 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
998 obj->location = cur->location;
999 * out++ = obj;
1000 } // if
1001 } // if
1002 } catch( SemanticError &e ) {
1003 e.set_location( cur->location );
1004 errors.append( e );
1005 } // try
1006 } // for
1007
1008 if ( ! errors.isEmpty() ) {
1009 throw errors;
1010 } // if
1011} // buildList
1012
1013void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
1014 SemanticError errors;
1015 std::back_insert_iterator< std::list< Type * > > out( outputList );
1016 const DeclarationNode * cur = firstNode;
1017
1018 while ( cur ) {
1019 try {
1020 * out++ = cur->buildType();
1021 } catch( SemanticError &e ) {
1022 e.set_location( cur->location );
1023 errors.append( e );
1024 } // try
1025 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
1026 } // while
1027
1028 if ( ! errors.isEmpty() ) {
1029 throw errors;
1030 } // if
1031} // buildTypeList
1032
1033Declaration * DeclarationNode::build() const {
1034 if ( ! error.empty() ) throw SemanticError( error + " in declaration of ", this );
1035
1036 if ( asmStmt ) {
1037 return new AsmDecl( strict_dynamic_cast<AsmStmt *>( asmStmt->build() ) );
1038 } // if
1039
1040 if ( variable.tyClass != NoTypeClass ) {
1041 // otype is internally converted to dtype + otype parameters
1042 static const TypeDecl::Kind kindMap[] = { TypeDecl::Dtype, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype };
1043 assertf( sizeof(kindMap)/sizeof(kindMap[0]) == NoTypeClass, "DeclarationNode::build: kindMap is out of sync." );
1044 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
1045 TypeDecl * ret = new TypeDecl( *name, Type::StorageClasses(), nullptr, kindMap[ variable.tyClass ], variable.tyClass == Otype, variable.initializer ? variable.initializer->buildType() : nullptr );
1046 buildList( variable.assertions, ret->get_assertions() );
1047 return ret;
1048 } // if
1049
1050 if ( type ) {
1051 // Function specifiers can only appear on a function definition/declaration.
1052 //
1053 // inline _Noreturn int f(); // allowed
1054 // inline _Noreturn int g( int i ); // allowed
1055 // inline _Noreturn int i; // disallowed
1056 if ( type->kind != TypeData::Function && funcSpecs.any() ) {
1057 throw SemanticError( "invalid function specifier for ", this );
1058 } // if
1059 return buildDecl( type, name ? *name : string( "" ), storageClasses, maybeBuild< Expression >( bitfieldWidth ), funcSpecs, linkage, asmName, maybeBuild< Initializer >(initializer), attributes )->set_extension( extension );
1060 } // if
1061
1062 // SUE's cannot have function specifiers, either
1063 //
1064 // inlne _Noreturn struct S { ... }; // disallowed
1065 // inlne _Noreturn enum E { ... }; // disallowed
1066 if ( funcSpecs.any() ) {
1067 throw SemanticError( "invalid function specifier for ", this );
1068 } // if
1069 assertf( name, "ObjectDecl must a have name\n" );
1070 return (new ObjectDecl( *name, storageClasses, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension );
1071}
1072
1073Type * DeclarationNode::buildType() const {
1074 assert( type );
1075
1076 if ( attr.expr ) {
1077 return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes );
1078 } else if ( attr.type ) {
1079 return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes );
1080 } // if
1081
1082 switch ( type->kind ) {
1083 case TypeData::Enum:
1084 case TypeData::Aggregate: {
1085 ReferenceToType * ret = buildComAggInst( type, attributes, linkage );
1086 buildList( type->aggregate.actuals, ret->get_parameters() );
1087 return ret;
1088 }
1089 case TypeData::Symbolic: {
1090 TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false, attributes );
1091 buildList( type->symbolic.actuals, ret->get_parameters() );
1092 return ret;
1093 }
1094 default:
1095 Type * simpletypes = typebuild( type );
1096 simpletypes->get_attributes() = attributes; // copy because member is const
1097 return simpletypes;
1098 } // switch
1099}
1100
1101// Local Variables: //
1102// tab-width: 4 //
1103// mode: c++ //
1104// compile-command: "make install" //
1105// End: //
Note: See TracBrowser for help on using the repository browser.