source: src/Parser/TypeData.cc@ 7edd5c1

ADT ast-experimental pthread-emulation qualifiedEnum
Last change on this file since 7edd5c1 was 9e7236f4, checked in by JiadaL <j82liang@…>, 3 years ago

Resolution of struct enum. The codegen of struct enum will be in the next commit

  • Property mode set to 100644
File size: 37.4 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// TypeData.cc --
8//
9// Author : Rodolfo G. Esteves
10// Created On : Sat May 16 15:12:51 2015
11// Last Modified By : Henry Xue
12// Last Modified On : Tue Jul 20 04:10:50 2021
13// Update Count : 673
14//
15
16#include <cassert> // for assert
17#include <ostream> // for operator<<, ostream, basic_ostream
18
19#include "Common/SemanticError.h" // for SemanticError
20#include "Common/utility.h" // for maybeClone, maybeBuild, maybeMoveB...
21#include "Parser/ParseNode.h" // for DeclarationNode, ExpressionNode
22#include "SynTree/Declaration.h" // for TypeDecl, ObjectDecl, FunctionDecl
23#include "SynTree/Expression.h" // for Expression, ConstantExpr (ptr only)
24#include "SynTree/Initializer.h" // for SingleInit, Initializer (ptr only)
25#include "SynTree/Statement.h" // for CompoundStmt, Statement
26#include "SynTree/Type.h" // for BasicType, Type, Type::ForallList
27#include "TypeData.h"
28
29class Attribute;
30
31using namespace std;
32
33TypeData::TypeData( Kind k ) : location( yylloc ), kind( k ), base( nullptr ), forall( nullptr ) /*, PTR1( (void*)(0xdeadbeefdeadbeef)), PTR2( (void*)(0xdeadbeefdeadbeef) ) */ {
34 switch ( kind ) {
35 case Unknown:
36 case Pointer:
37 case Reference:
38 case EnumConstant:
39 case GlobalScope:
40 // nothing else to initialize
41 break;
42 case Basic:
43 // basic = new Basic_t;
44 break;
45 case Array:
46 // array = new Array_t;
47 array.dimension = nullptr;
48 array.isVarLen = false;
49 array.isStatic = false;
50 break;
51 case Function:
52 // function = new Function_t;
53 function.params = nullptr;
54 function.idList = nullptr;
55 function.oldDeclList = nullptr;
56 function.body = nullptr;
57 function.withExprs = nullptr;
58 break;
59 // Enum is an Aggregate, so both structures are initialized together.
60 case Enum:
61 // enumeration = new Enumeration_t;
62 enumeration.name = nullptr;
63 enumeration.constants = nullptr;
64 enumeration.body = false;
65 enumeration.anon = false;
66 break;
67 case Aggregate:
68 // aggregate = new Aggregate_t;
69 aggregate.kind = AggregateDecl::NoAggregate;
70 aggregate.name = nullptr;
71 aggregate.params = nullptr;
72 aggregate.actuals = nullptr;
73 aggregate.fields = nullptr;
74 aggregate.body = false;
75 aggregate.tagged = false;
76 aggregate.parent = nullptr;
77 aggregate.anon = false;
78 break;
79 case AggregateInst:
80 // aggInst = new AggInst_t;
81 aggInst.aggregate = nullptr;
82 aggInst.params = nullptr;
83 aggInst.hoistType = false;
84 break;
85 case Symbolic:
86 case SymbolicInst:
87 // symbolic = new Symbolic_t;
88 symbolic.name = nullptr;
89 symbolic.params = nullptr;
90 symbolic.actuals = nullptr;
91 symbolic.assertions = nullptr;
92 break;
93 case Tuple:
94 // tuple = new Tuple_t;
95 tuple = nullptr;
96 break;
97 case Typeof:
98 case Basetypeof:
99 // typeexpr = new Typeof_t;
100 typeexpr = nullptr;
101 break;
102 case Vtable:
103 break;
104 case Builtin:
105 // builtin = new Builtin_t;
106 case Qualified:
107 qualified.parent = nullptr;
108 qualified.child = nullptr;
109 break;
110 } // switch
111} // TypeData::TypeData
112
113
114TypeData::~TypeData() {
115 delete base;
116 delete forall;
117
118 switch ( kind ) {
119 case Unknown:
120 case Pointer:
121 case Reference:
122 case EnumConstant:
123 case GlobalScope:
124 // nothing to destroy
125 break;
126 case Basic:
127 // delete basic;
128 break;
129 case Array:
130 delete array.dimension;
131 // delete array;
132 break;
133 case Function:
134 delete function.params;
135 delete function.idList;
136 delete function.oldDeclList;
137 delete function.body;
138 delete function.withExprs;
139 // delete function;
140 break;
141 case Aggregate:
142 delete aggregate.name;
143 delete aggregate.params;
144 delete aggregate.actuals;
145 delete aggregate.fields;
146 // delete aggregate;
147 break;
148 case AggregateInst:
149 delete aggInst.aggregate;
150 delete aggInst.params;
151 // delete aggInst;
152 break;
153 case Enum:
154 delete enumeration.name;
155 delete enumeration.constants;
156 // delete enumeration;
157 break;
158 case Symbolic:
159 case SymbolicInst:
160 delete symbolic.name;
161 delete symbolic.params;
162 delete symbolic.actuals;
163 delete symbolic.assertions;
164 // delete symbolic;
165 break;
166 case Tuple:
167 // delete tuple->members;
168 delete tuple;
169 break;
170 case Typeof:
171 case Basetypeof:
172 // delete typeexpr->expr;
173 delete typeexpr;
174 break;
175 case Vtable:
176 break;
177 case Builtin:
178 // delete builtin;
179 break;
180 case Qualified:
181 delete qualified.parent;
182 delete qualified.child;
183 } // switch
184} // TypeData::~TypeData
185
186
187TypeData * TypeData::clone() const {
188 TypeData * newtype = new TypeData( kind );
189 newtype->qualifiers = qualifiers;
190 newtype->base = maybeClone( base );
191 newtype->forall = maybeClone( forall );
192
193 switch ( kind ) {
194 case Unknown:
195 case EnumConstant:
196 case Pointer:
197 case Reference:
198 case GlobalScope:
199 // nothing else to copy
200 break;
201 case Basic:
202 newtype->basictype = basictype;
203 newtype->complextype = complextype;
204 newtype->signedness = signedness;
205 newtype->length = length;
206 break;
207 case Array:
208 newtype->array.dimension = maybeClone( array.dimension );
209 newtype->array.isVarLen = array.isVarLen;
210 newtype->array.isStatic = array.isStatic;
211 break;
212 case Function:
213 newtype->function.params = maybeClone( function.params );
214 newtype->function.idList = maybeClone( function.idList );
215 newtype->function.oldDeclList = maybeClone( function.oldDeclList );
216 newtype->function.body = maybeClone( function.body );
217 newtype->function.withExprs = maybeClone( function.withExprs );
218 break;
219 case Aggregate:
220 newtype->aggregate.kind = aggregate.kind;
221 newtype->aggregate.name = aggregate.name ? new string( *aggregate.name ) : nullptr;
222 newtype->aggregate.params = maybeClone( aggregate.params );
223 newtype->aggregate.actuals = maybeClone( aggregate.actuals );
224 newtype->aggregate.fields = maybeClone( aggregate.fields );
225 newtype->aggregate.body = aggregate.body;
226 newtype->aggregate.anon = aggregate.anon;
227 newtype->aggregate.tagged = aggregate.tagged;
228 newtype->aggregate.parent = aggregate.parent ? new string( *aggregate.parent ) : nullptr;
229 break;
230 case AggregateInst:
231 newtype->aggInst.aggregate = maybeClone( aggInst.aggregate );
232 newtype->aggInst.params = maybeClone( aggInst.params );
233 newtype->aggInst.hoistType = aggInst.hoistType;
234 break;
235 case Enum:
236 newtype->enumeration.name = enumeration.name ? new string( *enumeration.name ) : nullptr;
237 newtype->enumeration.constants = maybeClone( enumeration.constants );
238 newtype->enumeration.body = enumeration.body;
239 newtype->enumeration.anon = enumeration.anon;
240 break;
241 case Symbolic:
242 case SymbolicInst:
243 newtype->symbolic.name = symbolic.name ? new string( *symbolic.name ) : nullptr;
244 newtype->symbolic.params = maybeClone( symbolic.params );
245 newtype->symbolic.actuals = maybeClone( symbolic.actuals );
246 newtype->symbolic.assertions = maybeClone( symbolic.assertions );
247 newtype->symbolic.isTypedef = symbolic.isTypedef;
248 break;
249 case Tuple:
250 newtype->tuple = maybeClone( tuple );
251 break;
252 case Typeof:
253 case Basetypeof:
254 newtype->typeexpr = maybeClone( typeexpr );
255 break;
256 case Vtable:
257 break;
258 case Builtin:
259 assert( builtintype == DeclarationNode::Zero || builtintype == DeclarationNode::One );
260 newtype->builtintype = builtintype;
261 break;
262 case Qualified:
263 newtype->qualified.parent = maybeClone( qualified.parent );
264 newtype->qualified.child = maybeClone( qualified.child );
265 break;
266 } // switch
267 return newtype;
268} // TypeData::clone
269
270
271void TypeData::print( ostream &os, int indent ) const {
272 for ( int i = 0; i < Type::NumTypeQualifier; i += 1 ) {
273 if ( qualifiers[i] ) os << Type::QualifiersNames[ i ] << ' ';
274 } // for
275
276 if ( forall ) {
277 os << "forall " << endl;
278 forall->printList( os, indent + 4 );
279 } // if
280
281 switch ( kind ) {
282 case Basic:
283 if ( signedness != DeclarationNode::NoSignedness ) os << DeclarationNode::signednessNames[ signedness ] << " ";
284 if ( length != DeclarationNode::NoLength ) os << DeclarationNode::lengthNames[ length ] << " ";
285 if ( complextype == DeclarationNode::NoComplexType ) { // basic type
286 assert( basictype != DeclarationNode::NoBasicType );
287 os << DeclarationNode::basicTypeNames[ basictype ] << " ";
288 } else { // complex type
289 // handle double _Complex
290 if ( basictype != DeclarationNode::NoBasicType ) os << DeclarationNode::basicTypeNames[ basictype ] << " ";
291 os << DeclarationNode::complexTypeNames[ complextype ] << " ";
292 } // if
293 break;
294 case Pointer:
295 os << "pointer ";
296 if ( base ) {
297 os << "to ";
298 base->print( os, indent );
299 } // if
300 break;
301 case Reference:
302 os << "reference ";
303 if ( base ) {
304 os << "to ";
305 base->print( os, indent );
306 } // if
307 break;
308 case Array:
309 if ( array.isStatic ) {
310 os << "static ";
311 } // if
312 if ( array.dimension ) {
313 os << "array of ";
314 array.dimension->printOneLine( os, indent );
315 } else if ( array.isVarLen ) {
316 os << "variable-length array of ";
317 } else {
318 os << "open array of ";
319 } // if
320 if ( base ) {
321 base->print( os, indent );
322 } // if
323 break;
324 case Function:
325 os << "function" << endl;
326 if ( function.params ) {
327 os << string( indent + 2, ' ' ) << "with parameters " << endl;
328 function.params->printList( os, indent + 4 );
329 } else {
330 os << string( indent + 2, ' ' ) << "with no parameters" << endl;
331 } // if
332 if ( function.idList ) {
333 os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl;
334 function.idList->printList( os, indent + 4 );
335 } // if
336 if ( function.oldDeclList ) {
337 os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl;
338 function.oldDeclList->printList( os, indent + 4 );
339 } // if
340 os << string( indent + 2, ' ' ) << "returning ";
341 if ( base ) {
342 base->print( os, indent + 4 );
343 } else {
344 os << "nothing ";
345 } // if
346 os << endl;
347 if ( function.body ) {
348 os << string( indent + 2, ' ' ) << "with body " << endl;
349 function.body->printList( os, indent + 2 );
350 } // if
351 break;
352 case Aggregate:
353 os << AggregateDecl::aggrString( aggregate.kind ) << ' ' << *aggregate.name << endl;
354 if ( aggregate.params ) {
355 os << string( indent + 2, ' ' ) << "with type parameters" << endl;
356 aggregate.params->printList( os, indent + 4 );
357 } // if
358 if ( aggregate.actuals ) {
359 os << string( indent + 2, ' ' ) << "instantiated with actual parameters" << endl;
360 aggregate.actuals->printList( os, indent + 4 );
361 } // if
362 if ( aggregate.fields ) {
363 os << string( indent + 2, ' ' ) << "with members" << endl;
364 aggregate.fields->printList( os, indent + 4 );
365 } // if
366 if ( aggregate.body ) {
367 os << string( indent + 2, ' ' ) << " with body" << endl;
368 } // if
369 break;
370 case AggregateInst:
371 if ( aggInst.aggregate ) {
372 os << "instance of " ;
373 aggInst.aggregate->print( os, indent );
374 } else {
375 os << "instance of an unspecified aggregate ";
376 } // if
377 if ( aggInst.params ) {
378 os << string( indent + 2, ' ' ) << "with parameters" << endl;
379 aggInst.params->printList( os, indent + 2 );
380 } // if
381 break;
382 case Enum:
383 os << "enumeration ";
384 if ( enumeration.constants ) {
385 os << "with constants" << endl;
386 enumeration.constants->printList( os, indent + 2 );
387 } // if
388 if ( enumeration.body ) {
389 os << string( indent + 2, ' ' ) << " with body" << endl;
390 } // if
391 if ( base ) {
392 os << "for ";
393 base->print( os, indent + 2 );
394 } // if
395 break;
396 case EnumConstant:
397 os << "enumeration constant ";
398 break;
399 case Symbolic:
400 if ( symbolic.isTypedef ) {
401 os << "typedef definition ";
402 } else {
403 os << "type definition ";
404 } // if
405 if ( symbolic.params ) {
406 os << endl << string( indent + 2, ' ' ) << "with parameters" << endl;
407 symbolic.params->printList( os, indent + 2 );
408 } // if
409 if ( symbolic.assertions ) {
410 os << endl << string( indent + 2, ' ' ) << "with assertions" << endl;
411 symbolic.assertions->printList( os, indent + 4 );
412 os << string( indent + 2, ' ' );
413 } // if
414 if ( base ) {
415 os << "for ";
416 base->print( os, indent + 2 );
417 } // if
418 break;
419 case SymbolicInst:
420 os << *symbolic.name;
421 if ( symbolic.actuals ) {
422 os << "(";
423 symbolic.actuals->printList( os, indent + 2 );
424 os << ")";
425 } // if
426 break;
427 case Tuple:
428 os << "tuple ";
429 if ( tuple ) {
430 os << "with members" << endl;
431 tuple->printList( os, indent + 2 );
432 } // if
433 break;
434 case Basetypeof:
435 os << "base-";
436 #if defined(__GNUC__) && __GNUC__ >= 7
437 __attribute__((fallthrough));
438 #endif
439 case Typeof:
440 os << "type-of expression ";
441 if ( typeexpr ) {
442 typeexpr->print( os, indent + 2 );
443 } // if
444 break;
445 case Builtin:
446 os << DeclarationNode::builtinTypeNames[builtintype];
447 break;
448 case GlobalScope:
449 break;
450 case Qualified:
451 qualified.parent->print( os );
452 os << ".";
453 qualified.child->print( os );
454 break;
455 case Unknown:
456 os << "entity of unknown type ";
457 break;
458 default:
459 os << "internal error: TypeData::print " << kind << endl;
460 assert( false );
461 } // switch
462} // TypeData::print
463
464const std::string * TypeData::leafName() const {
465 switch ( kind ) {
466 case Unknown:
467 case Pointer:
468 case Reference:
469 case EnumConstant:
470 case GlobalScope:
471 case Array:
472 case Basic:
473 case Function:
474 case AggregateInst:
475 case Tuple:
476 case Typeof:
477 case Basetypeof:
478 case Builtin:
479 case Vtable:
480 assertf(false, "Tried to get leaf name from kind without a name: %d", kind);
481 break;
482 case Aggregate:
483 return aggregate.name;
484 case Enum:
485 return enumeration.name;
486 case Symbolic:
487 case SymbolicInst:
488 return symbolic.name;
489 case Qualified:
490 return qualified.child->leafName();
491 } // switch
492 assert(false);
493}
494
495
496template< typename ForallList >
497void buildForall( const DeclarationNode * firstNode, ForallList &outputList ) {
498 buildList( firstNode, outputList );
499 auto n = firstNode;
500 for ( typename ForallList::iterator i = outputList.begin(); i != outputList.end(); ++i, n = (DeclarationNode*)n->get_next() ) {
501 TypeDecl * td = static_cast<TypeDecl *>(*i);
502 if ( n->variable.tyClass == TypeDecl::Otype ) {
503 // add assertion parameters to `type' tyvars in reverse order
504 // add dtor: void ^?{}(T *)
505 FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false );
506 dtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
507 td->get_assertions().push_front( new FunctionDecl( "^?{}", Type::StorageClasses(), LinkageSpec::Cforall, dtorType, nullptr ) );
508
509 // add copy ctor: void ?{}(T *, T)
510 FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false );
511 copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
512 copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
513 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, copyCtorType, nullptr ) );
514
515 // add default ctor: void ?{}(T *)
516 FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false );
517 ctorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
518 td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, ctorType, nullptr ) );
519
520 // add assignment operator: T * ?=?(T *, T)
521 FunctionType * assignType = new FunctionType( Type::Qualifiers(), false );
522 assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new ReferenceType( Type::Qualifiers(), new TypeInstType( Type::Qualifiers(), td->get_name(), *i ) ), nullptr ) );
523 assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
524 assignType->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
525 td->get_assertions().push_front( new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Cforall, assignType, nullptr ) );
526 } // if
527 } // for
528} // buildForall
529
530
531Type * typebuild( const TypeData * td ) {
532 assert( td );
533 switch ( td->kind ) {
534 case TypeData::Unknown:
535 // fill in implicit int
536 return new BasicType( buildQualifiers( td ), BasicType::SignedInt );
537 case TypeData::Basic:
538 return buildBasicType( td );
539 case TypeData::Pointer:
540 return buildPointer( td );
541 case TypeData::Array:
542 return buildArray( td );
543 case TypeData::Reference:
544 return buildReference( td );
545 case TypeData::Function:
546 return buildFunction( td );
547 case TypeData::AggregateInst:
548 return buildAggInst( td );
549 case TypeData::EnumConstant:
550 // the name gets filled in later -- by SymTab::Validate
551 return new EnumInstType( buildQualifiers( td ), "" );
552 case TypeData::SymbolicInst:
553 return buildSymbolicInst( td );
554 case TypeData::Tuple:
555 return buildTuple( td );
556 case TypeData::Typeof:
557 case TypeData::Basetypeof:
558 return buildTypeof( td );
559 case TypeData::Vtable:
560 return buildVtable( td );
561 case TypeData::Builtin:
562 switch ( td->builtintype ) {
563 case DeclarationNode::Zero:
564 return new ZeroType( noQualifiers );
565 case DeclarationNode::One:
566 return new OneType( noQualifiers );
567 default:
568 return new VarArgsType( buildQualifiers( td ) );
569 } // switch
570 case TypeData::GlobalScope:
571 return new GlobalScopeType();
572 case TypeData::Qualified:
573 return new QualifiedType( buildQualifiers( td ), typebuild( td->qualified.parent ), typebuild( td->qualified.child ) );
574 case TypeData::Symbolic:
575 case TypeData::Enum:
576 case TypeData::Aggregate:
577 assert( false );
578 } // switch
579
580 return nullptr;
581} // typebuild
582
583
584TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) {
585 TypeData * ret = nullptr;
586
587 switch ( td->kind ) {
588 case TypeData::Aggregate:
589 if ( ! toplevel && td->aggregate.body ) {
590 ret = td->clone();
591 } // if
592 break;
593 case TypeData::Enum:
594 if ( ! toplevel && td->enumeration.body ) {
595 ret = td->clone();
596 } // if
597 break;
598 case TypeData::AggregateInst:
599 if ( td->aggInst.aggregate ) {
600 ret = typeextractAggregate( td->aggInst.aggregate, false );
601 } // if
602 break;
603 default:
604 if ( td->base ) {
605 ret = typeextractAggregate( td->base, false );
606 } // if
607 } // switch
608 return ret;
609} // typeextractAggregate
610
611
612Type::Qualifiers buildQualifiers( const TypeData * td ) {
613 return td->qualifiers;
614} // buildQualifiers
615
616
617static string genTSError( string msg, DeclarationNode::BasicType basictype ) {
618 SemanticError( yylloc, string( "invalid type specifier \"" ) + msg + "\" for type \"" + DeclarationNode::basicTypeNames[basictype] + "\"." );
619} // genTSError
620
621Type * buildBasicType( const TypeData * td ) {
622 BasicType::Kind ret;
623
624 switch ( td->basictype ) {
625 case DeclarationNode::Void:
626 if ( td->signedness != DeclarationNode::NoSignedness ) {
627 genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
628 } // if
629 if ( td->length != DeclarationNode::NoLength ) {
630 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
631 } // if
632 return new VoidType( buildQualifiers( td ) );
633 break;
634
635 case DeclarationNode::Bool:
636 if ( td->signedness != DeclarationNode::NoSignedness ) {
637 genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
638 } // if
639 if ( td->length != DeclarationNode::NoLength ) {
640 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
641 } // if
642
643 ret = BasicType::Bool;
644 break;
645
646 case DeclarationNode::Char:
647 // C11 Standard 6.2.5.15: The three types char, signed char, and unsigned char are collectively called the
648 // character types. The implementation shall define char to have the same range, representation, and behavior as
649 // either signed char or unsigned char.
650 static BasicType::Kind chartype[] = { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::Char };
651
652 if ( td->length != DeclarationNode::NoLength ) {
653 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
654 } // if
655
656 ret = chartype[ td->signedness ];
657 break;
658
659 case DeclarationNode::Int:
660 static BasicType::Kind inttype[2][4] = {
661 { BasicType::ShortSignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt },
662 { BasicType::ShortUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt },
663 };
664
665 Integral: ;
666 if ( td->signedness == DeclarationNode::NoSignedness ) {
667 const_cast<TypeData *>(td)->signedness = DeclarationNode::Signed;
668 } // if
669 ret = inttype[ td->signedness ][ td->length ];
670 break;
671
672 case DeclarationNode::Int128:
673 ret = td->signedness == DeclarationNode::Unsigned ? BasicType::UnsignedInt128 : BasicType::SignedInt128;
674 if ( td->length != DeclarationNode::NoLength ) {
675 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
676 } // if
677 break;
678
679 case DeclarationNode::Float:
680 case DeclarationNode::Double:
681 case DeclarationNode::LongDouble: // not set until below
682 case DeclarationNode::uuFloat80:
683 case DeclarationNode::uuFloat128:
684 case DeclarationNode::uFloat16:
685 case DeclarationNode::uFloat32:
686 case DeclarationNode::uFloat32x:
687 case DeclarationNode::uFloat64:
688 case DeclarationNode::uFloat64x:
689 case DeclarationNode::uFloat128:
690 case DeclarationNode::uFloat128x:
691 static BasicType::Kind floattype[2][12] = {
692 { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, (BasicType::Kind)-1, (BasicType::Kind)-1, BasicType::uFloat16Complex, BasicType::uFloat32Complex, BasicType::uFloat32xComplex, BasicType::uFloat64Complex, BasicType::uFloat64xComplex, BasicType::uFloat128Complex, BasicType::uFloat128xComplex, },
693 { BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::uuFloat80, BasicType::uuFloat128, BasicType::uFloat16, BasicType::uFloat32, BasicType::uFloat32x, BasicType::uFloat64, BasicType::uFloat64x, BasicType::uFloat128, BasicType::uFloat128x, },
694 };
695
696 FloatingPoint: ;
697 if ( td->signedness != DeclarationNode::NoSignedness ) {
698 genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
699 } // if
700 if ( td->length == DeclarationNode::Short || td->length == DeclarationNode::LongLong ) {
701 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
702 } // if
703 if ( td->basictype != DeclarationNode::Double && td->length == DeclarationNode::Long ) {
704 genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
705 } // if
706 if ( td->complextype == DeclarationNode::Imaginary ) {
707 genTSError( DeclarationNode::complexTypeNames[ td->complextype ], td->basictype );
708 } // if
709 if ( (td->basictype == DeclarationNode::uuFloat80 || td->basictype == DeclarationNode::uuFloat128) && td->complextype == DeclarationNode::Complex ) { // gcc unsupported
710 genTSError( DeclarationNode::complexTypeNames[ td->complextype ], td->basictype );
711 } // if
712 if ( td->length == DeclarationNode::Long ) {
713 const_cast<TypeData *>(td)->basictype = DeclarationNode::LongDouble;
714 } // if
715
716 ret = floattype[ td->complextype ][ td->basictype - DeclarationNode::Float ];
717 //printf( "XXXX %d %d %d %d\n", td->complextype, td->basictype, DeclarationNode::Float, ret );
718 break;
719
720 case DeclarationNode::NoBasicType:
721 // No basic type in declaration => default double for Complex/Imaginary and int type for integral types
722 if ( td->complextype == DeclarationNode::Complex || td->complextype == DeclarationNode::Imaginary ) {
723 const_cast<TypeData *>(td)->basictype = DeclarationNode::Double;
724 goto FloatingPoint;
725 } // if
726
727 const_cast<TypeData *>(td)->basictype = DeclarationNode::Int;
728 goto Integral;
729 default:
730 assertf( false, "unknown basic type" );
731 return nullptr;
732 } // switch
733
734 BasicType * bt = new BasicType( buildQualifiers( td ), ret );
735 buildForall( td->forall, bt->get_forall() );
736 return bt;
737} // buildBasicType
738
739
740PointerType * buildPointer( const TypeData * td ) {
741 PointerType * pt;
742 if ( td->base ) {
743 pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) );
744 } else {
745 pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
746 } // if
747 buildForall( td->forall, pt->get_forall() );
748 return pt;
749} // buildPointer
750
751
752ArrayType * buildArray( const TypeData * td ) {
753 ArrayType * at;
754 if ( td->base ) {
755 at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild< Expression >( td->array.dimension ),
756 td->array.isVarLen, td->array.isStatic );
757 } else {
758 at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
759 maybeBuild< Expression >( td->array.dimension ), td->array.isVarLen, td->array.isStatic );
760 } // if
761 buildForall( td->forall, at->get_forall() );
762 return at;
763} // buildArray
764
765
766ReferenceType * buildReference( const TypeData * td ) {
767 ReferenceType * rt;
768 if ( td->base ) {
769 rt = new ReferenceType( buildQualifiers( td ), typebuild( td->base ) );
770 } else {
771 rt = new ReferenceType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
772 } // if
773 buildForall( td->forall, rt->get_forall() );
774 return rt;
775} // buildReference
776
777
778AggregateDecl * buildAggregate( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
779 assert( td->kind == TypeData::Aggregate );
780 AggregateDecl * at;
781 switch ( td->aggregate.kind ) {
782 case AggregateDecl::Struct:
783 case AggregateDecl::Coroutine:
784 case AggregateDecl::Exception:
785 case AggregateDecl::Generator:
786 case AggregateDecl::Monitor:
787 case AggregateDecl::Thread:
788 at = new StructDecl( *td->aggregate.name, td->aggregate.kind, attributes, linkage );
789 buildForall( td->aggregate.params, at->get_parameters() );
790 break;
791 case AggregateDecl::Union:
792 at = new UnionDecl( *td->aggregate.name, attributes, linkage );
793 buildForall( td->aggregate.params, at->get_parameters() );
794 break;
795 case AggregateDecl::Trait:
796 at = new TraitDecl( *td->aggregate.name, attributes, linkage );
797 buildList( td->aggregate.params, at->get_parameters() );
798 break;
799 default:
800 assert( false );
801 } // switch
802
803 buildList( td->aggregate.fields, at->get_members() );
804 at->set_body( td->aggregate.body );
805
806 return at;
807} // buildAggregate
808
809
810ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
811 switch ( type->kind ) {
812 case TypeData::Enum: {
813 if ( type->enumeration.body ) {
814 EnumDecl * typedecl = buildEnum( type, attributes, linkage );
815 return new EnumInstType( buildQualifiers( type ), typedecl );
816 } else {
817 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
818 } // if
819 }
820 case TypeData::Aggregate: {
821 ReferenceToType * ret;
822 if ( type->aggregate.body ) {
823 AggregateDecl * typedecl = buildAggregate( type, attributes, linkage );
824 switch ( type->aggregate.kind ) {
825 case AggregateDecl::Struct:
826 case AggregateDecl::Coroutine:
827 case AggregateDecl::Monitor:
828 case AggregateDecl::Thread:
829 ret = new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl );
830 break;
831 case AggregateDecl::Union:
832 ret = new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl );
833 break;
834 case AggregateDecl::Trait:
835 assert( false );
836 //ret = new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl );
837 break;
838 default:
839 assert( false );
840 } // switch
841 } else {
842 switch ( type->aggregate.kind ) {
843 case AggregateDecl::Struct:
844 case AggregateDecl::Coroutine:
845 case AggregateDecl::Monitor:
846 case AggregateDecl::Thread:
847 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
848 break;
849 case AggregateDecl::Union:
850 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
851 break;
852 case AggregateDecl::Trait:
853 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
854 break;
855 default:
856 assert( false );
857 } // switch
858 } // if
859 return ret;
860 }
861 default:
862 assert( false );
863 } // switch
864} // buildAggInst
865
866
867ReferenceToType * buildAggInst( const TypeData * td ) {
868 assert( td->kind == TypeData::AggregateInst );
869
870 // ReferenceToType * ret = buildComAggInst( td->aggInst.aggregate, std::list< Attribute * >() );
871 ReferenceToType * ret = nullptr;
872 TypeData * type = td->aggInst.aggregate;
873 switch ( type->kind ) {
874 case TypeData::Enum: {
875 return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
876 }
877 case TypeData::Aggregate: {
878 switch ( type->aggregate.kind ) {
879 case AggregateDecl::Struct:
880 case AggregateDecl::Coroutine:
881 case AggregateDecl::Monitor:
882 case AggregateDecl::Thread:
883 ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
884 break;
885 case AggregateDecl::Union:
886 ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
887 break;
888 case AggregateDecl::Trait:
889 ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
890 break;
891 default:
892 assert( false );
893 } // switch
894 }
895 break;
896 default:
897 assert( false );
898 } // switch
899
900 ret->set_hoistType( td->aggInst.hoistType );
901 buildList( td->aggInst.params, ret->get_parameters() );
902 buildForall( td->forall, ret->get_forall() );
903 return ret;
904} // buildAggInst
905
906
907NamedTypeDecl * buildSymbolic( const TypeData * td, std::list< Attribute * > attributes, const string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage ) {
908 assert( td->kind == TypeData::Symbolic );
909 NamedTypeDecl * ret;
910 assert( td->base );
911 if ( td->symbolic.isTypedef ) {
912 ret = new TypedefDecl( name, td->location, scs, typebuild( td->base ), linkage );
913 } else {
914 ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Dtype, true );
915 } // if
916 buildList( td->symbolic.assertions, ret->get_assertions() );
917 ret->base->attributes.splice( ret->base->attributes.end(), attributes );
918 return ret;
919} // buildSymbolic
920
921
922EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
923 assert( td->kind == TypeData::Enum );
924 Type * baseType = td->base ? typebuild(td->base) : nullptr;
925 EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes, linkage, baseType );
926 buildList( td->enumeration.constants, ret->get_members() );
927 list< Declaration * >::iterator members = ret->get_members().begin();
928 for ( const DeclarationNode * cur = td->enumeration.constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
929 if ( cur->has_enumeratorValue() ) {
930 ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
931 member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ) ) );
932 } else if ( !cur->initializer ) {
933 if ( baseType && (!dynamic_cast<BasicType *>(baseType) || !dynamic_cast<BasicType *>(baseType)->isWholeNumber())) {
934 SemanticError( td->location, "A non whole number enum value decl must be explicitly initialized." );
935 }
936 }
937 // else cur is a List Initializer and has been set as init in buildList()
938 // if
939 } // for
940 ret->set_body( td->enumeration.body );
941 return ret;
942} // buildEnum
943
944
945TypeInstType * buildSymbolicInst( const TypeData * td ) {
946 assert( td->kind == TypeData::SymbolicInst );
947 TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false );
948 buildList( td->symbolic.actuals, ret->get_parameters() );
949 buildForall( td->forall, ret->get_forall() );
950 return ret;
951} // buildSymbolicInst
952
953
954TupleType * buildTuple( const TypeData * td ) {
955 assert( td->kind == TypeData::Tuple );
956 std::list< Type * > types;
957 buildTypeList( td->tuple, types );
958 TupleType * ret = new TupleType( buildQualifiers( td ), types );
959 buildForall( td->forall, ret->get_forall() );
960 return ret;
961} // buildTuple
962
963
964TypeofType * buildTypeof( const TypeData * td ) {
965 assert( td->kind == TypeData::Typeof || td->kind == TypeData::Basetypeof );
966 assert( td->typeexpr );
967 // assert( td->typeexpr->expr );
968 return new TypeofType{ buildQualifiers( td ), td->typeexpr->build(), td->kind == TypeData::Basetypeof };
969} // buildTypeof
970
971
972VTableType * buildVtable( const TypeData * td ) {
973 assert( td->base );
974 return new VTableType{ buildQualifiers( td ), typebuild( td->base ) };
975} // buildVtable
976
977
978Declaration * buildDecl( const TypeData * td, const string &name, Type::StorageClasses scs, Expression * bitfieldWidth, Type::FuncSpecifiers funcSpec, LinkageSpec::Spec linkage, Expression *asmName, Initializer * init, std::list< Attribute * > attributes ) {
979 if ( td->kind == TypeData::Function ) {
980 if ( td->function.idList ) { // KR function ?
981 buildKRFunction( td->function ); // transform into C11 function
982 } // if
983
984 FunctionDecl * decl;
985 Statement * stmt = maybeBuild<Statement>( td->function.body );
986 CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt );
987 decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec );
988 buildList( td->function.withExprs, decl->withExprs );
989 return decl->set_asmName( asmName );
990 } else if ( td->kind == TypeData::Aggregate ) {
991 return buildAggregate( td, attributes, linkage );
992 } else if ( td->kind == TypeData::Enum ) {
993 return buildEnum( td, attributes, linkage );
994 } else if ( td->kind == TypeData::Symbolic ) {
995 return buildSymbolic( td, attributes, name, scs, linkage );
996 } else {
997 return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName );
998 } // if
999 return nullptr;
1000} // buildDecl
1001
1002
1003FunctionType * buildFunction( const TypeData * td ) {
1004 assert( td->kind == TypeData::Function );
1005 FunctionType * ft = new FunctionType( buildQualifiers( td ), ! td->function.params || td->function.params->hasEllipsis );
1006 buildList( td->function.params, ft->parameters );
1007 buildForall( td->forall, ft->forall );
1008 if ( td->base ) {
1009 switch ( td->base->kind ) {
1010 case TypeData::Tuple:
1011 buildList( td->base->tuple, ft->returnVals );
1012 break;
1013 default:
1014 ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType * >( buildDecl( td->base, "", Type::StorageClasses(), nullptr, Type::FuncSpecifiers(), LinkageSpec::Cforall, nullptr ) ) );
1015 } // switch
1016 } else {
1017 ft->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) );
1018 } // if
1019 return ft;
1020} // buildFunction
1021
1022
1023// Transform KR routine declarations into C99 routine declarations:
1024//
1025// rtn( a, b, c ) int a, c; double b {} => int rtn( int a, double c, int b ) {}
1026//
1027// The type information for each post-declaration is moved to the corresponding pre-parameter and the post-declaration
1028// is deleted. Note, the order of the parameter names may not be the same as the declaration names. Duplicate names and
1029// extra names are disallowed.
1030//
1031// Note, there is no KR routine-prototype syntax:
1032//
1033// rtn( a, b, c ) int a, c; double b; // invalid KR prototype
1034// rtn(); // valid KR prototype
1035
1036void buildKRFunction( const TypeData::Function_t & function ) {
1037 assert( ! function.params );
1038 // loop over declaration first as it is easier to spot errors
1039 for ( DeclarationNode * decl = function.oldDeclList; decl != nullptr; decl = dynamic_cast< DeclarationNode * >( decl->get_next() ) ) {
1040 // scan ALL parameter names for each declaration name to check for duplicates
1041 for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
1042 if ( *decl->name == *param->name ) {
1043 // type set => parameter name already transformed by a declaration names so there is a duplicate
1044 // declaration name attempting a second transformation
1045 if ( param->type ) SemanticError( param->location, string( "duplicate declaration name " ) + *param->name );
1046 // declaration type reset => declaration already transformed by a parameter name so there is a duplicate
1047 // parameter name attempting a second transformation
1048 if ( ! decl->type ) SemanticError( param->location, string( "duplicate parameter name " ) + *param->name );
1049 param->type = decl->type; // set copy declaration type to parameter type
1050 decl->type = nullptr; // reset declaration type
1051 param->attributes.splice( param->attributes.end(), decl->attributes ); // copy and reset attributes from declaration to parameter
1052 } // if
1053 } // for
1054 // declaration type still set => type not moved to a matching parameter so there is a missing parameter name
1055 if ( decl->type ) SemanticError( decl->location, string( "missing name in parameter list " ) + *decl->name );
1056 } // for
1057
1058 // Parameter names without a declaration default to type int:
1059 //
1060 // rtb( a, b, c ) const char * b; {} => int rtn( int a, const char * b, int c ) {}
1061
1062 for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
1063 if ( ! param->type ) { // generate type int for empty parameter type
1064 param->type = new TypeData( TypeData::Basic );
1065 param->type->basictype = DeclarationNode::Int;
1066 } // if
1067 } // for
1068
1069 function.params = function.idList; // newly modified idList becomes parameters
1070 function.idList = nullptr; // idList now empty
1071 delete function.oldDeclList; // deletes entire list
1072 function.oldDeclList = nullptr; // reset
1073} // buildKRFunction
1074
1075// Local Variables: //
1076// tab-width: 4 //
1077// mode: c++ //
1078// compile-command: "make install" //
1079// End: //
Note: See TracBrowser for help on using the repository browser.