source: src/Parser/TypeData.cc @ 6411b7d

ADTast-experimental
Last change on this file since 6411b7d was e874605, checked in by JiadaL <j82liang@…>, 19 months ago

Add class InlineValueDecl?, which is a Declaration class that works as a placeholder for aggregration value inherited from other aggregration. Disable inline value overwrite.

  • 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 : Peter A. Buhr
12// Last Modified On : Tue May 10 22:36:52 2022
13// Update Count     : 677
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 ) os << DeclarationNode::complexTypeNames[ complextype ] << " ";
286                if ( basictype != DeclarationNode::NoBasicType ) os << DeclarationNode::basicTypeNames[ basictype ] << " ";
287                break;
288          case Pointer:
289                os << "pointer ";
290                if ( base ) {
291                        os << "to ";
292                        base->print( os, indent );
293                } // if
294                break;
295          case Reference:
296                os << "reference ";
297                if ( base ) {
298                        os << "to ";
299                        base->print( os, indent );
300                } // if
301                break;
302          case Array:
303                if ( array.isStatic ) {
304                        os << "static ";
305                } // if
306                if ( array.dimension ) {
307                        os << "array of ";
308                        array.dimension->printOneLine( os, indent );
309                } else if ( array.isVarLen ) {
310                        os << "variable-length array of ";
311                } else {
312                        os << "open array of ";
313                } // if
314                if ( base ) {
315                        base->print( os, indent );
316                } // if
317                break;
318          case Function:
319                os << "function" << endl;
320                if ( function.params ) {
321                        os << string( indent + 2, ' ' ) << "with parameters " << endl;
322                        function.params->printList( os, indent + 4 );
323                } else {
324                        os << string( indent + 2, ' ' ) << "with no parameters" << endl;
325                } // if
326                if ( function.idList ) {
327                        os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl;
328                        function.idList->printList( os, indent + 4 );
329                } // if
330                if ( function.oldDeclList ) {
331                        os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl;
332                        function.oldDeclList->printList( os, indent + 4 );
333                } // if
334                os << string( indent + 2, ' ' ) << "returning ";
335                if ( base ) {
336                        base->print( os, indent + 4 );
337                } else {
338                        os << "nothing ";
339                } // if
340                os << endl;
341                if ( function.body ) {
342                        os << string( indent + 2, ' ' ) << "with body " << endl;
343                        function.body->printList( os, indent + 2 );
344                } // if
345                break;
346          case Aggregate:
347                os << AggregateDecl::aggrString( aggregate.kind ) << ' ' << *aggregate.name << endl;
348                if ( aggregate.params ) {
349                        os << string( indent + 2, ' ' ) << "with type parameters" << endl;
350                        aggregate.params->printList( os, indent + 4 );
351                } // if
352                if ( aggregate.actuals ) {
353                        os << string( indent + 2, ' ' ) << "instantiated with actual parameters" << endl;
354                        aggregate.actuals->printList( os, indent + 4 );
355                } // if
356                if ( aggregate.fields ) {
357                        os << string( indent + 2, ' ' ) << "with members" << endl;
358                        aggregate.fields->printList( os, indent + 4 );
359                } // if
360                if ( aggregate.body ) {
361                        os << string( indent + 2, ' ' ) << " with body" << endl;
362                } // if
363                break;
364          case AggregateInst:
365                if ( aggInst.aggregate ) {
366                        os << "instance of " ;
367                        aggInst.aggregate->print( os, indent );
368                } else {
369                        os << "instance of an unspecified aggregate ";
370                } // if
371                if ( aggInst.params ) {
372                        os << string( indent + 2, ' ' ) << "with parameters" << endl;
373                        aggInst.params->printList( os, indent + 2 );
374                } // if
375                break;
376          case Enum:
377                os << "enumeration ";
378                if ( enumeration.constants ) {
379                        os << "with constants" << endl;
380                        enumeration.constants->printList( os, indent + 2 );
381                } // if
382                if ( enumeration.body ) {
383                        os << string( indent + 2, ' ' ) << " with body" << endl;
384                } // if
385                if ( base ) {
386                        os << "for ";
387                        base->print( os, indent + 2 );
388                } // if
389                break;
390          case EnumConstant:
391                os << "enumeration constant ";
392                break;
393          case Symbolic:
394                if ( symbolic.isTypedef ) {
395                        os << "typedef definition ";
396                } else {
397                        os << "type definition ";
398                } // if
399                if ( symbolic.params ) {
400                        os << endl << string( indent + 2, ' ' ) << "with parameters" << endl;
401                        symbolic.params->printList( os, indent + 2 );
402                } // if
403                if ( symbolic.assertions ) {
404                        os << endl << string( indent + 2, ' ' ) << "with assertions" << endl;
405                        symbolic.assertions->printList( os, indent + 4 );
406                        os << string( indent + 2, ' ' );
407                } // if
408                if ( base ) {
409                        os << "for ";
410                        base->print( os, indent + 2 );
411                } // if
412                break;
413          case SymbolicInst:
414                os << *symbolic.name;
415                if ( symbolic.actuals ) {
416                        os << "(";
417                        symbolic.actuals->printList( os, indent + 2 );
418                        os << ")";
419                } // if
420                break;
421          case Tuple:
422                os << "tuple ";
423                if ( tuple ) {
424                        os << "with members" << endl;
425                        tuple->printList( os, indent + 2 );
426                } // if
427                break;
428          case Basetypeof:
429                os << "base-";
430                #if defined(__GNUC__) && __GNUC__ >= 7
431                        __attribute__((fallthrough));
432                #endif
433                // FALL THROUGH
434          case Typeof:
435                os << "type-of expression ";
436                if ( typeexpr ) {
437                        typeexpr->print( os, indent + 2 );
438                } // if
439                break;
440          case Vtable:
441                os << "vtable";
442                break;
443          case Builtin:
444                os << DeclarationNode::builtinTypeNames[builtintype];
445                break;
446          case GlobalScope:
447                break;
448          case Qualified:
449                qualified.parent->print( os );
450                os << ".";
451                qualified.child->print( os );
452                break;
453          case Unknown:
454                os << "entity of unknown type ";
455                break;
456          default:
457                os << "internal error: TypeData::print " << kind << endl;
458                assert( false );
459        } // switch
460} // TypeData::print
461
462const std::string * TypeData::leafName() const {
463        switch ( kind ) {
464          case Unknown:
465          case Pointer:
466          case Reference:
467          case EnumConstant:
468          case GlobalScope:
469          case Array:
470          case Basic:
471          case Function:
472          case AggregateInst:
473          case Tuple:
474          case Typeof:
475          case Basetypeof:
476          case Builtin:
477          case Vtable:
478                assertf(false, "Tried to get leaf name from kind without a name: %d", kind);
479                break;
480          case Aggregate:
481                return aggregate.name;
482          case Enum:
483                return enumeration.name;
484          case Symbolic:
485          case SymbolicInst:
486                return symbolic.name;
487          case Qualified:
488                return qualified.child->leafName();
489        } // switch
490        assert(false);
491}
492
493
494template< typename ForallList >
495void buildForall( const DeclarationNode * firstNode, ForallList &outputList ) {
496        buildList( firstNode, outputList );
497        auto n = firstNode;
498        for ( typename ForallList::iterator i = outputList.begin(); i != outputList.end(); ++i, n = (DeclarationNode*)n->get_next() ) {
499                TypeDecl * td = static_cast<TypeDecl *>(*i);
500                if ( n->variable.tyClass == TypeDecl::Otype ) {
501                        // add assertion parameters to `type' tyvars in reverse order
502                        // add dtor:  void ^?{}(T *)
503                        FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false );
504                        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 ) );
505                        td->get_assertions().push_front( new FunctionDecl( "^?{}", Type::StorageClasses(), LinkageSpec::Cforall, dtorType, nullptr ) );
506
507                        // add copy ctor:  void ?{}(T *, T)
508                        FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false );
509                        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 ) );
510                        copyCtorType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
511                        td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, copyCtorType, nullptr ) );
512
513                        // add default ctor:  void ?{}(T *)
514                        FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false );
515                        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 ) );
516                        td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, ctorType, nullptr ) );
517
518                        // add assignment operator:  T * ?=?(T *, T)
519                        FunctionType * assignType = new FunctionType( Type::Qualifiers(), false );
520                        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 ) );
521                        assignType->get_parameters().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
522                        assignType->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new TypeInstType( Type::Qualifiers(), td->get_name(), *i ), nullptr ) );
523                        td->get_assertions().push_front( new FunctionDecl( "?=?", Type::StorageClasses(), LinkageSpec::Cforall, assignType, nullptr ) );
524                } // if
525        } // for
526} // buildForall
527
528
529Type * typebuild( const TypeData * td ) {
530        assert( td );
531        switch ( td->kind ) {
532          case TypeData::Unknown:
533                // fill in implicit int
534                return new BasicType( buildQualifiers( td ), BasicType::SignedInt );
535          case TypeData::Basic:
536                return buildBasicType( td );
537          case TypeData::Pointer:
538                return buildPointer( td );
539          case TypeData::Array:
540                return buildArray( td );
541          case TypeData::Reference:
542                return buildReference( td );
543          case TypeData::Function:
544                return buildFunction( td );
545          case TypeData::AggregateInst:
546                return buildAggInst( td );
547          case TypeData::EnumConstant:
548                return new EnumInstType( buildQualifiers( td ), "" );
549          case TypeData::SymbolicInst:
550                return buildSymbolicInst( td );
551          case TypeData::Tuple:
552                return buildTuple( td );
553          case TypeData::Typeof:
554          case TypeData::Basetypeof:
555                return buildTypeof( td );
556          case TypeData::Vtable:
557                return buildVtable( td );
558          case TypeData::Builtin:
559                switch ( td->builtintype ) {
560                  case DeclarationNode::Zero:
561                        return new ZeroType( noQualifiers );
562                  case DeclarationNode::One:
563                        return new OneType( noQualifiers );
564                  default:
565                        return new VarArgsType( buildQualifiers( td ) );
566                } // switch
567          case TypeData::GlobalScope:
568                return new GlobalScopeType();
569          case TypeData::Qualified:
570                return new QualifiedType( buildQualifiers( td ), typebuild( td->qualified.parent ), typebuild( td->qualified.child ) );
571          case TypeData::Symbolic:
572          case TypeData::Enum:
573          case TypeData::Aggregate:
574                assert( false );
575        } // switch
576
577        return nullptr;
578} // typebuild
579
580
581TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) {
582        TypeData * ret = nullptr;
583
584        switch ( td->kind ) {
585          case TypeData::Aggregate:
586                if ( ! toplevel && td->aggregate.body ) {
587                        ret = td->clone();
588                } // if
589                break;
590          case TypeData::Enum:
591                if ( ! toplevel && td->enumeration.body ) {
592                        ret = td->clone();
593                } // if
594                break;
595          case TypeData::AggregateInst:
596                if ( td->aggInst.aggregate ) {
597                        ret = typeextractAggregate( td->aggInst.aggregate, false );
598                } // if
599                break;
600          default:
601                if ( td->base ) {
602                        ret = typeextractAggregate( td->base, false );
603                } // if
604        } // switch
605        return ret;
606} // typeextractAggregate
607
608
609Type::Qualifiers buildQualifiers( const TypeData * td ) {
610        return td->qualifiers;
611} // buildQualifiers
612
613
614static string genTSError( string msg, DeclarationNode::BasicType basictype ) {
615        SemanticError( yylloc, string( "invalid type specifier \"" ) + msg + "\" for type \"" + DeclarationNode::basicTypeNames[basictype] + "\"." );
616} // genTSError
617
618Type * buildBasicType( const TypeData * td ) {
619        BasicType::Kind ret;
620
621        switch ( td->basictype ) {
622          case DeclarationNode::Void:
623                if ( td->signedness != DeclarationNode::NoSignedness ) {
624                        genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
625                } // if
626                if ( td->length != DeclarationNode::NoLength ) {
627                        genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
628                } // if
629                return new VoidType( buildQualifiers( td ) );
630                break;
631
632          case DeclarationNode::Bool:
633                if ( td->signedness != DeclarationNode::NoSignedness ) {
634                        genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
635                } // if
636                if ( td->length != DeclarationNode::NoLength ) {
637                        genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
638                } // if
639
640                ret = BasicType::Bool;
641                break;
642
643          case DeclarationNode::Char:
644                // C11 Standard 6.2.5.15: The three types char, signed char, and unsigned char are collectively called the
645                // character types. The implementation shall define char to have the same range, representation, and behavior as
646                // either signed char or unsigned char.
647                static BasicType::Kind chartype[] = { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::Char };
648
649                if ( td->length != DeclarationNode::NoLength ) {
650                        genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
651                } // if
652
653                ret = chartype[ td->signedness ];
654                break;
655
656          case DeclarationNode::Int:
657                static BasicType::Kind inttype[2][4] = {
658                        { BasicType::ShortSignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, BasicType::SignedInt },
659                        { BasicType::ShortUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt },
660                };
661
662          Integral: ;
663                if ( td->signedness == DeclarationNode::NoSignedness ) {
664                        const_cast<TypeData *>(td)->signedness = DeclarationNode::Signed;
665                } // if
666                ret = inttype[ td->signedness ][ td->length ];
667                break;
668
669          case DeclarationNode::Int128:
670                ret = td->signedness == DeclarationNode::Unsigned ? BasicType::UnsignedInt128 : BasicType::SignedInt128;
671                if ( td->length != DeclarationNode::NoLength ) {
672                        genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
673                } // if
674                break;
675
676          case DeclarationNode::Float:
677          case DeclarationNode::Double:
678          case DeclarationNode::LongDouble:                                     // not set until below
679          case DeclarationNode::uuFloat80:
680          case DeclarationNode::uuFloat128:
681          case DeclarationNode::uFloat16:
682          case DeclarationNode::uFloat32:
683          case DeclarationNode::uFloat32x:
684          case DeclarationNode::uFloat64:
685          case DeclarationNode::uFloat64x:
686          case DeclarationNode::uFloat128:
687          case DeclarationNode::uFloat128x:
688                static BasicType::Kind floattype[2][12] = {
689                        { 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, },
690                        { BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::uuFloat80, BasicType::uuFloat128, BasicType::uFloat16, BasicType::uFloat32, BasicType::uFloat32x, BasicType::uFloat64, BasicType::uFloat64x, BasicType::uFloat128, BasicType::uFloat128x, },
691                };
692
693          FloatingPoint: ;
694                if ( td->signedness != DeclarationNode::NoSignedness ) {
695                        genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
696                } // if
697                if ( td->length == DeclarationNode::Short || td->length == DeclarationNode::LongLong ) {
698                        genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
699                } // if
700                if ( td->basictype != DeclarationNode::Double && td->length == DeclarationNode::Long ) {
701                        genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
702                } // if
703                if ( td->complextype == DeclarationNode::Imaginary ) {
704                        genTSError( DeclarationNode::complexTypeNames[ td->complextype ], td->basictype );
705                } // if
706                if ( (td->basictype == DeclarationNode::uuFloat80 || td->basictype == DeclarationNode::uuFloat128) && td->complextype == DeclarationNode::Complex ) { // gcc unsupported
707                        genTSError( DeclarationNode::complexTypeNames[ td->complextype ], td->basictype );
708                } // if
709                if ( td->length == DeclarationNode::Long ) {
710                        const_cast<TypeData *>(td)->basictype = DeclarationNode::LongDouble;
711                } // if
712
713                ret = floattype[ td->complextype ][ td->basictype - DeclarationNode::Float ];
714                //printf( "XXXX %d %d %d %d\n", td->complextype, td->basictype, DeclarationNode::Float, ret );
715                break;
716
717          case DeclarationNode::NoBasicType:
718                // No basic type in declaration => default double for Complex/Imaginary and int type for integral types
719                if ( td->complextype == DeclarationNode::Complex || td->complextype == DeclarationNode::Imaginary ) {
720                        const_cast<TypeData *>(td)->basictype = DeclarationNode::Double;
721                        goto FloatingPoint;
722                } // if
723
724                const_cast<TypeData *>(td)->basictype = DeclarationNode::Int;
725                goto Integral;
726          default:
727                assertf( false, "unknown basic type" );
728                return nullptr;
729        } // switch
730
731        BasicType * bt = new BasicType( buildQualifiers( td ), ret );
732        buildForall( td->forall, bt->get_forall() );
733        return bt;
734} // buildBasicType
735
736
737PointerType * buildPointer( const TypeData * td ) {
738        PointerType * pt;
739        if ( td->base ) {
740                pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) );
741        } else {
742                pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
743        } // if
744        buildForall( td->forall, pt->get_forall() );
745        return pt;
746} // buildPointer
747
748
749ArrayType * buildArray( const TypeData * td ) {
750        ArrayType * at;
751        if ( td->base ) {
752                at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild< Expression >( td->array.dimension ),
753                                                        td->array.isVarLen, td->array.isStatic );
754        } else {
755                at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
756                                                        maybeBuild< Expression >( td->array.dimension ), td->array.isVarLen, td->array.isStatic );
757        } // if
758        buildForall( td->forall, at->get_forall() );
759        return at;
760} // buildArray
761
762
763ReferenceType * buildReference( const TypeData * td ) {
764        ReferenceType * rt;
765        if ( td->base ) {
766                rt = new ReferenceType( buildQualifiers( td ), typebuild( td->base ) );
767        } else {
768                rt = new ReferenceType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
769        } // if
770        buildForall( td->forall, rt->get_forall() );
771        return rt;
772} // buildReference
773
774
775AggregateDecl * buildAggregate( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
776        assert( td->kind == TypeData::Aggregate );
777        AggregateDecl * at;
778        switch ( td->aggregate.kind ) {
779          case AggregateDecl::Struct:
780          case AggregateDecl::Coroutine:
781          case AggregateDecl::Exception:
782          case AggregateDecl::Generator:
783          case AggregateDecl::Monitor:
784          case AggregateDecl::Thread:
785                at = new StructDecl( *td->aggregate.name, td->aggregate.kind, attributes, linkage );
786                buildForall( td->aggregate.params, at->get_parameters() );
787                break;
788          case AggregateDecl::Union:
789                at = new UnionDecl( *td->aggregate.name, attributes, linkage );
790                buildForall( td->aggregate.params, at->get_parameters() );
791                break;
792          case AggregateDecl::Trait:
793                at = new TraitDecl( *td->aggregate.name, attributes, linkage );
794                buildList( td->aggregate.params, at->get_parameters() );
795                break;
796          default:
797                assert( false );
798        } // switch
799
800        buildList( td->aggregate.fields, at->get_members() );
801        at->set_body( td->aggregate.body );
802
803        return at;
804} // buildAggregate
805
806
807ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
808        switch ( type->kind ) {
809          case TypeData::Enum: {
810                  if ( type->enumeration.body ) {
811                          EnumDecl * typedecl = buildEnum( type, attributes, linkage );
812                          return new EnumInstType( buildQualifiers( type ), typedecl );
813                  } else {
814                          return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
815                  } // if
816          }
817          case TypeData::Aggregate: {
818                  ReferenceToType * ret;
819                  if ( type->aggregate.body ) {
820                          AggregateDecl * typedecl = buildAggregate( type, attributes, linkage );
821                          switch ( type->aggregate.kind ) {
822                                case AggregateDecl::Struct:
823                                case AggregateDecl::Coroutine:
824                                case AggregateDecl::Monitor:
825                                case AggregateDecl::Thread:
826                                  ret = new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl );
827                                  break;
828                                case AggregateDecl::Union:
829                                  ret = new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl );
830                                  break;
831                                case AggregateDecl::Trait:
832                                  assert( false );
833                                  //ret = new TraitInstType( buildQualifiers( type ), (TraitDecl *)typedecl );
834                                  break;
835                                default:
836                                  assert( false );
837                          } // switch
838                  } else {
839                          switch ( type->aggregate.kind ) {
840                                case AggregateDecl::Struct:
841                                case AggregateDecl::Coroutine:
842                                case AggregateDecl::Monitor:
843                                case AggregateDecl::Thread:
844                                  ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
845                                  break;
846                                case AggregateDecl::Union:
847                                  ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
848                                  break;
849                                case AggregateDecl::Trait:
850                                  ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
851                                  break;
852                                default:
853                                  assert( false );
854                          } // switch
855                  } // if
856                  return ret;
857          }
858          default:
859                assert( false );
860        } // switch
861} // buildAggInst
862
863
864ReferenceToType * buildAggInst( const TypeData * td ) {
865        assert( td->kind == TypeData::AggregateInst );
866
867        // ReferenceToType * ret = buildComAggInst( td->aggInst.aggregate, std::list< Attribute * >() );
868        ReferenceToType * ret = nullptr;
869        TypeData * type = td->aggInst.aggregate;
870        switch ( type->kind ) {
871          case TypeData::Enum: {
872                  return new EnumInstType( buildQualifiers( type ), *type->enumeration.name );
873          }
874          case TypeData::Aggregate: {
875                  switch ( type->aggregate.kind ) {
876                        case AggregateDecl::Struct:
877                        case AggregateDecl::Coroutine:
878                        case AggregateDecl::Monitor:
879                        case AggregateDecl::Thread:
880                          ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
881                          break;
882                        case AggregateDecl::Union:
883                          ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
884                          break;
885                        case AggregateDecl::Trait:
886                          ret = new TraitInstType( buildQualifiers( type ), *type->aggregate.name );
887                          break;
888                        default:
889                          assert( false );
890                  } // switch
891          }
892          break;
893          default:
894                assert( false );
895        } // switch
896
897        ret->set_hoistType( td->aggInst.hoistType );
898        buildList( td->aggInst.params, ret->get_parameters() );
899        buildForall( td->forall, ret->get_forall() );
900        return ret;
901} // buildAggInst
902
903
904NamedTypeDecl * buildSymbolic( const TypeData * td, std::list< Attribute * > attributes, const string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage ) {
905        assert( td->kind == TypeData::Symbolic );
906        NamedTypeDecl * ret;
907        assert( td->base );
908        if ( td->symbolic.isTypedef ) {
909                ret = new TypedefDecl( name, td->location, scs, typebuild( td->base ), linkage );
910        } else {
911                ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Dtype, true );
912        } // if
913        buildList( td->symbolic.assertions, ret->get_assertions() );
914        ret->base->attributes.splice( ret->base->attributes.end(), attributes );
915        return ret;
916} // buildSymbolic
917
918
919EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
920        assert( td->kind == TypeData::Enum );
921        Type * baseType = td->base ? typebuild(td->base) : nullptr;
922        EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes, td->enumeration.typed, linkage, baseType );
923        buildList( td->enumeration.constants, ret->get_members() );
924        list< Declaration * >::iterator members = ret->get_members().begin();
925        for ( const DeclarationNode * cur = td->enumeration.constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
926                if ( cur->enumInLine ) {
927                        // Do Nothing
928                } else if ( ret->isTyped && !ret->base && cur->has_enumeratorValue() ) {
929                        SemanticError( td->location, "Enumerator of enum(void) cannot have an explicit initializer value." );
930                } else if ( cur->has_enumeratorValue() ) {
931                        ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
932                        member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ) ) );
933                } else if ( !cur->initializer ) {
934                        if ( baseType && (!dynamic_cast<BasicType *>(baseType) || !dynamic_cast<BasicType *>(baseType)->isWholeNumber())) {
935                                SemanticError( td->location, "Enumerators of an non-integer typed enum must be explicitly initialized." );
936                        }
937                }
938                // else cur is a List Initializer and has been set as init in buildList()
939                // if
940        } // for
941        ret->set_body( td->enumeration.body );
942        return ret;
943} // buildEnum
944
945
946TypeInstType * buildSymbolicInst( const TypeData * td ) {
947        assert( td->kind == TypeData::SymbolicInst );
948        TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false );
949        buildList( td->symbolic.actuals, ret->get_parameters() );
950        buildForall( td->forall, ret->get_forall() );
951        return ret;
952} // buildSymbolicInst
953
954
955TupleType * buildTuple( const TypeData * td ) {
956        assert( td->kind == TypeData::Tuple );
957        std::list< Type * > types;
958        buildTypeList( td->tuple, types );
959        TupleType * ret = new TupleType( buildQualifiers( td ), types );
960        buildForall( td->forall, ret->get_forall() );
961        return ret;
962} // buildTuple
963
964
965TypeofType * buildTypeof( const TypeData * td ) {
966        assert( td->kind == TypeData::Typeof || td->kind == TypeData::Basetypeof );
967        assert( td->typeexpr );
968        // assert( td->typeexpr->expr );
969        return new TypeofType{ buildQualifiers( td ), td->typeexpr->build(), td->kind == TypeData::Basetypeof };
970} // buildTypeof
971
972
973VTableType * buildVtable( const TypeData * td ) {
974        assert( td->base );
975        return new VTableType{ buildQualifiers( td ), typebuild( td->base ) };
976} // buildVtable
977
978
979Declaration * 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 ) {
980        if ( td->kind == TypeData::Function ) {
981                if ( td->function.idList ) {                                    // KR function ?
982                        buildKRFunction( td->function );                        // transform into C11 function
983                } // if
984
985                FunctionDecl * decl;
986                Statement * stmt = maybeBuild<Statement>( td->function.body );
987                CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt );
988                decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec );
989                buildList( td->function.withExprs, decl->withExprs );
990                return decl->set_asmName( asmName );
991        } else if ( td->kind == TypeData::Aggregate ) {
992                return buildAggregate( td, attributes, linkage );
993        } else if ( td->kind == TypeData::Enum ) {
994                return buildEnum( td, attributes, linkage );
995        } else if ( td->kind == TypeData::Symbolic ) {
996                return buildSymbolic( td, attributes, name, scs, linkage );
997        } else {
998                return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName );
999        } // if
1000        return nullptr;
1001} // buildDecl
1002
1003
1004FunctionType * buildFunction( const TypeData * td ) {
1005        assert( td->kind == TypeData::Function );
1006        FunctionType * ft = new FunctionType( buildQualifiers( td ), ! td->function.params || td->function.params->hasEllipsis );
1007        buildList( td->function.params, ft->parameters );
1008        buildForall( td->forall, ft->forall );
1009        if ( td->base ) {
1010                switch ( td->base->kind ) {
1011                  case TypeData::Tuple:
1012                        buildList( td->base->tuple, ft->returnVals );
1013                        break;
1014                  default:
1015                        ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType * >( buildDecl( td->base, "", Type::StorageClasses(), nullptr, Type::FuncSpecifiers(), LinkageSpec::Cforall, nullptr ) ) );
1016                } // switch
1017        } else {
1018                ft->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) );
1019        } // if
1020        return ft;
1021} // buildFunction
1022
1023
1024// Transform KR routine declarations into C99 routine declarations:
1025//
1026//    rtn( a, b, c ) int a, c; double b {}  =>  int rtn( int a, double c, int b ) {}
1027//
1028// The type information for each post-declaration is moved to the corresponding pre-parameter and the post-declaration
1029// is deleted. Note, the order of the parameter names may not be the same as the declaration names. Duplicate names and
1030// extra names are disallowed.
1031//
1032// Note, there is no KR routine-prototype syntax:
1033//
1034//    rtn( a, b, c ) int a, c; double b; // invalid KR prototype
1035//    rtn(); // valid KR prototype
1036
1037void buildKRFunction( const TypeData::Function_t & function ) {
1038        assert( ! function.params );
1039        // loop over declaration first as it is easier to spot errors
1040        for ( DeclarationNode * decl = function.oldDeclList; decl != nullptr; decl = dynamic_cast< DeclarationNode * >( decl->get_next() ) ) {
1041                // scan ALL parameter names for each declaration name to check for duplicates
1042                for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
1043                        if ( *decl->name == *param->name ) {
1044                                // type set => parameter name already transformed by a declaration names so there is a duplicate
1045                                // declaration name attempting a second transformation
1046                                if ( param->type ) SemanticError( param->location, string( "duplicate declaration name " ) + *param->name );
1047                                // declaration type reset => declaration already transformed by a parameter name so there is a duplicate
1048                                // parameter name attempting a second transformation
1049                                if ( ! decl->type ) SemanticError( param->location, string( "duplicate parameter name " ) + *param->name );
1050                                param->type = decl->type;                               // set copy declaration type to parameter type
1051                                decl->type = nullptr;                                   // reset declaration type
1052                                param->attributes.splice( param->attributes.end(), decl->attributes ); // copy and reset attributes from declaration to parameter
1053                        } // if
1054                } // for
1055                // declaration type still set => type not moved to a matching parameter so there is a missing parameter name
1056                if ( decl->type ) SemanticError( decl->location, string( "missing name in parameter list " ) + *decl->name );
1057        } // for
1058
1059        // Parameter names without a declaration default to type int:
1060        //
1061        //    rtb( a, b, c ) const char * b; {} => int rtn( int a, const char * b, int c ) {}
1062
1063        for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
1064                if ( ! param->type ) {                                                  // generate type int for empty parameter type
1065                        param->type = new TypeData( TypeData::Basic );
1066                        param->type->basictype = DeclarationNode::Int;
1067                } // if
1068        } // for
1069
1070        function.params = function.idList;                                      // newly modified idList becomes parameters
1071        function.idList = nullptr;                                                      // idList now empty
1072        delete function.oldDeclList;                                            // deletes entire list
1073        function.oldDeclList = nullptr;                                         // reset
1074} // buildKRFunction
1075
1076// Local Variables: //
1077// tab-width: 4 //
1078// mode: c++ //
1079// compile-command: "make install" //
1080// End: //
Note: See TracBrowser for help on using the repository browser.