source: src/Parser/DeclarationNode.cc@ 6152c81

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 6152c81 was c3396e0, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

code clean up

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