source: src/Parser/TypeData.cc@ 233e4d9

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 233e4d9 was 615a096, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

fix BFCommon problem on gcc-4.9, and begin consistent renaming

  • Property mode set to 100644
File size: 31.0 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// TypeData.cc --
8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 15:12:51 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Mar 17 08:46:10 2017
13// Update Count : 560
14//
15
16#include <cassert>
17#include <algorithm>
18#include <iterator>
19#include "Common/utility.h"
20#include "TypeData.h"
21#include "SynTree/Type.h"
22#include "SynTree/Declaration.h"
23#include "SynTree/Expression.h"
24#include "SynTree/Statement.h"
25#include "SynTree/Initializer.h"
26using namespace std;
27
28TypeData::TypeData( Kind k ) : kind( k ), base( nullptr ), forall( nullptr ) /*, PTR1( (void*)(0xdeadbeefdeadbeef)), PTR2( (void*)(0xdeadbeefdeadbeef) ) */ {
29 switch ( kind ) {
30 case Unknown:
31 case Pointer:
32 case EnumConstant:
33 // nothing else to initialize
34 break;
35 case Basic:
36 // basic = new Basic_t;
37 break;
38 case Array:
39 // array = new Array_t;
40 array.dimension = nullptr;
41 array.isVarLen = false;
42 array.isStatic = false;
43 break;
44 case Function:
45 // function = new Function_t;
46 function.params = nullptr;
47 function.idList = nullptr;
48 function.oldDeclList = nullptr;
49 function.body = nullptr;
50 function.newStyle = false;
51 break;
52 // Enum is an Aggregate, so both structures are initialized together.
53 case Enum:
54 // enumeration = new Enumeration_t;
55 enumeration.name = nullptr;
56 enumeration.constants = nullptr;
57 enumeration.body = false;
58 case Aggregate:
59 // aggregate = new Aggregate_t;
60 aggregate.name = nullptr;
61 aggregate.params = nullptr;
62 aggregate.actuals = nullptr;
63 aggregate.fields = nullptr;
64 aggregate.body = false;
65 break;
66 case AggregateInst:
67 // aggInst = new AggInst_t;
68 aggInst.aggregate = nullptr;
69 aggInst.params = nullptr;
70 aggInst.hoistType = false;;
71 break;
72 case Symbolic:
73 case SymbolicInst:
74 // symbolic = new Symbolic_t;
75 symbolic.name = nullptr;
76 symbolic.params = nullptr;
77 symbolic.actuals = nullptr;
78 symbolic.assertions = nullptr;
79 break;
80 case Tuple:
81 // tuple = new Tuple_t;
82 tuple = nullptr;
83 break;
84 case Typeof:
85 // typeexpr = new Typeof_t;
86 typeexpr = nullptr;
87 break;
88 case Builtin:
89 // builtin = new Builtin_t;
90 break;
91 } // switch
92} // TypeData::TypeData
93
94TypeData::~TypeData() {
95 delete base;
96 delete forall;
97
98 switch ( kind ) {
99 case Unknown:
100 case Pointer:
101 case EnumConstant:
102 // nothing to destroy
103 break;
104 case Basic:
105 // delete basic;
106 break;
107 case Array:
108 delete array.dimension;
109 // delete array;
110 break;
111 case Function:
112 delete function.params;
113 delete function.idList;
114 delete function.oldDeclList;
115 delete function.body;
116 // delete function;
117 break;
118 case Aggregate:
119 delete aggregate.name;
120 delete aggregate.params;
121 delete aggregate.actuals;
122 delete aggregate.fields;
123 // delete aggregate;
124 break;
125 case AggregateInst:
126 delete aggInst.aggregate;
127 delete aggInst.params;
128 // delete aggInst;
129 break;
130 case Enum:
131 delete enumeration.name;
132 delete enumeration.constants;
133 // delete enumeration;
134 break;
135 case Symbolic:
136 case SymbolicInst:
137 delete symbolic.name;
138 delete symbolic.params;
139 delete symbolic.actuals;
140 delete symbolic.assertions;
141 // delete symbolic;
142 break;
143 case Tuple:
144 // delete tuple->members;
145 delete tuple;
146 break;
147 case Typeof:
148 // delete typeexpr->expr;
149 delete typeexpr;
150 break;
151 case Builtin:
152 // delete builtin;
153 break;
154 } // switch
155} // TypeData::~TypeData
156
157TypeData * TypeData::clone() const {
158 TypeData * newtype = new TypeData( kind );
159 newtype->qualifiers = qualifiers;
160 newtype->base = maybeClone( base );
161 newtype->forall = maybeClone( forall );
162
163 switch ( kind ) {
164 case Unknown:
165 case EnumConstant:
166 case Pointer:
167 // nothing else to copy
168 break;
169 case Basic:
170 newtype->basictype = basictype;
171 newtype->complextype = complextype;
172 newtype->signedness = signedness;
173 newtype->length = length;
174 break;
175 case Array:
176 newtype->array.dimension = maybeClone( array.dimension );
177 newtype->array.isVarLen = array.isVarLen;
178 newtype->array.isStatic = array.isStatic;
179 break;
180 case Function:
181 newtype->function.params = maybeClone( function.params );
182 newtype->function.idList = maybeClone( function.idList );
183 newtype->function.oldDeclList = maybeClone( function.oldDeclList );
184 newtype->function.body = maybeClone( function.body );
185 newtype->function.newStyle = function.newStyle;
186 break;
187 case Aggregate:
188 newtype->aggregate.name = aggregate.name ? new string( *aggregate.name ) : nullptr;
189 newtype->aggregate.params = maybeClone( aggregate.params );
190 newtype->aggregate.actuals = maybeClone( aggregate.actuals );
191 newtype->aggregate.fields = maybeClone( aggregate.fields );
192 newtype->aggregate.kind = aggregate.kind;
193 newtype->aggregate.body = aggregate.body;
194 break;
195 case AggregateInst:
196 newtype->aggInst.aggregate = maybeClone( aggInst.aggregate );
197 newtype->aggInst.params = maybeClone( aggInst.params );
198 newtype->aggInst.hoistType = aggInst.hoistType;
199 break;
200 case Enum:
201 newtype->enumeration.name = enumeration.name ? new string( *enumeration.name ) : nullptr;
202 newtype->enumeration.constants = maybeClone( enumeration.constants );
203 newtype->enumeration.body = enumeration.body;
204 break;
205 case Symbolic:
206 case SymbolicInst:
207 newtype->symbolic.name = symbolic.name ? new string( *symbolic.name ) : nullptr;
208 newtype->symbolic.params = maybeClone( symbolic.params );
209 newtype->symbolic.actuals = maybeClone( symbolic.actuals );
210 newtype->symbolic.assertions = maybeClone( symbolic.assertions );
211 newtype->symbolic.isTypedef = symbolic.isTypedef;
212 break;
213 case Tuple:
214 newtype->tuple = maybeClone( tuple );
215 break;
216 case Typeof:
217 newtype->typeexpr = maybeClone( typeexpr );
218 break;
219 case Builtin:
220 assert( builtintype == DeclarationNode::Zero || builtintype == DeclarationNode::One );
221 newtype->builtintype = builtintype;
222 break;
223 } // switch
224 return newtype;
225} // TypeData::clone
226
227void TypeData::print( ostream &os, int indent ) const {
228 for ( int i = 0; i < Type::NumTypeQualifier; i += 1 ) {
229 if ( qualifiers[i] ) os << Type::QualifiersNames[ i ] << ' ';
230 } // for
231
232 if ( forall ) {
233 os << "forall " << endl;
234 forall->printList( os, indent + 4 );
235 } // if
236
237 switch ( kind ) {
238 case Unknown:
239 os << "entity of unknown type ";
240 break;
241 case Pointer:
242 os << "pointer ";
243 if ( base ) {
244 os << "to ";
245 base->print( os, indent );
246 } // if
247 break;
248 case EnumConstant:
249 os << "enumeration constant ";
250 break;
251 case Basic:
252 if ( signedness != DeclarationNode::NoSignedness ) os << DeclarationNode::signednessNames[ signedness ] << " ";
253 if ( length != DeclarationNode::NoLength ) os << DeclarationNode::lengthNames[ length ] << " ";
254 assert( basictype != DeclarationNode::NoBasicType );
255 os << DeclarationNode::basicTypeNames[ basictype ] << " ";
256 if ( complextype != DeclarationNode::NoComplexType ) os << DeclarationNode::complexTypeNames[ complextype ] << " ";
257 break;
258 case Array:
259 if ( array.isStatic ) {
260 os << "static ";
261 } // if
262 if ( array.dimension ) {
263 os << "array of ";
264 array.dimension->printOneLine( os, indent );
265 } else if ( array.isVarLen ) {
266 os << "variable-length array of ";
267 } else {
268 os << "open array of ";
269 } // if
270 if ( base ) {
271 base->print( os, indent );
272 } // if
273 break;
274 case Function:
275 os << "function" << endl;
276 if ( function.params ) {
277 os << string( indent + 2, ' ' ) << "with parameters " << endl;
278 function.params->printList( os, indent + 4 );
279 } else {
280 os << string( indent + 2, ' ' ) << "with no parameters " << endl;
281 } // if
282 if ( function.idList ) {
283 os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl;
284 function.idList->printList( os, indent + 4 );
285 } // if
286 if ( function.oldDeclList ) {
287 os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl;
288 function.oldDeclList->printList( os, indent + 4 );
289 } // if
290 os << string( indent + 2, ' ' ) << "returning ";
291 if ( base ) {
292 base->print( os, indent + 4 );
293 } else {
294 os << "nothing ";
295 } // if
296 os << endl;
297 if ( function.body ) {
298 os << string( indent + 2, ' ' ) << "with body " << endl;
299 function.body->printList( os, indent + 2 );
300 } // if
301 break;
302 case Aggregate:
303 os << DeclarationNode::aggregateNames[ aggregate.kind ] << ' ' << *aggregate.name << endl;
304 if ( aggregate.params ) {
305 os << string( indent + 2, ' ' ) << "with type parameters " << endl;
306 aggregate.params->printList( os, indent + 4 );
307 } // if
308 if ( aggregate.actuals ) {
309 os << string( indent + 2, ' ' ) << "instantiated with actual parameters " << endl;
310 aggregate.actuals->printList( os, indent + 4 );
311 } // if
312 if ( aggregate.fields ) {
313 os << string( indent + 2, ' ' ) << "with members " << endl;
314 aggregate.fields->printList( os, indent + 4 );
315 } // if
316 if ( aggregate.body ) {
317 os << string( indent + 2, ' ' ) << " with body " << endl;
318 } // if
319 break;
320 case AggregateInst:
321 if ( aggInst.aggregate ) {
322 os << "instance of " ;
323 aggInst.aggregate->print( os, indent );
324 } else {
325 os << "instance of an unspecified aggregate ";
326 } // if
327 if ( aggInst.params ) {
328 os << string( indent + 2, ' ' ) << "with parameters " << endl;
329 aggInst.params->printList( os, indent + 2 );
330 } // if
331 break;
332 case Enum:
333 os << "enumeration ";
334 if ( enumeration.constants ) {
335 os << "with constants" << endl;
336 enumeration.constants->printList( os, indent + 2 );
337 } // if
338 if ( enumeration.body ) {
339 os << string( indent + 2, ' ' ) << " with body " << endl;
340 } // if
341 break;
342 case SymbolicInst:
343 os << "instance of type " << *symbolic.name;
344 if ( symbolic.actuals ) {
345 os << " with parameters" << endl;
346 symbolic.actuals->printList( os, indent + 2 );
347 } // if
348 break;
349 case Symbolic:
350 if ( symbolic.isTypedef ) {
351 os << "typedef definition ";
352 } else {
353 os << "type definition ";
354 } // if
355 if ( symbolic.params ) {
356 os << endl << string( indent + 2, ' ' ) << "with parameters" << endl;
357 symbolic.params->printList( os, indent + 2 );
358 } // if
359 if ( symbolic.assertions ) {
360 os << endl << string( indent + 2, ' ' ) << "with assertions" << endl;
361 symbolic.assertions->printList( os, indent + 4 );
362 os << string( indent + 2, ' ' );
363 } // if
364 if ( base ) {
365 os << "for ";
366 base->print( os, indent + 2 );
367 } // if
368 break;
369 case Tuple:
370 os << "tuple ";
371 if ( tuple ) {
372 os << "with members " << endl;
373 tuple->printList( os, indent + 2 );
374 } // if
375 break;
376 case Typeof:
377 os << "type-of expression ";
378 if ( typeexpr ) {
379 typeexpr->print( os, indent + 2 );
380 } // if
381 break;
382 case Builtin:
383 os << "gcc builtin type";
384 break;
385 default:
386 os << "internal error: TypeData::print " << kind << endl;
387 assert( false );
388 } // switch
389} // TypeData::print
390
391template< typename ForallList >
392void buildForall( const DeclarationNode * firstNode, ForallList &outputList ) {
393 buildList( firstNode, outputList );
394 for ( typename ForallList::iterator i = outputList.begin(); i != outputList.end(); ++i ) {
395 TypeDecl * td = static_cast<TypeDecl *>(*i);
396 if ( td->get_kind() == TypeDecl::Any ) {
397 // add assertion parameters to `type' tyvars in reverse order
398 // add dtor: void ^?{}(T *)
399 FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false );
400 dtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
401 td->get_assertions().push_front( new FunctionDecl( "^?{}", Type::StorageClasses(), LinkageSpec::Cforall, dtorType, nullptr ) );
402
403 // add copy ctor: void ?{}(T *, T)
404 FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false );
405 copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
406 copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
407 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, copyCtorType, nullptr ) );
408
409 // add default ctor: void ?{}(T *)
410 FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false );
411 ctorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
412 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, ctorType, nullptr ) );
413
414 // add assignment operator: T * ?=?(T *, T)
415 FunctionType * assignType = new FunctionType( Type::Qualifiers(), false );
416 assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new PointerType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
417 assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
418 assignType->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
419 td->get_assertions().push_front( new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Cforall, assignType, nullptr ) );
420 } // if
421 } // for
422}
423
424Type * typebuild( const TypeData * td ) {
425 assert( td );
426 switch ( td->kind ) {
427 case TypeData::Unknown:
428 // fill in implicit int
429 return new BasicType( buildQualifiers( td ), BasicType::SignedInt );
430 case TypeData::Basic:
431 return buildBasicType( td );
432 case TypeData::Pointer:
433 return buildPointer( td );
434 case TypeData::Array:
435 return buildArray( td );
436 case TypeData::Function:
437 return buildFunction( td );
438 case TypeData::AggregateInst:
439 return buildAggInst( td );
440 case TypeData::EnumConstant:
441 // the name gets filled in later -- by SymTab::Validate
442 return new EnumInstType( buildQualifiers( td ), "" );
443 case TypeData::SymbolicInst:
444 return buildSymbolicInst( td );;
445 case TypeData::Tuple:
446 return buildTuple( td );
447 case TypeData::Typeof:
448 return buildTypeof( td );
449 case TypeData::Builtin:
450 if(td->builtintype == DeclarationNode::Zero) {
451 return new ZeroType( emptyQualifiers );
452 }
453 else if(td->builtintype == DeclarationNode::One) {
454 return new OneType( emptyQualifiers );
455 }
456 else {
457 return new VarArgsType( buildQualifiers( td ) );
458 }
459 case TypeData::Symbolic:
460 case TypeData::Enum:
461 case TypeData::Aggregate:
462 assert( false );
463 } // switch
464 return nullptr;
465} // typebuild
466
467TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) {
468 TypeData * ret = nullptr;
469
470 switch ( td->kind ) {
471 case TypeData::Aggregate:
472 if ( ! toplevel && td->aggregate.fields ) {
473 ret = td->clone();
474 } // if
475 break;
476 case TypeData::Enum:
477 if ( ! toplevel && td->enumeration.constants ) {
478 ret = td->clone();
479 } // if
480 break;
481 case TypeData::AggregateInst:
482 if ( td->aggInst.aggregate ) {
483 ret = typeextractAggregate( td->aggInst.aggregate, false );
484 } // if
485 break;
486 default:
487 if ( td->base ) {
488 ret = typeextractAggregate( td->base, false );
489 } // if
490 } // switch
491 return ret;
492} // typeextractAggregate
493
494Type::Qualifiers buildQualifiers( const TypeData * td ) {
495 return td->qualifiers;
496} // buildQualifiers
497
498Type * buildBasicType( const TypeData * td ) {
499 BasicType::Kind ret;
500
501 switch ( td->basictype ) {
502 case DeclarationNode::Void:
503 if ( td->signedness != DeclarationNode::NoSignedness && td->length != DeclarationNode::NoLength ) {
504 throw SemanticError( "invalid type specifier \"void\" in type: ", td );
505 } // if
506
507 return new VoidType( buildQualifiers( td ) );
508 break;
509
510 case DeclarationNode::Bool:
511 if ( td->signedness != DeclarationNode::NoSignedness ) {
512 throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::signednessNames[ td->signedness ] + " in type: ", td );
513 } // if
514 if ( td->length != DeclarationNode::NoLength ) {
515 throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthNames[ td->length ] + " in type: ", td );
516 } // if
517
518 ret = BasicType::Bool;
519 break;
520
521 case DeclarationNode::Char:
522 // C11 Standard 6.2.5.15: The three types char, signed char, and unsigned char are collectively called the
523 // character types. The implementation shall define char to have the same range, representation, and behavior as
524 // either signed char or unsigned char.
525 static BasicType::Kind chartype[] = { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::Char };
526
527 if ( td->length != DeclarationNode::NoLength ) {
528 throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthNames[ td->length ] + " in type: ", td );
529 } // if
530
531 ret = chartype[ td->signedness ];
532 break;
533
534 case DeclarationNode::Int:
535 static BasicType::Kind inttype[2][4] = {
536 { BasicType::ShortSignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt },
537 { BasicType::ShortUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt },
538 };
539
540 Integral: ;
541 if ( td->signedness == DeclarationNode::NoSignedness ) {
542 const_cast<TypeData *>(td)->signedness = DeclarationNode::Signed;
543 } // if
544 ret = inttype[ td->signedness ][ td->length ];
545 break;
546
547 case DeclarationNode::Float:
548 case DeclarationNode::Double:
549 case DeclarationNode::LongDouble: // not set until below
550 static BasicType::Kind floattype[3][3] = {
551 { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },
552 { BasicType::FloatImaginary, BasicType::DoubleImaginary, BasicType::LongDoubleImaginary },
553 { BasicType::Float, BasicType::Double, BasicType::LongDouble },
554 };
555
556 FloatingPoint: ;
557 if ( td->signedness != DeclarationNode::NoSignedness ) {
558 throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::signednessNames[ td->signedness ] + " in type: ", td );
559 } // if
560 if ( td->length == DeclarationNode::Short || td->length == DeclarationNode::LongLong ) {
561 throw SemanticError( string( "invalid type specifier " ) + DeclarationNode::lengthNames[ td->length ] + " in type: ", td );
562 } // if
563 if ( td->basictype == DeclarationNode::Float && td->length == DeclarationNode::Long ) {
564 throw SemanticError( "invalid type specifier \"long\" in type: ", td );
565 } // if
566 if ( td->length == DeclarationNode::Long ) {
567 const_cast<TypeData *>(td)->basictype = DeclarationNode::LongDouble;
568 } // if
569
570 ret = floattype[ td->complextype ][ td->basictype - DeclarationNode::Float ];
571 break;
572
573 case DeclarationNode::NoBasicType:
574 // No basic type in declaration => default double for Complex/Imaginary and int type for integral types
575 if ( td->complextype == DeclarationNode::Complex || td->complextype == DeclarationNode::Imaginary ) {
576 const_cast<TypeData *>(td)->basictype = DeclarationNode::Double;
577 goto FloatingPoint;
578 } // if
579
580 const_cast<TypeData *>(td)->basictype = DeclarationNode::Int;
581 goto Integral;
582 default:
583 assert(false);
584 return nullptr;
585 } // switch
586
587 BasicType * bt = new BasicType( buildQualifiers( td ), ret );
588 buildForall( td->forall, bt->get_forall() );
589 return bt;
590} // buildBasicType
591
592PointerType * buildPointer( const TypeData * td ) {
593 PointerType * pt;
594 if ( td->base ) {
595 pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) );
596 } else {
597 pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
598 } // if
599 buildForall( td->forall, pt->get_forall() );
600 return pt;
601} // buildPointer
602
603ArrayType * buildArray( const TypeData * td ) {
604 ArrayType * at;
605 if ( td->base ) {
606 at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild< Expression >( td->array.dimension ),
607 td->array.isVarLen, td->array.isStatic );
608 } else {
609 at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
610 maybeBuild< Expression >( td->array.dimension ), td->array.isVarLen, td->array.isStatic );
611 } // if
612 buildForall( td->forall, at->get_forall() );
613 return at;
614} // buildPointer
615
616AggregateDecl * buildAggregate( const TypeData * td, std::list< Attribute * > attributes ) {
617 assert( td->kind == TypeData::Aggregate );
618 AggregateDecl * at;
619 switch ( td->aggregate.kind ) {
620 case DeclarationNode::Struct:
621 at = new StructDecl( *td->aggregate.name, attributes );
622 buildForall( td->aggregate.params, at->get_parameters() );
623 break;
624 case DeclarationNode::Union:
625 at = new UnionDecl( *td->aggregate.name, attributes );
626 buildForall( td->aggregate.params, at->get_parameters() );
627 break;
628 case DeclarationNode::Trait:
629 at = new TraitDecl( *td->aggregate.name, attributes );
630 buildList( td->aggregate.params, at->get_parameters() );
631 break;
632 default:
633 assert( false );
634 } // switch
635
636 buildList( td->aggregate.fields, at->get_members() );
637 at->set_body( td->aggregate.body );
638
639 return at;
640} // buildAggregate
641
642ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes ) {
643 switch ( type->kind ) {
644 case TypeData::Enum: {
645 if ( type->enumeration.body ) {
646 EnumDecl * typedecl = buildEnum( type, attributes );
647 return new EnumInstType( buildQualifiers( type ), typedecl );
648 } else {
649 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
650 } // if
651 }
652 case TypeData::Aggregate: {
653 ReferenceToType * ret;
654 if ( type->aggregate.body ) {
655 AggregateDecl * typedecl = buildAggregate( type, attributes );
656 switch ( type->aggregate.kind ) {
657 case DeclarationNode::Struct:
658 ret = new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl );
659 break;
660 case DeclarationNode::Union:
661 ret = new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl );
662 break;
663 case DeclarationNode::Trait:
664 assert( false );
665 //ret = new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl );
666 break;
667 default:
668 assert( false );
669 } // switch
670 } else {
671 switch ( type->aggregate.kind ) {
672 case DeclarationNode::Struct:
673 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
674 break;
675 case DeclarationNode::Union:
676 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
677 break;
678 case DeclarationNode::Trait:
679 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
680 break;
681 default:
682 assert( false );
683 } // switch
684 } // if
685 return ret;
686 }
687 default:
688 assert( false );
689 } // switch
690} // buildAggInst
691
692ReferenceToType * buildAggInst( const TypeData * td ) {
693 assert( td->kind == TypeData::AggregateInst );
694
695 // ReferenceToType * ret = buildComAggInst( td->aggInst.aggregate, std::list< Attribute * >() );
696 ReferenceToType * ret = nullptr;
697 TypeData * type = td->aggInst.aggregate;
698 switch ( type->kind ) {
699 case TypeData::Enum: {
700 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
701 }
702 case TypeData::Aggregate: {
703 switch ( type->aggregate.kind ) {
704 case DeclarationNode::Struct:
705 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
706 break;
707 case DeclarationNode::Union:
708 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
709 break;
710 case DeclarationNode::Trait:
711 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
712 break;
713 default:
714 assert( false );
715 } // switch
716 }
717 break;
718 default:
719 assert( false );
720 } // switch
721
722 ret->set_hoistType( td->aggInst.hoistType );
723 buildList( td->aggInst.params, ret->get_parameters() );
724 buildForall( td->forall, ret->get_forall() );
725 return ret;
726} // buildAggInst
727
728NamedTypeDecl * buildSymbolic( const TypeData * td, const string & name, Type::StorageClasses scs ) {
729 assert( td->kind == TypeData::Symbolic );
730 NamedTypeDecl * ret;
731 assert( td->base );
732 if ( td->symbolic.isTypedef ) {
733 ret = new TypedefDecl( name, scs, typebuild( td->base ) );
734 } else {
735 ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Any );
736 } // if
737 buildList( td->symbolic.params, ret->get_parameters() );
738 buildList( td->symbolic.assertions, ret->get_assertions() );
739 return ret;
740} // buildSymbolic
741
742EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes ) {
743 assert( td->kind == TypeData::Enum );
744 EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes );
745 buildList( td->enumeration.constants, ret->get_members() );
746 list< Declaration * >::iterator members = ret->get_members().begin();
747 for ( const DeclarationNode * cur = td->enumeration. constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
748 if ( cur->has_enumeratorValue() ) {
749 ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
750 member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ), list< Expression * >() ) );
751 } // if
752 } // for
753 ret->set_body( td->enumeration.body );
754 return ret;
755} // buildEnum
756
757TypeInstType * buildSymbolicInst( const TypeData * td ) {
758 assert( td->kind == TypeData::SymbolicInst );
759 TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false );
760 buildList( td->symbolic.actuals, ret->get_parameters() );
761 buildForall( td->forall, ret->get_forall() );
762 return ret;
763} // buildSymbolicInst
764
765TupleType * buildTuple( const TypeData * td ) {
766 assert( td->kind == TypeData::Tuple );
767 TupleType * ret = new TupleType( buildQualifiers( td ) );
768 buildTypeList( td->tuple, ret->get_types() );
769 buildForall( td->forall, ret->get_forall() );
770 return ret;
771} // buildTuple
772
773TypeofType * buildTypeof( const TypeData * td ) {
774 assert( td->kind == TypeData::Typeof );
775 assert( td->typeexpr );
776 // assert( td->typeexpr->expr );
777 return new TypeofType( buildQualifiers( td ), td->typeexpr->build() );
778} // buildTypeof
779
780Declaration * buildDecl( const TypeData * td, const string &name, Type::StorageClasses scs, Expression * bitfieldWidth, Type::FuncSpecifiers funcSpec, LinkageSpec::Spec linkage, ConstantExpr *asmName, Initializer * init, std::list< Attribute * > attributes ) {
781 if ( td->kind == TypeData::Function ) {
782 if ( td->function.idList ) { // KR function ?
783 buildKRFunction( td->function ); // transform into C11 function
784 } // if
785
786 FunctionDecl * decl;
787 Statement * stmt = maybeBuild<Statement>( td->function.body );
788 CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt );
789 decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec );
790 return decl->set_asmName( asmName );
791 } else if ( td->kind == TypeData::Aggregate ) {
792 return buildAggregate( td, attributes );
793 } else if ( td->kind == TypeData::Enum ) {
794 return buildEnum( td, attributes );
795 } else if ( td->kind == TypeData::Symbolic ) {
796 return buildSymbolic( td, name, scs );
797 } else {
798 return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName );
799 } // if
800 return nullptr;
801} // buildDecl
802
803FunctionType * buildFunction( const TypeData * td ) {
804 assert( td->kind == TypeData::Function );
805 bool hasEllipsis = td->function.params ? td->function.params->get_hasEllipsis() : true;
806 if ( ! td->function.params ) hasEllipsis = ! td->function.newStyle;
807 FunctionType * ft = new FunctionType( buildQualifiers( td ), hasEllipsis );
808 buildList( td->function.params, ft->get_parameters() );
809 buildForall( td->forall, ft->get_forall() );
810 if ( td->base ) {
811 switch ( td->base->kind ) {
812 case TypeData::Tuple:
813 buildList( td->base->tuple, ft->get_returnVals() );
814 break;
815 default:
816 ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType * >( buildDecl( td->base, "", Type::StorageClasses(), nullptr, Type::FuncSpecifiers(), LinkageSpec::Cforall, nullptr ) ) );
817 } // switch
818 } else {
819 ft->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) );
820 } // if
821 return ft;
822} // buildFunction
823
824// Transform KR routine declarations into C99 routine declarations:
825//
826// rtn( a, b, c ) int a, c; double b {} => int rtn( int a, double c, int b ) {}
827//
828// The type information for each post-declaration is moved to the corresponding pre-parameter and the post-declaration
829// is deleted. Note, the order of the parameter names may not be the same as the declaration names. Duplicate names and
830// extra names are disallowed.
831//
832// Note, there is no KR routine-prototype syntax:
833//
834// rtn( a, b, c ) int a, c; double b; // invalid KR prototype
835// rtn(); // valid KR prototype
836
837void buildKRFunction( const TypeData::Function_t & function ) {
838 assert( ! function.params );
839 // loop over declaration first as it is easier to spot errors
840 for ( DeclarationNode * decl = function.oldDeclList; decl != nullptr; decl = dynamic_cast< DeclarationNode * >( decl->get_next() ) ) {
841 // scan ALL parameter names for each declaration name to check for duplicates
842 for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
843 if ( *decl->name == *param->name ) {
844 // type set => parameter name already transformed by a declaration names so there is a duplicate
845 // declaration name attempting a second transformation
846 if ( param->type ) throw SemanticError( string( "duplicate declaration name " ) + *param->name );
847 // declaration type reset => declaration already transformed by a parameter name so there is a duplicate
848 // parameter name attempting a second transformation
849 if ( ! decl->type ) throw SemanticError( string( "duplicate parameter name " ) + *param->name );
850 param->type = decl->type; // set copy declaration type to parameter type
851 decl->type = nullptr; // reset declaration type
852 param->attributes.splice( param->attributes.end(), decl->attributes ); // copy and reset attributes from declaration to parameter
853 } // if
854 } // for
855 // declaration type still set => type not moved to a matching parameter so there is a missing parameter name
856 if ( decl->type ) throw SemanticError( string( "missing name in parameter list " ) + *decl->name );
857 } // for
858
859 // Parameter names without a declaration default to type int:
860 //
861 // rtb( a, b, c ) const char * b; {} => int rtn( int a, const char * b, int c ) {}
862
863 for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
864 if ( ! param->type ) { // generate type int for empty parameter type
865 param->type = new TypeData( TypeData::Basic );
866 param->type->basictype = DeclarationNode::Int;
867 } // if
868 } // for
869
870 function.params = function.idList; // newly modified idList becomes parameters
871 function.idList = nullptr; // idList now empty
872 delete function.oldDeclList; // deletes entire list
873 function.oldDeclList = nullptr; // reset
874} // buildKRFunction
875
876// Local Variables: //
877// tab-width: 4 //
878// mode: c++ //
879// compile-command: "make install" //
880// End: //
Note: See TracBrowser for help on using the repository browser.