source: src/Parser/DeclarationNode.cc@ 6d2f993

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 6d2f993 was 5fcba14, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Implement function declaration's with clause

  • Property mode set to 100644
File size: 36.2 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 ( qualifiers ) {
346 return newnode->addQualifiers( qualifiers );
347 } else {
348 return newnode;
349 } // if
350} // DeclarationNode::newPointer
351
352DeclarationNode * DeclarationNode::newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic ) {
353 DeclarationNode * newnode = new DeclarationNode;
354 newnode->type = new TypeData( TypeData::Array );
355 newnode->type->array.dimension = size;
356 newnode->type->array.isStatic = isStatic;
357 if ( newnode->type->array.dimension == nullptr || newnode->type->array.dimension->isExpressionType<ConstantExpr * >() ) {
358 newnode->type->array.isVarLen = false;
359 } else {
360 newnode->type->array.isVarLen = true;
361 } // if
362 return newnode->addQualifiers( qualifiers );
363} // DeclarationNode::newArray
364
365DeclarationNode * DeclarationNode::newVarArray( DeclarationNode * qualifiers ) {
366 DeclarationNode * newnode = new DeclarationNode;
367 newnode->type = new TypeData( TypeData::Array );
368 newnode->type->array.dimension = nullptr;
369 newnode->type->array.isStatic = false;
370 newnode->type->array.isVarLen = true;
371 return newnode->addQualifiers( qualifiers );
372}
373
374DeclarationNode * DeclarationNode::newBitfield( ExpressionNode * size ) {
375 DeclarationNode * newnode = new DeclarationNode;
376 newnode->bitfieldWidth = size;
377 return newnode;
378}
379
380DeclarationNode * DeclarationNode::newTuple( DeclarationNode * members ) {
381 DeclarationNode * newnode = new DeclarationNode;
382 newnode->type = new TypeData( TypeData::Tuple );
383 newnode->type->tuple = members;
384 return newnode;
385}
386
387DeclarationNode * DeclarationNode::newTypeof( ExpressionNode * expr ) {
388 DeclarationNode * newnode = new DeclarationNode;
389 newnode->type = new TypeData( TypeData::Typeof );
390 newnode->type->typeexpr = expr;
391 return newnode;
392}
393
394DeclarationNode * DeclarationNode::newBuiltinType( BuiltinType bt ) {
395 DeclarationNode * newnode = new DeclarationNode;
396 newnode->type = new TypeData( TypeData::Builtin );
397 newnode->builtin = bt;
398 newnode->type->builtintype = newnode->builtin;
399 return newnode;
400} // DeclarationNode::newBuiltinType
401
402DeclarationNode * DeclarationNode::newAttr( string * name, ExpressionNode * expr ) {
403 DeclarationNode * newnode = new DeclarationNode;
404 newnode->type = nullptr;
405// newnode->attr.name = name;
406 newnode->name = name;
407 newnode->attr.expr = expr;
408 return newnode;
409}
410
411DeclarationNode * DeclarationNode::newAttr( string * name, DeclarationNode * type ) {
412 DeclarationNode * newnode = new DeclarationNode;
413 newnode->type = nullptr;
414// newnode->attr.name = name;
415 newnode->name = name;
416 newnode->attr.type = type;
417 return newnode;
418}
419
420DeclarationNode * DeclarationNode::newAttribute( string * name, ExpressionNode * expr ) {
421 DeclarationNode * newnode = new DeclarationNode;
422 newnode->type = nullptr;
423 std::list< Expression * > exprs;
424 buildList( expr, exprs );
425 newnode->attributes.push_back( new Attribute( *name, exprs ) );
426 delete name;
427 return newnode;
428}
429
430DeclarationNode * DeclarationNode::newAsmStmt( StatementNode * stmt ) {
431 DeclarationNode * newnode = new DeclarationNode;
432 newnode->asmStmt = stmt;
433 return newnode;
434}
435
436void appendError( string & dst, const string & src ) {
437 if ( src.empty() ) return;
438 if ( dst.empty() ) { dst = src; return; }
439 dst += ", " + src;
440} // appendError
441
442void DeclarationNode::checkQualifiers( const TypeData * src, const TypeData * dst ) {
443 const Type::Qualifiers qsrc = src->qualifiers, qdst = dst->qualifiers; // optimization
444
445 if ( (qsrc & qdst).any() ) { // duplicates ?
446 for ( unsigned int i = 0; i < Type::NumTypeQualifier; i += 1 ) { // find duplicates
447 if ( qsrc[i] && qdst[i] ) {
448 appendError( error, string( "duplicate " ) + Type::QualifiersNames[i] );
449 } // if
450 } // for
451 } // for
452} // DeclarationNode::checkQualifiers
453
454void DeclarationNode::checkSpecifiers( DeclarationNode * src ) {
455 if ( (funcSpecs & src->funcSpecs).any() ) { // duplicates ?
456 for ( unsigned int i = 0; i < Type::NumFuncSpecifier; i += 1 ) { // find duplicates
457 if ( funcSpecs[i] && src->funcSpecs[i] ) {
458 appendError( error, string( "duplicate " ) + Type::FuncSpecifiersNames[i] );
459 } // if
460 } // for
461 } // if
462
463 if ( storageClasses.any() && src->storageClasses.any() ) { // any reason to check ?
464 if ( (storageClasses & src->storageClasses ).any() ) { // duplicates ?
465 for ( unsigned int i = 0; i < Type::NumStorageClass; i += 1 ) { // find duplicates
466 if ( storageClasses[i] && src->storageClasses[i] ) {
467 appendError( error, string( "duplicate " ) + Type::StorageClassesNames[i] );
468 } // if
469 } // for
470 // src is the new item being added and has a single bit
471 } else if ( ! src->storageClasses.is_threadlocal ) { // conflict ?
472 appendError( error, string( "conflicting " ) + Type::StorageClassesNames[storageClasses.ffs()] +
473 " & " + Type::StorageClassesNames[src->storageClasses.ffs()] );
474 src->storageClasses.reset(); // FIX to preserve invariant of one basic storage specifier
475 } // if
476 } // if
477
478 appendError( error, src->error );
479} // DeclarationNode::checkSpecifiers
480
481DeclarationNode * DeclarationNode::copySpecifiers( DeclarationNode * q ) {
482 funcSpecs |= q->funcSpecs;
483 storageClasses |= q->storageClasses;
484
485 for ( Attribute *attr: reverseIterate( q->attributes ) ) {
486 attributes.push_front( attr->clone() );
487 } // for
488 return this;
489} // DeclarationNode::copySpecifiers
490
491static void addQualifiersToType( TypeData *&src, TypeData * dst ) {
492 if ( src->forall && dst->kind == TypeData::Function ) {
493 if ( dst->forall ) {
494 dst->forall->appendList( src->forall );
495 } else {
496 dst->forall = src->forall;
497 } // if
498 src->forall = nullptr;
499 } // if
500 if ( dst->base ) {
501 addQualifiersToType( src, dst->base );
502 } else if ( dst->kind == TypeData::Function ) {
503 dst->base = src;
504 src = nullptr;
505 } else {
506 dst->qualifiers |= src->qualifiers;
507 } // if
508} // addQualifiersToType
509
510DeclarationNode * DeclarationNode::addQualifiers( DeclarationNode * q ) {
511 if ( ! q ) { delete q; return this; } // empty qualifier
512
513 checkSpecifiers( q );
514 copySpecifiers( q );
515
516 if ( ! q->type ) { delete q; return this; }
517
518 if ( ! type ) {
519 type = q->type; // reuse structure
520 q->type = nullptr;
521 delete q;
522 return this;
523 } // if
524
525 if ( q->type->forall ) { // forall qualifier ?
526 if ( type->forall ) { // polymorphic routine ?
527 type->forall->appendList( q->type->forall ); // augment forall qualifier
528 } else {
529 if ( type->kind == TypeData::Aggregate ) { // struct/union ?
530 if ( type->aggregate.params ) { // polymorphic ?
531 type->aggregate.params->appendList( q->type->forall ); // augment forall qualifier
532 } else { // not polymorphic
533 type->aggregate.params = q->type->forall; // make polymorphic type
534 // change implicit typedef from TYPEDEFname to TYPEGENname
535 typedefTable.changeKind( *type->aggregate.name, TypedefTable::TG );
536 } // if
537 } else { // not polymorphic
538 type->forall = q->type->forall; // make polymorphic routine
539 } // if
540 } // if
541 q->type->forall = nullptr; // forall qualifier moved
542 } // if
543
544 checkQualifiers( type, q->type );
545 addQualifiersToType( q->type, type );
546
547 delete q;
548 return this;
549} // addQualifiers
550
551static void addTypeToType( TypeData *&src, TypeData *&dst ) {
552 if ( src->forall && dst->kind == TypeData::Function ) {
553 if ( dst->forall ) {
554 dst->forall->appendList( src->forall );
555 } else {
556 dst->forall = src->forall;
557 } // if
558 src->forall = nullptr;
559 } // if
560 if ( dst->base ) {
561 addTypeToType( src, dst->base );
562 } else {
563 switch ( dst->kind ) {
564 case TypeData::Unknown:
565 src->qualifiers |= dst->qualifiers;
566 dst = src;
567 src = nullptr;
568 break;
569 case TypeData::Basic:
570 dst->qualifiers |= src->qualifiers;
571 if ( src->kind != TypeData::Unknown ) {
572 assert( src->kind == TypeData::Basic );
573
574 if ( dst->basictype == DeclarationNode::NoBasicType ) {
575 dst->basictype = src->basictype;
576 } else if ( src->basictype != DeclarationNode::NoBasicType )
577 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::basicTypeNames[ src->basictype ] + " in type: ", src );
578
579 if ( dst->complextype == DeclarationNode::NoComplexType ) {
580 dst->complextype = src->complextype;
581 } else if ( src->complextype != DeclarationNode::NoComplexType )
582 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::complexTypeNames[ src->complextype ] + " in type: ", src );
583
584 if ( dst->signedness == DeclarationNode::NoSignedness ) {
585 dst->signedness = src->signedness;
586 } else if ( src->signedness != DeclarationNode::NoSignedness )
587 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::signednessNames[ src->signedness ] + " in type: ", src );
588
589 if ( dst->length == DeclarationNode::NoLength ) {
590 dst->length = src->length;
591 } else if ( dst->length == DeclarationNode::Long && src->length == DeclarationNode::Long ) {
592 dst->length = DeclarationNode::LongLong;
593 } else if ( src->length != DeclarationNode::NoLength )
594 throw SemanticError( string( "conflicting type specifier " ) + DeclarationNode::lengthNames[ src->length ] + " in type: ", src );
595 } // if
596 break;
597 default:
598 switch ( src->kind ) {
599 case TypeData::Aggregate:
600 case TypeData::Enum:
601 dst->base = new TypeData( TypeData::AggregateInst );
602 dst->base->aggInst.aggregate = src;
603 if ( src->kind == TypeData::Aggregate ) {
604 dst->base->aggInst.params = maybeClone( src->aggregate.actuals );
605 } // if
606 dst->base->qualifiers |= src->qualifiers;
607 src = nullptr;
608 break;
609 default:
610 if ( dst->forall ) {
611 dst->forall->appendList( src->forall );
612 } else {
613 dst->forall = src->forall;
614 } // if
615 src->forall = nullptr;
616 dst->base = src;
617 src = nullptr;
618 } // switch
619 } // switch
620 } // if
621}
622
623DeclarationNode * DeclarationNode::addType( DeclarationNode * o ) {
624 if ( o ) {
625 checkSpecifiers( o );
626 copySpecifiers( o );
627 if ( o->type ) {
628 if ( ! type ) {
629 if ( o->type->kind == TypeData::Aggregate || o->type->kind == TypeData::Enum ) {
630 type = new TypeData( TypeData::AggregateInst );
631 type->aggInst.aggregate = o->type;
632 if ( o->type->kind == TypeData::Aggregate ) {
633 type->aggInst.hoistType = o->type->aggregate.body;
634 type->aggInst.params = maybeClone( o->type->aggregate.actuals );
635 } else {
636 type->aggInst.hoistType = o->type->enumeration.body;
637 } // if
638 type->qualifiers |= o->type->qualifiers;
639 } else {
640 type = o->type;
641 } // if
642 o->type = nullptr;
643 } else {
644 addTypeToType( o->type, type );
645 } // if
646 } // if
647 if ( o->bitfieldWidth ) {
648 bitfieldWidth = o->bitfieldWidth;
649 } // if
650
651 // there may be typedefs chained onto the type
652 if ( o->get_next() ) {
653 set_last( o->get_next()->clone() );
654 } // if
655 } // if
656 delete o;
657 return this;
658}
659
660DeclarationNode * DeclarationNode::addTypedef() {
661 TypeData * newtype = new TypeData( TypeData::Symbolic );
662 newtype->symbolic.params = nullptr;
663 newtype->symbolic.isTypedef = true;
664 newtype->symbolic.name = name ? new string( *name ) : nullptr;
665 newtype->base = type;
666 type = newtype;
667 return this;
668}
669
670DeclarationNode * DeclarationNode::addAssertions( DeclarationNode * assertions ) {
671 if ( variable.tyClass != NoTypeClass ) {
672 if ( variable.assertions ) {
673 variable.assertions->appendList( assertions );
674 } else {
675 variable.assertions = assertions;
676 } // if
677 return this;
678 } // if
679
680 assert( type );
681 switch ( type->kind ) {
682 case TypeData::Symbolic:
683 if ( type->symbolic.assertions ) {
684 type->symbolic.assertions->appendList( assertions );
685 } else {
686 type->symbolic.assertions = assertions;
687 } // if
688 break;
689 default:
690 assert( false );
691 } // switch
692
693 return this;
694}
695
696DeclarationNode * DeclarationNode::addName( string * newname ) {
697 assert( ! name );
698 name = newname;
699 return this;
700}
701
702DeclarationNode * DeclarationNode::addAsmName( DeclarationNode * newname ) {
703 assert( ! asmName );
704 asmName = newname ? newname->asmName : nullptr;
705 return this->addQualifiers( newname );
706}
707
708DeclarationNode * DeclarationNode::addBitfield( ExpressionNode * size ) {
709 bitfieldWidth = size;
710 return this;
711}
712
713DeclarationNode * DeclarationNode::addVarArgs() {
714 assert( type );
715 hasEllipsis = true;
716 return this;
717}
718
719DeclarationNode * DeclarationNode::addFunctionBody( StatementNode * body, StatementNode * with ) {
720 assert( type );
721 assert( type->kind == TypeData::Function );
722 assert( ! type->function.body );
723 if ( with ) {
724 // convert
725 // void f(S s) with (s) { x = 0; }
726 // to
727 // void f(S s) { with(s) { x = 0; } }
728 WithStmt * withStmt = strict_dynamic_cast< WithStmt * >( with->build() );
729 withStmt->stmt = body->build();
730 delete body;
731 delete with;
732 body = new StatementNode( new CompoundStmt( { withStmt } ) );
733 }
734 type->function.body = body;
735 return this;
736}
737
738DeclarationNode * DeclarationNode::addOldDeclList( DeclarationNode * list ) {
739 assert( type );
740 assert( type->kind == TypeData::Function );
741 assert( ! type->function.oldDeclList );
742 type->function.oldDeclList = list;
743 return this;
744}
745
746DeclarationNode * DeclarationNode::setBase( TypeData * newType ) {
747 if ( type ) {
748 TypeData * prevBase = type;
749 TypeData * curBase = type->base;
750 while ( curBase != nullptr ) {
751 prevBase = curBase;
752 curBase = curBase->base;
753 } // while
754 prevBase->base = newType;
755 } else {
756 type = newType;
757 } // if
758 return this;
759}
760
761DeclarationNode * DeclarationNode::copyAttribute( DeclarationNode * a ) {
762 if ( a ) {
763 for ( Attribute *attr: reverseIterate( a->attributes ) ) {
764 attributes.push_front( attr );
765 } // for
766 a->attributes.clear();
767 } // if
768 return this;
769} // copyAttribute
770
771DeclarationNode * DeclarationNode::addPointer( DeclarationNode * p ) {
772 if ( p ) {
773 assert( p->type->kind == TypeData::Pointer || TypeData::Reference );
774 setBase( p->type );
775 p->type = nullptr;
776 copyAttribute( p );
777 delete p;
778 } // if
779 return this;
780}
781
782DeclarationNode * DeclarationNode::addArray( DeclarationNode * a ) {
783 if ( a ) {
784 assert( a->type->kind == TypeData::Array );
785 setBase( a->type );
786 a->type = nullptr;
787 copyAttribute( a );
788 delete a;
789 } // if
790 return this;
791}
792
793DeclarationNode * DeclarationNode::addNewPointer( DeclarationNode * p ) {
794 if ( p ) {
795 assert( p->type->kind == TypeData::Pointer || p->type->kind == TypeData::Reference );
796 if ( type ) {
797 switch ( type->kind ) {
798 case TypeData::Aggregate:
799 case TypeData::Enum:
800 p->type->base = new TypeData( TypeData::AggregateInst );
801 p->type->base->aggInst.aggregate = type;
802 if ( type->kind == TypeData::Aggregate ) {
803 p->type->base->aggInst.params = maybeClone( type->aggregate.actuals );
804 } // if
805 p->type->base->qualifiers |= type->qualifiers;
806 break;
807
808 default:
809 p->type->base = type;
810 } // switch
811 type = nullptr;
812 } // if
813 delete this;
814 return p;
815 } else {
816 return this;
817 } // if
818}
819
820static TypeData * findLast( TypeData * a ) {
821 assert( a );
822 TypeData * cur = a;
823 while ( cur->base ) {
824 cur = cur->base;
825 } // while
826 return cur;
827}
828
829DeclarationNode * DeclarationNode::addNewArray( DeclarationNode * a ) {
830 if ( ! a ) return this;
831 assert( a->type->kind == TypeData::Array );
832 TypeData * lastArray = findLast( a->type );
833 if ( type ) {
834 switch ( type->kind ) {
835 case TypeData::Aggregate:
836 case TypeData::Enum:
837 lastArray->base = new TypeData( TypeData::AggregateInst );
838 lastArray->base->aggInst.aggregate = type;
839 if ( type->kind == TypeData::Aggregate ) {
840 lastArray->base->aggInst.params = maybeClone( type->aggregate.actuals );
841 } // if
842 lastArray->base->qualifiers |= type->qualifiers;
843 break;
844 default:
845 lastArray->base = type;
846 } // switch
847 type = nullptr;
848 } // if
849 delete this;
850 return a;
851}
852
853DeclarationNode * DeclarationNode::addParamList( DeclarationNode * params ) {
854 TypeData * ftype = new TypeData( TypeData::Function );
855 ftype->function.params = params;
856 setBase( ftype );
857 return this;
858}
859
860static TypeData * addIdListToType( TypeData * type, DeclarationNode * ids ) {
861 if ( type ) {
862 if ( type->kind != TypeData::Function ) {
863 type->base = addIdListToType( type->base, ids );
864 } else {
865 type->function.idList = ids;
866 } // if
867 return type;
868 } else {
869 TypeData * newtype = new TypeData( TypeData::Function );
870 newtype->function.idList = ids;
871 return newtype;
872 } // if
873} // addIdListToType
874
875DeclarationNode * DeclarationNode::addIdList( DeclarationNode * ids ) {
876 type = addIdListToType( type, ids );
877 return this;
878}
879
880DeclarationNode * DeclarationNode::addInitializer( InitializerNode * init ) {
881 initializer = init;
882 return this;
883}
884
885DeclarationNode * DeclarationNode::addTypeInitializer( DeclarationNode * init ) {
886 assertf( variable.tyClass != NoTypeClass, "Called addTypeInitializer on something that isn't a type variable." );
887 variable.initializer = init;
888 return this;
889}
890
891DeclarationNode * DeclarationNode::cloneType( string * newName ) {
892 DeclarationNode * newnode = new DeclarationNode;
893 newnode->type = maybeClone( type );
894 newnode->copySpecifiers( this );
895 assert( newName );
896 newnode->name = newName;
897 return newnode;
898}
899
900DeclarationNode * DeclarationNode::cloneBaseType( DeclarationNode * o ) {
901 if ( ! o ) return nullptr;
902
903 o->copySpecifiers( this );
904 if ( type ) {
905 TypeData * srcType = type;
906
907 // search for the base type by scanning off pointers and array designators
908 while ( srcType->base ) {
909 srcType = srcType->base;
910 } // while
911
912 TypeData * newType = srcType->clone();
913 if ( newType->kind == TypeData::AggregateInst ) {
914 // don't duplicate members
915 if ( newType->aggInst.aggregate->kind == TypeData::Enum ) {
916 delete newType->aggInst.aggregate->enumeration.constants;
917 newType->aggInst.aggregate->enumeration.constants = nullptr;
918 } else {
919 assert( newType->aggInst.aggregate->kind == TypeData::Aggregate );
920 delete newType->aggInst.aggregate->aggregate.fields;
921 newType->aggInst.aggregate->aggregate.fields = nullptr;
922 } // if
923 // don't hoist twice
924 newType->aggInst.hoistType = false;
925 } // if
926
927 newType->forall = maybeClone( type->forall );
928 if ( ! o->type ) {
929 o->type = newType;
930 } else {
931 addTypeToType( newType, o->type );
932 delete newType;
933 } // if
934 } // if
935 return o;
936}
937
938DeclarationNode * DeclarationNode::extractAggregate() const {
939 if ( type ) {
940 TypeData * ret = typeextractAggregate( type );
941 if ( ret ) {
942 DeclarationNode * newnode = new DeclarationNode;
943 newnode->type = ret;
944 return newnode;
945 } // if
946 } // if
947 return nullptr;
948}
949
950void buildList( const DeclarationNode * firstNode, std::list< Declaration * > &outputList ) {
951 SemanticError errors;
952 std::back_insert_iterator< std::list< Declaration * > > out( outputList );
953
954 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
955 try {
956 if ( DeclarationNode * extr = cur->extractAggregate() ) {
957 // handle the case where a structure declaration is contained within an object or type declaration
958 Declaration * decl = extr->build();
959 if ( decl ) {
960 decl->location = cur->location;
961 * out++ = decl;
962 } // if
963 delete extr;
964 } // if
965
966 Declaration * decl = cur->build();
967 if ( decl ) {
968 decl->location = cur->location;
969 * out++ = decl;
970 } // if
971 } catch( SemanticError &e ) {
972 e.set_location( cur->location );
973 errors.append( e );
974 } // try
975 } // while
976
977 if ( ! errors.isEmpty() ) {
978 throw errors;
979 } // if
980} // buildList
981
982void buildList( const DeclarationNode * firstNode, std::list< DeclarationWithType * > &outputList ) {
983 SemanticError errors;
984 std::back_insert_iterator< std::list< DeclarationWithType * > > out( outputList );
985
986 for ( const DeclarationNode * cur = firstNode; cur; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ) ) {
987 try {
988 Declaration * decl = cur->build();
989 if ( decl ) {
990 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) {
991 dwt->location = cur->location;
992 * out++ = dwt;
993 } else if ( StructDecl * agg = dynamic_cast< StructDecl * >( decl ) ) {
994 StructInstType * inst = new StructInstType( Type::Qualifiers(), agg->get_name() );
995 auto obj = new ObjectDecl( "", Type::StorageClasses(), linkage, nullptr, inst, nullptr );
996 obj->location = cur->location;
997 * out++ = obj;
998 delete agg;
999 } else if ( UnionDecl * agg = dynamic_cast< UnionDecl * >( decl ) ) {
1000 UnionInstType * inst = new UnionInstType( 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 } // if
1005 } // if
1006 } catch( SemanticError &e ) {
1007 e.set_location( cur->location );
1008 errors.append( e );
1009 } // try
1010 } // for
1011
1012 if ( ! errors.isEmpty() ) {
1013 throw errors;
1014 } // if
1015} // buildList
1016
1017void buildTypeList( const DeclarationNode * firstNode, std::list< Type * > &outputList ) {
1018 SemanticError errors;
1019 std::back_insert_iterator< std::list< Type * > > out( outputList );
1020 const DeclarationNode * cur = firstNode;
1021
1022 while ( cur ) {
1023 try {
1024 * out++ = cur->buildType();
1025 } catch( SemanticError &e ) {
1026 e.set_location( cur->location );
1027 errors.append( e );
1028 } // try
1029 cur = dynamic_cast< DeclarationNode * >( cur->get_next() );
1030 } // while
1031
1032 if ( ! errors.isEmpty() ) {
1033 throw errors;
1034 } // if
1035} // buildTypeList
1036
1037Declaration * DeclarationNode::build() const {
1038 if ( ! error.empty() ) throw SemanticError( error + " in declaration of ", this );
1039
1040 if ( asmStmt ) {
1041 return new AsmDecl( strict_dynamic_cast<AsmStmt *>( asmStmt->build() ) );
1042 } // if
1043
1044 if ( variable.tyClass != NoTypeClass ) {
1045 // otype is internally converted to dtype + otype parameters
1046 static const TypeDecl::Kind kindMap[] = { TypeDecl::Dtype, TypeDecl::Dtype, TypeDecl::Ftype, TypeDecl::Ttype };
1047 assertf( sizeof(kindMap)/sizeof(kindMap[0]) == NoTypeClass, "DeclarationNode::build: kindMap is out of sync." );
1048 assertf( variable.tyClass < sizeof(kindMap)/sizeof(kindMap[0]), "Variable's tyClass is out of bounds." );
1049 TypeDecl * ret = new TypeDecl( *name, Type::StorageClasses(), nullptr, kindMap[ variable.tyClass ], variable.tyClass == Otype, variable.initializer ? variable.initializer->buildType() : nullptr );
1050 buildList( variable.assertions, ret->get_assertions() );
1051 return ret;
1052 } // if
1053
1054 if ( type ) {
1055 // Function specifiers can only appear on a function definition/declaration.
1056 //
1057 // inline _Noreturn int f(); // allowed
1058 // inline _Noreturn int g( int i ); // allowed
1059 // inline _Noreturn int i; // disallowed
1060 if ( type->kind != TypeData::Function && funcSpecs.any() ) {
1061 throw SemanticError( "invalid function specifier for ", this );
1062 } // if
1063 return buildDecl( type, name ? *name : string( "" ), storageClasses, maybeBuild< Expression >( bitfieldWidth ), funcSpecs, linkage, asmName, maybeBuild< Initializer >(initializer), attributes )->set_extension( extension );
1064 } // if
1065
1066 // SUE's cannot have function specifiers, either
1067 //
1068 // inlne _Noreturn struct S { ... }; // disallowed
1069 // inlne _Noreturn enum E { ... }; // disallowed
1070 if ( funcSpecs.any() ) {
1071 throw SemanticError( "invalid function specifier for ", this );
1072 } // if
1073 assertf( name, "ObjectDecl must a have name\n" );
1074 return (new ObjectDecl( *name, storageClasses, linkage, maybeBuild< Expression >( bitfieldWidth ), nullptr, maybeBuild< Initializer >( initializer ) ))->set_asmName( asmName )->set_extension( extension );
1075}
1076
1077Type * DeclarationNode::buildType() const {
1078 assert( type );
1079
1080 if ( attr.expr ) {
1081 return new AttrType( buildQualifiers( type ), *name, attr.expr->build(), attributes );
1082 } else if ( attr.type ) {
1083 return new AttrType( buildQualifiers( type ), *name, attr.type->buildType(), attributes );
1084 } // if
1085
1086 switch ( type->kind ) {
1087 case TypeData::Enum:
1088 case TypeData::Aggregate: {
1089 ReferenceToType * ret = buildComAggInst( type, attributes, linkage );
1090 buildList( type->aggregate.actuals, ret->get_parameters() );
1091 return ret;
1092 }
1093 case TypeData::Symbolic: {
1094 TypeInstType * ret = new TypeInstType( buildQualifiers( type ), *type->symbolic.name, false, attributes );
1095 buildList( type->symbolic.actuals, ret->get_parameters() );
1096 return ret;
1097 }
1098 default:
1099 Type * simpletypes = typebuild( type );
1100 simpletypes->get_attributes() = attributes; // copy because member is const
1101 return simpletypes;
1102 } // switch
1103}
1104
1105// Local Variables: //
1106// tab-width: 4 //
1107// mode: c++ //
1108// compile-command: "make install" //
1109// End: //
Note: See TracBrowser for help on using the repository browser.