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

ADTast-experimental
Last change on this file since 6411b7d was e874605, checked in by JiadaL <j82liang@…>, 20 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
RevLine 
[b87a5ed]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//
[f1e012b]7// TypeData.cc --
[b87a5ed]8//
9// Author           : Rodolfo G. Esteves
10// Created On       : Sat May 16 15:12:51 2015
[66406f3]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue May 10 22:36:52 2022
13// Update Count     : 677
[b87a5ed]14//
15
[d180746]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
[51b7345]27#include "TypeData.h"
[d180746]28
29class Attribute;
30
[2298f728]31using namespace std;
[51b7345]32
[0b0f1dd]33TypeData::TypeData( Kind k ) : location( yylloc ), kind( k ), base( nullptr ), forall( nullptr ) /*, PTR1( (void*)(0xdeadbeefdeadbeef)), PTR2( (void*)(0xdeadbeefdeadbeef) ) */ {
[b87a5ed]34        switch ( kind ) {
35          case Unknown:
36          case Pointer:
[ce8c12f]37          case Reference:
[b87a5ed]38          case EnumConstant:
[47498bd]39          case GlobalScope:
[b87a5ed]40                // nothing else to initialize
41                break;
42          case Basic:
[8f6f47d7]43                // basic = new Basic_t;
[b87a5ed]44                break;
45          case Array:
[8f6f47d7]46                // array = new Array_t;
[2298f728]47                array.dimension = nullptr;
[8f6f47d7]48                array.isVarLen = false;
49                array.isStatic = false;
[b87a5ed]50                break;
51          case Function:
[8f6f47d7]52                // function = new Function_t;
[2298f728]53                function.params = nullptr;
54                function.idList = nullptr;
55                function.oldDeclList = nullptr;
56                function.body = nullptr;
[84276ba]57                function.withExprs = nullptr;
[b87a5ed]58                break;
[e496303]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;
[3d7e53b]65                enumeration.anon = false;
[6926a6d]66                break;
[b87a5ed]67          case Aggregate:
[8f6f47d7]68                // aggregate = new Aggregate_t;
[312029a]69                aggregate.kind = AggregateDecl::NoAggregate;
[2298f728]70                aggregate.name = nullptr;
71                aggregate.params = nullptr;
72                aggregate.actuals = nullptr;
73                aggregate.fields = nullptr;
[4a9ccc3]74                aggregate.body = false;
[d15a45d]75                aggregate.tagged = false;
76                aggregate.parent = nullptr;
[3d7e53b]77                aggregate.anon = false;
[b87a5ed]78                break;
79          case AggregateInst:
[8f6f47d7]80                // aggInst = new AggInst_t;
[2298f728]81                aggInst.aggregate = nullptr;
82                aggInst.params = nullptr;
[8f91c9ae]83                aggInst.hoistType = false;
[b87a5ed]84                break;
85          case Symbolic:
86          case SymbolicInst:
[8f6f47d7]87                // symbolic = new Symbolic_t;
[2298f728]88                symbolic.name = nullptr;
89                symbolic.params = nullptr;
90                symbolic.actuals = nullptr;
91                symbolic.assertions = nullptr;
[b87a5ed]92                break;
93          case Tuple:
[8f6f47d7]94                // tuple = new Tuple_t;
95                tuple = nullptr;
[b87a5ed]96                break;
97          case Typeof:
[f855545]98          case Basetypeof:
[8f6f47d7]99                // typeexpr = new Typeof_t;
100                typeexpr = nullptr;
[b87a5ed]101                break;
[93bbbc4]102          case Vtable:
103                break;
[28307be]104          case Builtin:
105                // builtin = new Builtin_t;
[c194661]106                case Qualified:
107                qualified.parent = nullptr;
108                qualified.child = nullptr;
[b87a5ed]109                break;
[68cd1ce]110        } // switch
[413ad05]111} // TypeData::TypeData
[51b7345]112
[201aeb9]113
[c8ffe20b]114TypeData::~TypeData() {
[b87a5ed]115        delete base;
116        delete forall;
117
118        switch ( kind ) {
119          case Unknown:
120          case Pointer:
[ce8c12f]121          case Reference:
[b87a5ed]122          case EnumConstant:
[47498bd]123          case GlobalScope:
[b87a5ed]124                // nothing to destroy
125                break;
126          case Basic:
[8f6f47d7]127                // delete basic;
[b87a5ed]128                break;
129          case Array:
[8f6f47d7]130                delete array.dimension;
131                // delete array;
[b87a5ed]132                break;
133          case Function:
[8f6f47d7]134                delete function.params;
135                delete function.idList;
136                delete function.oldDeclList;
137                delete function.body;
[84276ba]138                delete function.withExprs;
[8f6f47d7]139                // delete function;
[b87a5ed]140                break;
141          case Aggregate:
[2298f728]142                delete aggregate.name;
[8f6f47d7]143                delete aggregate.params;
144                delete aggregate.actuals;
145                delete aggregate.fields;
146                // delete aggregate;
[b87a5ed]147                break;
148          case AggregateInst:
[8f6f47d7]149                delete aggInst.aggregate;
150                delete aggInst.params;
151                // delete aggInst;
[b87a5ed]152                break;
153          case Enum:
[2298f728]154                delete enumeration.name;
[8f6f47d7]155                delete enumeration.constants;
156                // delete enumeration;
[b87a5ed]157                break;
158          case Symbolic:
159          case SymbolicInst:
[2298f728]160                delete symbolic.name;
[8f6f47d7]161                delete symbolic.params;
162                delete symbolic.actuals;
163                delete symbolic.assertions;
164                // delete symbolic;
[b87a5ed]165                break;
166          case Tuple:
[8f6f47d7]167                // delete tuple->members;
[b87a5ed]168                delete tuple;
169                break;
170          case Typeof:
[f855545]171          case Basetypeof:
[8f6f47d7]172                // delete typeexpr->expr;
[b87a5ed]173                delete typeexpr;
174                break;
[93bbbc4]175          case Vtable:
176                break;
[28307be]177          case Builtin:
178                // delete builtin;
179                break;
[c194661]180          case Qualified:
181                delete qualified.parent;
182                delete qualified.child;
[68cd1ce]183        } // switch
[413ad05]184} // TypeData::~TypeData
[51b7345]185
[201aeb9]186
[413ad05]187TypeData * TypeData::clone() const {
188        TypeData * newtype = new TypeData( kind );
[738e304]189        newtype->qualifiers = qualifiers;
[b87a5ed]190        newtype->base = maybeClone( base );
191        newtype->forall = maybeClone( forall );
192
193        switch ( kind ) {
194          case Unknown:
195          case EnumConstant:
196          case Pointer:
[ce8c12f]197          case Reference:
[47498bd]198          case GlobalScope:
[b87a5ed]199                // nothing else to copy
200                break;
201          case Basic:
[5b639ee]202                newtype->basictype = basictype;
203                newtype->complextype = complextype;
204                newtype->signedness = signedness;
205                newtype->length = length;
[b87a5ed]206                break;
207          case Array:
[8f6f47d7]208                newtype->array.dimension = maybeClone( array.dimension );
209                newtype->array.isVarLen = array.isVarLen;
210                newtype->array.isStatic = array.isStatic;
[b87a5ed]211                break;
212          case Function:
[8f6f47d7]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 );
[84276ba]217                newtype->function.withExprs = maybeClone( function.withExprs );
[b87a5ed]218                break;
219          case Aggregate:
[d15a45d]220                newtype->aggregate.kind = aggregate.kind;
[2298f728]221                newtype->aggregate.name = aggregate.name ? new string( *aggregate.name ) : nullptr;
[8f6f47d7]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;
[3d7e53b]226                newtype->aggregate.anon = aggregate.anon;
[6ea87486]227                newtype->aggregate.tagged = aggregate.tagged;
228                newtype->aggregate.parent = aggregate.parent ? new string( *aggregate.parent ) : nullptr;
[b87a5ed]229                break;
230          case AggregateInst:
[8f6f47d7]231                newtype->aggInst.aggregate = maybeClone( aggInst.aggregate );
232                newtype->aggInst.params = maybeClone( aggInst.params );
[43c89a7]233                newtype->aggInst.hoistType = aggInst.hoistType;
[b87a5ed]234                break;
235          case Enum:
[2298f728]236                newtype->enumeration.name = enumeration.name ? new string( *enumeration.name ) : nullptr;
[8f6f47d7]237                newtype->enumeration.constants = maybeClone( enumeration.constants );
[ca1a547]238                newtype->enumeration.body = enumeration.body;
[3d7e53b]239                newtype->enumeration.anon = enumeration.anon;
[b87a5ed]240                break;
241          case Symbolic:
242          case SymbolicInst:
[2298f728]243                newtype->symbolic.name = symbolic.name ? new string( *symbolic.name ) : nullptr;
[8f6f47d7]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;
[b87a5ed]248                break;
249          case Tuple:
[8f6f47d7]250                newtype->tuple = maybeClone( tuple );
[b87a5ed]251                break;
252          case Typeof:
[f855545]253          case Basetypeof:
[8f6f47d7]254                newtype->typeexpr = maybeClone( typeexpr );
[b87a5ed]255                break;
[93bbbc4]256          case Vtable:
257                break;
[90c3b1c]258          case Builtin:
[4cb935e]259                assert( builtintype == DeclarationNode::Zero || builtintype == DeclarationNode::One );
260                newtype->builtintype = builtintype;
[90c3b1c]261                break;
[c194661]262                case Qualified:
263                newtype->qualified.parent = maybeClone( qualified.parent );
264                newtype->qualified.child = maybeClone( qualified.child );
265                break;
[68cd1ce]266        } // switch
[b87a5ed]267        return newtype;
[413ad05]268} // TypeData::clone
[51b7345]269
[201aeb9]270
[2298f728]271void TypeData::print( ostream &os, int indent ) const {
[738e304]272        for ( int i = 0; i < Type::NumTypeQualifier; i += 1 ) {
[615a096]273                if ( qualifiers[i] ) os << Type::QualifiersNames[ i ] << ' ';
[c1c1112]274        } // for
[b87a5ed]275
276        if ( forall ) {
277                os << "forall " << endl;
[721f17a]278                forall->printList( os, indent + 4 );
[68cd1ce]279        } // if
[b87a5ed]280
281        switch ( kind ) {
[f7e4db27]282          case Basic:
283                if ( signedness != DeclarationNode::NoSignedness ) os << DeclarationNode::signednessNames[ signedness ] << " ";
284                if ( length != DeclarationNode::NoLength ) os << DeclarationNode::lengthNames[ length ] << " ";
[66406f3]285                if ( complextype != DeclarationNode::NoComplexType ) os << DeclarationNode::complexTypeNames[ complextype ] << " ";
286                if ( basictype != DeclarationNode::NoBasicType ) os << DeclarationNode::basicTypeNames[ basictype ] << " ";
[b87a5ed]287                break;
288          case Pointer:
289                os << "pointer ";
290                if ( base ) {
291                        os << "to ";
292                        base->print( os, indent );
[68cd1ce]293                } // if
[b87a5ed]294                break;
[f7e4db27]295          case Reference:
296                os << "reference ";
297                if ( base ) {
298                        os << "to ";
299                        base->print( os, indent );
300                } // if
[b87a5ed]301                break;
302          case Array:
[8f6f47d7]303                if ( array.isStatic ) {
[b87a5ed]304                        os << "static ";
[68cd1ce]305                } // if
[8f6f47d7]306                if ( array.dimension ) {
[b87a5ed]307                        os << "array of ";
[8f6f47d7]308                        array.dimension->printOneLine( os, indent );
309                } else if ( array.isVarLen ) {
[b87a5ed]310                        os << "variable-length array of ";
311                } else {
312                        os << "open array of ";
[68cd1ce]313                } // if
[b87a5ed]314                if ( base ) {
315                        base->print( os, indent );
[68cd1ce]316                } // if
[b87a5ed]317                break;
318          case Function:
319                os << "function" << endl;
[8f6f47d7]320                if ( function.params ) {
[721f17a]321                        os << string( indent + 2, ' ' ) << "with parameters " << endl;
[8f6f47d7]322                        function.params->printList( os, indent + 4 );
[b87a5ed]323                } else {
[07ec1a2]324                        os << string( indent + 2, ' ' ) << "with no parameters" << endl;
[68cd1ce]325                } // if
[8f6f47d7]326                if ( function.idList ) {
[721f17a]327                        os << string( indent + 2, ' ' ) << "with old-style identifier list " << endl;
[8f6f47d7]328                        function.idList->printList( os, indent + 4 );
[68cd1ce]329                } // if
[8f6f47d7]330                if ( function.oldDeclList ) {
[721f17a]331                        os << string( indent + 2, ' ' ) << "with old-style declaration list " << endl;
[8f6f47d7]332                        function.oldDeclList->printList( os, indent + 4 );
[68cd1ce]333                } // if
[721f17a]334                os << string( indent + 2, ' ' ) << "returning ";
[b87a5ed]335                if ( base ) {
[721f17a]336                        base->print( os, indent + 4 );
[b87a5ed]337                } else {
338                        os << "nothing ";
[68cd1ce]339                } // if
[b87a5ed]340                os << endl;
[8f6f47d7]341                if ( function.body ) {
[ca1a547]342                        os << string( indent + 2, ' ' ) << "with body " << endl;
[8f6f47d7]343                        function.body->printList( os, indent + 2 );
[68cd1ce]344                } // if
[b87a5ed]345                break;
346          case Aggregate:
[312029a]347                os << AggregateDecl::aggrString( aggregate.kind ) << ' ' << *aggregate.name << endl;
[8f6f47d7]348                if ( aggregate.params ) {
[07ec1a2]349                        os << string( indent + 2, ' ' ) << "with type parameters" << endl;
[8f6f47d7]350                        aggregate.params->printList( os, indent + 4 );
[68cd1ce]351                } // if
[8f6f47d7]352                if ( aggregate.actuals ) {
[07ec1a2]353                        os << string( indent + 2, ' ' ) << "instantiated with actual parameters" << endl;
[8f6f47d7]354                        aggregate.actuals->printList( os, indent + 4 );
[68cd1ce]355                } // if
[8f6f47d7]356                if ( aggregate.fields ) {
[07ec1a2]357                        os << string( indent + 2, ' ' ) << "with members" << endl;
[8f6f47d7]358                        aggregate.fields->printList( os, indent + 4 );
[5d125e4]359                } // if
[8f6f47d7]360                if ( aggregate.body ) {
[07ec1a2]361                        os << string( indent + 2, ' ' ) << " with body" << endl;
[68cd1ce]362                } // if
[b87a5ed]363                break;
364          case AggregateInst:
[8f6f47d7]365                if ( aggInst.aggregate ) {
[b87a5ed]366                        os << "instance of " ;
[8f6f47d7]367                        aggInst.aggregate->print( os, indent );
[b87a5ed]368                } else {
369                        os << "instance of an unspecified aggregate ";
[68cd1ce]370                } // if
[8f6f47d7]371                if ( aggInst.params ) {
[07ec1a2]372                        os << string( indent + 2, ' ' ) << "with parameters" << endl;
[8f6f47d7]373                        aggInst.params->printList( os, indent + 2 );
[68cd1ce]374                } // if
[b87a5ed]375                break;
376          case Enum:
377                os << "enumeration ";
[8f6f47d7]378                if ( enumeration.constants ) {
[b87a5ed]379                        os << "with constants" << endl;
[8f6f47d7]380                        enumeration.constants->printList( os, indent + 2 );
[68cd1ce]381                } // if
[ca1a547]382                if ( enumeration.body ) {
[07ec1a2]383                        os << string( indent + 2, ' ' ) << " with body" << endl;
[ca1a547]384                } // if
[9e7236f4]385                if ( base ) {
386                        os << "for ";
387                        base->print( os, indent + 2 );
388                } // if
[b87a5ed]389                break;
[f7e4db27]390          case EnumConstant:
391                os << "enumeration constant ";
[b87a5ed]392                break;
393          case Symbolic:
[8f6f47d7]394                if ( symbolic.isTypedef ) {
[b87a5ed]395                        os << "typedef definition ";
396                } else {
397                        os << "type definition ";
[68cd1ce]398                } // if
[8f6f47d7]399                if ( symbolic.params ) {
[721f17a]400                        os << endl << string( indent + 2, ' ' ) << "with parameters" << endl;
[8f6f47d7]401                        symbolic.params->printList( os, indent + 2 );
[68cd1ce]402                } // if
[8f6f47d7]403                if ( symbolic.assertions ) {
[721f17a]404                        os << endl << string( indent + 2, ' ' ) << "with assertions" << endl;
[8f6f47d7]405                        symbolic.assertions->printList( os, indent + 4 );
[721f17a]406                        os << string( indent + 2, ' ' );
[68cd1ce]407                } // if
[b87a5ed]408                if ( base ) {
409                        os << "for ";
410                        base->print( os, indent + 2 );
[68cd1ce]411                } // if
[b87a5ed]412                break;
[f7e4db27]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;
[b87a5ed]421          case Tuple:
422                os << "tuple ";
[8f6f47d7]423                if ( tuple ) {
[07ec1a2]424                        os << "with members" << endl;
[8f6f47d7]425                        tuple->printList( os, indent + 2 );
[68cd1ce]426                } // if
[b87a5ed]427                break;
[f855545]428          case Basetypeof:
429                os << "base-";
[c45b304]430                #if defined(__GNUC__) && __GNUC__ >= 7
431                        __attribute__((fallthrough));
432                #endif
[66406f3]433                // FALL THROUGH
[b87a5ed]434          case Typeof:
435                os << "type-of expression ";
[8f6f47d7]436                if ( typeexpr ) {
437                        typeexpr->print( os, indent + 2 );
[68cd1ce]438                } // if
[b87a5ed]439                break;
[66406f3]440          case Vtable:
441                os << "vtable";
442                break;
[90c3b1c]443          case Builtin:
[9dc31c10]444                os << DeclarationNode::builtinTypeNames[builtintype];
[90c3b1c]445                break;
[f7e4db27]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;
[1db21619]456          default:
[5b639ee]457                os << "internal error: TypeData::print " << kind << endl;
[1db21619]458                assert( false );
[68cd1ce]459        } // switch
[413ad05]460} // TypeData::print
[51b7345]461
[7de22b28]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:
[f855545]475          case Basetypeof:
[7de22b28]476          case Builtin:
[93bbbc4]477          case Vtable:
[7de22b28]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
[201aeb9]493
[8c49c0e]494template< typename ForallList >
495void buildForall( const DeclarationNode * firstNode, ForallList &outputList ) {
[b87a5ed]496        buildList( firstNode, outputList );
[f0ecf9b]497        auto n = firstNode;
498        for ( typename ForallList::iterator i = outputList.begin(); i != outputList.end(); ++i, n = (DeclarationNode*)n->get_next() ) {
[a7c90d4]499                TypeDecl * td = static_cast<TypeDecl *>(*i);
[07de76b]500                if ( n->variable.tyClass == TypeDecl::Otype ) {
[6943f051]501                        // add assertion parameters to `type' tyvars in reverse order
502                        // add dtor:  void ^?{}(T *)
[413ad05]503                        FunctionType * dtorType = new FunctionType( Type::Qualifiers(), false );
[cda7889]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 ) );
[68fe077a]505                        td->get_assertions().push_front( new FunctionDecl( "^?{}", Type::StorageClasses(), LinkageSpec::Cforall, dtorType, nullptr ) );
[4cc4286]506
[a9a259c]507                        // add copy ctor:  void ?{}(T *, T)
[413ad05]508                        FunctionType * copyCtorType = new FunctionType( Type::Qualifiers(), false );
[cda7889]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 ) );
[68fe077a]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 ) );
[a9a259c]512
[6943f051]513                        // add default ctor:  void ?{}(T *)
[413ad05]514                        FunctionType * ctorType = new FunctionType( Type::Qualifiers(), false );
[cda7889]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 ) );
[68fe077a]516                        td->get_assertions().push_front( new FunctionDecl( "?{}", Type::StorageClasses(), LinkageSpec::Cforall, ctorType, nullptr ) );
[6943f051]517
518                        // add assignment operator:  T * ?=?(T *, T)
[413ad05]519                        FunctionType * assignType = new FunctionType( Type::Qualifiers(), false );
[cda7889]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 ) );
[68fe077a]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 ) );
[68cd1ce]524                } // if
525        } // for
[201aeb9]526} // buildForall
527
[51b7345]528
[413ad05]529Type * typebuild( const TypeData * td ) {
530        assert( td );
531        switch ( td->kind ) {
532          case TypeData::Unknown:
[07de76b]533                // fill in implicit int
534                return new BasicType( buildQualifiers( td ), BasicType::SignedInt );
[413ad05]535          case TypeData::Basic:
[07de76b]536                return buildBasicType( td );
[413ad05]537          case TypeData::Pointer:
[07de76b]538                return buildPointer( td );
[413ad05]539          case TypeData::Array:
[07de76b]540                return buildArray( td );
[ce8c12f]541          case TypeData::Reference:
[07de76b]542                return buildReference( td );
[413ad05]543          case TypeData::Function:
[07de76b]544                return buildFunction( td );
[413ad05]545          case TypeData::AggregateInst:
[07de76b]546                return buildAggInst( td );
[413ad05]547          case TypeData::EnumConstant:
[07de76b]548                return new EnumInstType( buildQualifiers( td ), "" );
[413ad05]549          case TypeData::SymbolicInst:
[07de76b]550                return buildSymbolicInst( td );
[413ad05]551          case TypeData::Tuple:
[07de76b]552                return buildTuple( td );
[413ad05]553          case TypeData::Typeof:
[f855545]554          case TypeData::Basetypeof:
[07de76b]555                return buildTypeof( td );
[93bbbc4]556          case TypeData::Vtable:
557                return buildVtable( td );
[413ad05]558          case TypeData::Builtin:
[07de76b]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
[c194661]567          case TypeData::GlobalScope:
[07de76b]568                return new GlobalScopeType();
569          case TypeData::Qualified:
570                return new QualifiedType( buildQualifiers( td ), typebuild( td->qualified.parent ), typebuild( td->qualified.child ) );
[413ad05]571          case TypeData::Symbolic:
572          case TypeData::Enum:
573          case TypeData::Aggregate:
[07de76b]574                assert( false );
[68cd1ce]575        } // switch
[c194661]576
[2298f728]577        return nullptr;
[413ad05]578} // typebuild
579
[201aeb9]580
[413ad05]581TypeData * typeextractAggregate( const TypeData * td, bool toplevel ) {
[2298f728]582        TypeData * ret = nullptr;
[51b7345]583
[413ad05]584        switch ( td->kind ) {
585          case TypeData::Aggregate:
[b2da0574]586                if ( ! toplevel && td->aggregate.body ) {
[413ad05]587                        ret = td->clone();
588                } // if
589                break;
590          case TypeData::Enum:
[b2da0574]591                if ( ! toplevel && td->enumeration.body ) {
[413ad05]592                        ret = td->clone();
593                } // if
594                break;
595          case TypeData::AggregateInst:
[8f6f47d7]596                if ( td->aggInst.aggregate ) {
597                        ret = typeextractAggregate( td->aggInst.aggregate, false );
[413ad05]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
[201aeb9]608
[413ad05]609Type::Qualifiers buildQualifiers( const TypeData * td ) {
[738e304]610        return td->qualifiers;
[413ad05]611} // buildQualifiers
[51b7345]612
[201aeb9]613
614static string genTSError( string msg, DeclarationNode::BasicType basictype ) {
[a16764a6]615        SemanticError( yylloc, string( "invalid type specifier \"" ) + msg + "\" for type \"" + DeclarationNode::basicTypeNames[basictype] + "\"." );
[201aeb9]616} // genTSError
617
[413ad05]618Type * buildBasicType( const TypeData * td ) {
[b87a5ed]619        BasicType::Kind ret;
620
[5b639ee]621        switch ( td->basictype ) {
622          case DeclarationNode::Void:
[201aeb9]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 );
[68cd1ce]628                } // if
[5b639ee]629                return new VoidType( buildQualifiers( td ) );
630                break;
631
632          case DeclarationNode::Bool:
633                if ( td->signedness != DeclarationNode::NoSignedness ) {
[201aeb9]634                        genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
[5b639ee]635                } // if
636                if ( td->length != DeclarationNode::NoLength ) {
[201aeb9]637                        genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
[68cd1ce]638                } // if
[51b7345]639
[e15853c]640                ret = BasicType::Bool;
[5b639ee]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.
[8c49c0e]647                static BasicType::Kind chartype[] = { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::Char };
[5b639ee]648
649                if ( td->length != DeclarationNode::NoLength ) {
[201aeb9]650                        genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
[5b639ee]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
[201aeb9]669          case DeclarationNode::Int128:
[4ee3b0c1]670                ret = td->signedness == DeclarationNode::Unsigned ? BasicType::UnsignedInt128 : BasicType::SignedInt128;
[201aeb9]671                if ( td->length != DeclarationNode::NoLength ) {
672                        genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
673                } // if
674                break;
675
[5b639ee]676          case DeclarationNode::Float:
677          case DeclarationNode::Double:
678          case DeclarationNode::LongDouble:                                     // not set until below
[e15853c]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:
[ba01b14]688                static BasicType::Kind floattype[2][12] = {
[e15853c]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, },
[5b639ee]691                };
692
693          FloatingPoint: ;
694                if ( td->signedness != DeclarationNode::NoSignedness ) {
[201aeb9]695                        genTSError( DeclarationNode::signednessNames[ td->signedness ], td->basictype );
[5b639ee]696                } // if
697                if ( td->length == DeclarationNode::Short || td->length == DeclarationNode::LongLong ) {
[201aeb9]698                        genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
[5b639ee]699                } // if
[4ee3b0c1]700                if ( td->basictype != DeclarationNode::Double && td->length == DeclarationNode::Long ) {
[201aeb9]701                        genTSError( DeclarationNode::lengthNames[ td->length ], td->basictype );
[5b639ee]702                } // if
[ba01b14]703                if ( td->complextype == DeclarationNode::Imaginary ) {
704                        genTSError( DeclarationNode::complexTypeNames[ td->complextype ], td->basictype );
705                } // if
[e15853c]706                if ( (td->basictype == DeclarationNode::uuFloat80 || td->basictype == DeclarationNode::uuFloat128) && td->complextype == DeclarationNode::Complex ) { // gcc unsupported
[ba01b14]707                        genTSError( DeclarationNode::complexTypeNames[ td->complextype ], td->basictype );
708                } // if
[5b639ee]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 ];
[ba01b14]714                //printf( "XXXX %d %d %d %d\n", td->complextype, td->basictype, DeclarationNode::Float, ret );
[5b639ee]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;
[2ee5426]726          default:
[201aeb9]727                assertf( false, "unknown basic type" );
[2ee5426]728                return nullptr;
[5b639ee]729        } // switch
730
731        BasicType * bt = new BasicType( buildQualifiers( td ), ret );
[413ad05]732        buildForall( td->forall, bt->get_forall() );
[b87a5ed]733        return bt;
[413ad05]734} // buildBasicType
[51b7345]735
[201aeb9]736
[413ad05]737PointerType * buildPointer( const TypeData * td ) {
738        PointerType * pt;
739        if ( td->base ) {
740                pt = new PointerType( buildQualifiers( td ), typebuild( td->base ) );
[b87a5ed]741        } else {
[413ad05]742                pt = new PointerType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
[68cd1ce]743        } // if
[413ad05]744        buildForall( td->forall, pt->get_forall() );
[b87a5ed]745        return pt;
[413ad05]746} // buildPointer
[51b7345]747
[201aeb9]748
[413ad05]749ArrayType * buildArray( const TypeData * td ) {
750        ArrayType * at;
751        if ( td->base ) {
[8f6f47d7]752                at = new ArrayType( buildQualifiers( td ), typebuild( td->base ), maybeBuild< Expression >( td->array.dimension ),
753                                                        td->array.isVarLen, td->array.isStatic );
[b87a5ed]754        } else {
[413ad05]755                at = new ArrayType( buildQualifiers( td ), new BasicType( Type::Qualifiers(), BasicType::SignedInt ),
[8f6f47d7]756                                                        maybeBuild< Expression >( td->array.dimension ), td->array.isVarLen, td->array.isStatic );
[68cd1ce]757        } // if
[413ad05]758        buildForall( td->forall, at->get_forall() );
[b87a5ed]759        return at;
[ce8c12f]760} // buildArray
761
[201aeb9]762
[ce8c12f]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
[51b7345]773
[201aeb9]774
[fa4805f]775AggregateDecl * buildAggregate( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
[413ad05]776        assert( td->kind == TypeData::Aggregate );
777        AggregateDecl * at;
[8f6f47d7]778        switch ( td->aggregate.kind ) {
[312029a]779          case AggregateDecl::Struct:
780          case AggregateDecl::Coroutine:
[0c730d9]781          case AggregateDecl::Exception:
[427854b]782          case AggregateDecl::Generator:
[312029a]783          case AggregateDecl::Monitor:
784          case AggregateDecl::Thread:
[fa4805f]785                at = new StructDecl( *td->aggregate.name, td->aggregate.kind, attributes, linkage );
[8f6f47d7]786                buildForall( td->aggregate.params, at->get_parameters() );
[b87a5ed]787                break;
[312029a]788          case AggregateDecl::Union:
[bd46af4]789                at = new UnionDecl( *td->aggregate.name, attributes, linkage );
[8f6f47d7]790                buildForall( td->aggregate.params, at->get_parameters() );
[b87a5ed]791                break;
[312029a]792          case AggregateDecl::Trait:
[bd46af4]793                at = new TraitDecl( *td->aggregate.name, attributes, linkage );
[8f6f47d7]794                buildList( td->aggregate.params, at->get_parameters() );
[b87a5ed]795                break;
796          default:
797                assert( false );
[68cd1ce]798        } // switch
[4e06c1e]799
[8f6f47d7]800        buildList( td->aggregate.fields, at->get_members() );
801        at->set_body( td->aggregate.body );
[b87a5ed]802
803        return at;
[413ad05]804} // buildAggregate
[51b7345]805
[201aeb9]806
[fa4805f]807ReferenceToType * buildComAggInst( const TypeData * type, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
[43c89a7]808        switch ( type->kind ) {
809          case TypeData::Enum: {
810                  if ( type->enumeration.body ) {
[bd46af4]811                          EnumDecl * typedecl = buildEnum( type, attributes, linkage );
[43c89a7]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 ) {
[fa4805f]820                          AggregateDecl * typedecl = buildAggregate( type, attributes, linkage );
[43c89a7]821                          switch ( type->aggregate.kind ) {
[312029a]822                                case AggregateDecl::Struct:
823                                case AggregateDecl::Coroutine:
824                                case AggregateDecl::Monitor:
825                                case AggregateDecl::Thread:
[43c89a7]826                                  ret = new StructInstType( buildQualifiers( type ), (StructDecl *)typedecl );
827                                  break;
[312029a]828                                case AggregateDecl::Union:
[43c89a7]829                                  ret = new UnionInstType( buildQualifiers( type ), (UnionDecl *)typedecl );
830                                  break;
[312029a]831                                case AggregateDecl::Trait:
[43c89a7]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 ) {
[312029a]840                                case AggregateDecl::Struct:
841                                case AggregateDecl::Coroutine:
842                                case AggregateDecl::Monitor:
843                                case AggregateDecl::Thread:
[43c89a7]844                                  ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
845                                  break;
[312029a]846                                case AggregateDecl::Union:
[43c89a7]847                                  ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
848                                  break;
[312029a]849                                case AggregateDecl::Trait:
[43c89a7]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
[201aeb9]863
[413ad05]864ReferenceToType * buildAggInst( const TypeData * td ) {
865        assert( td->kind == TypeData::AggregateInst );
[b87a5ed]866
[43c89a7]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 ) {
[312029a]876                        case AggregateDecl::Struct:
877                        case AggregateDecl::Coroutine:
878                        case AggregateDecl::Monitor:
879                        case AggregateDecl::Thread:
[43c89a7]880                          ret = new StructInstType( buildQualifiers( type ), *type->aggregate.name );
881                          break;
[312029a]882                        case AggregateDecl::Union:
[43c89a7]883                          ret = new UnionInstType( buildQualifiers( type ), *type->aggregate.name );
884                          break;
[312029a]885                        case AggregateDecl::Trait:
[43c89a7]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 );
[8f6f47d7]898        buildList( td->aggInst.params, ret->get_parameters() );
[413ad05]899        buildForall( td->forall, ret->get_forall() );
[b87a5ed]900        return ret;
[413ad05]901} // buildAggInst
902
[201aeb9]903
[1f370451]904NamedTypeDecl * buildSymbolic( const TypeData * td, std::list< Attribute * > attributes, const string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage ) {
[413ad05]905        assert( td->kind == TypeData::Symbolic );
906        NamedTypeDecl * ret;
907        assert( td->base );
[8f6f47d7]908        if ( td->symbolic.isTypedef ) {
[0b0f1dd]909                ret = new TypedefDecl( name, td->location, scs, typebuild( td->base ), linkage );
[b87a5ed]910        } else {
[f0ecf9b]911                ret = new TypeDecl( name, scs, typebuild( td->base ), TypeDecl::Dtype, true );
[68cd1ce]912        } // if
[8f6f47d7]913        buildList( td->symbolic.assertions, ret->get_assertions() );
[1f370451]914        ret->base->attributes.splice( ret->base->attributes.end(), attributes );
[b87a5ed]915        return ret;
[413ad05]916} // buildSymbolic
[51b7345]917
[201aeb9]918
[bd46af4]919EnumDecl * buildEnum( const TypeData * td, std::list< Attribute * > attributes, LinkageSpec::Spec linkage ) {
[413ad05]920        assert( td->kind == TypeData::Enum );
[f238fcc2]921        Type * baseType = td->base ? typebuild(td->base) : nullptr;
[b0d9ff7]922        EnumDecl * ret = new EnumDecl( *td->enumeration.name, attributes, td->enumeration.typed, linkage, baseType );
[f238fcc2]923        buildList( td->enumeration.constants, ret->get_members() );
[2298f728]924        list< Declaration * >::iterator members = ret->get_members().begin();
[f135b50]925        for ( const DeclarationNode * cur = td->enumeration.constants; cur != nullptr; cur = dynamic_cast< DeclarationNode * >( cur->get_next() ), ++members ) {
[1e30df7]926                if ( cur->enumInLine ) {
[e874605]927                        // Do Nothing
928                } else if ( ret->isTyped && !ret->base && cur->has_enumeratorValue() ) {
[b0d9ff7]929                        SemanticError( td->location, "Enumerator of enum(void) cannot have an explicit initializer value." );
930                } else if ( cur->has_enumeratorValue() ) {
[413ad05]931                        ObjectDecl * member = dynamic_cast< ObjectDecl * >(* members);
[e4d829b]932                        member->set_init( new SingleInit( maybeMoveBuild< Expression >( cur->consume_enumeratorValue() ) ) );
[9e7236f4]933                } else if ( !cur->initializer ) {
[f238fcc2]934                        if ( baseType && (!dynamic_cast<BasicType *>(baseType) || !dynamic_cast<BasicType *>(baseType)->isWholeNumber())) {
[b0d9ff7]935                                SemanticError( td->location, "Enumerators of an non-integer typed enum must be explicitly initialized." );
[f238fcc2]936                        }
[9e7236f4]937                }
938                // else cur is a List Initializer and has been set as init in buildList()
939                // if
[90c3b1c]940        } // for
[9e7236f4]941        ret->set_body( td->enumeration.body );
[b87a5ed]942        return ret;
[413ad05]943} // buildEnum
[51b7345]944
[201aeb9]945
[413ad05]946TypeInstType * buildSymbolicInst( const TypeData * td ) {
947        assert( td->kind == TypeData::SymbolicInst );
[2298f728]948        TypeInstType * ret = new TypeInstType( buildQualifiers( td ), *td->symbolic.name, false );
[8f6f47d7]949        buildList( td->symbolic.actuals, ret->get_parameters() );
[413ad05]950        buildForall( td->forall, ret->get_forall() );
[b87a5ed]951        return ret;
[413ad05]952} // buildSymbolicInst
[51b7345]953
[201aeb9]954
[413ad05]955TupleType * buildTuple( const TypeData * td ) {
956        assert( td->kind == TypeData::Tuple );
[62423350]957        std::list< Type * > types;
958        buildTypeList( td->tuple, types );
959        TupleType * ret = new TupleType( buildQualifiers( td ), types );
[413ad05]960        buildForall( td->forall, ret->get_forall() );
[b87a5ed]961        return ret;
[413ad05]962} // buildTuple
963
[201aeb9]964
[413ad05]965TypeofType * buildTypeof( const TypeData * td ) {
[f855545]966        assert( td->kind == TypeData::Typeof || td->kind == TypeData::Basetypeof );
[413ad05]967        assert( td->typeexpr );
[8f6f47d7]968        // assert( td->typeexpr->expr );
[9f77301]969        return new TypeofType{ buildQualifiers( td ), td->typeexpr->build(), td->kind == TypeData::Basetypeof };
[413ad05]970} // buildTypeof
971
[201aeb9]972
[93bbbc4]973VTableType * buildVtable( const TypeData * td ) {
974        assert( td->base );
975        return new VTableType{ buildQualifiers( td ), typebuild( td->base ) };
976} // buildVtable
977
978
[e612146c]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 ) {
[413ad05]980        if ( td->kind == TypeData::Function ) {
[ca1a547]981                if ( td->function.idList ) {                                    // KR function ?
982                        buildKRFunction( td->function );                        // transform into C11 function
[3a5131ed]983                } // if
984
[413ad05]985                FunctionDecl * decl;
[ca1a547]986                Statement * stmt = maybeBuild<Statement>( td->function.body );
[a7c90d4]987                CompoundStmt * body = dynamic_cast< CompoundStmt * >( stmt );
988                decl = new FunctionDecl( name, scs, linkage, buildFunction( td ), body, attributes, funcSpec );
[c453ac4]989                buildList( td->function.withExprs, decl->withExprs );
[58dd019]990                return decl->set_asmName( asmName );
[413ad05]991        } else if ( td->kind == TypeData::Aggregate ) {
[fa4805f]992                return buildAggregate( td, attributes, linkage );
[413ad05]993        } else if ( td->kind == TypeData::Enum ) {
[bd46af4]994                return buildEnum( td, attributes, linkage );
[413ad05]995        } else if ( td->kind == TypeData::Symbolic ) {
[1f370451]996                return buildSymbolic( td, attributes, name, scs, linkage );
[413ad05]997        } else {
[a7c90d4]998                return (new ObjectDecl( name, scs, linkage, bitfieldWidth, typebuild( td ), init, attributes ))->set_asmName( asmName );
[413ad05]999        } // if
[2298f728]1000        return nullptr;
[413ad05]1001} // buildDecl
1002
[201aeb9]1003
[413ad05]1004FunctionType * buildFunction( const TypeData * td ) {
1005        assert( td->kind == TypeData::Function );
[2a8427c6]1006        FunctionType * ft = new FunctionType( buildQualifiers( td ), ! td->function.params || td->function.params->hasEllipsis );
[704d11e]1007        buildList( td->function.params, ft->parameters );
1008        buildForall( td->forall, ft->forall );
[413ad05]1009        if ( td->base ) {
1010                switch ( td->base->kind ) {
1011                  case TypeData::Tuple:
[704d11e]1012                        buildList( td->base->tuple, ft->returnVals );
[413ad05]1013                        break;
1014                  default:
[ddfd945]1015                        ft->get_returnVals().push_back( dynamic_cast< DeclarationWithType * >( buildDecl( td->base, "", Type::StorageClasses(), nullptr, Type::FuncSpecifiers(), LinkageSpec::Cforall, nullptr ) ) );
[413ad05]1016                } // switch
1017        } else {
[68fe077a]1018                ft->get_returnVals().push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) );
[413ad05]1019        } // if
1020        return ft;
1021} // buildFunction
[b87a5ed]1022
[201aeb9]1023
[a7c4921]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
[3a5131ed]1037void buildKRFunction( const TypeData::Function_t & function ) {
1038        assert( ! function.params );
[a7c4921]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
[a7c90d4]1042                for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
[3a5131ed]1043                        if ( *decl->name == *param->name ) {
[a7c4921]1044                                // type set => parameter name already transformed by a declaration names so there is a duplicate
1045                                // declaration name attempting a second transformation
[a16764a6]1046                                if ( param->type ) SemanticError( param->location, string( "duplicate declaration name " ) + *param->name );
[a7c4921]1047                                // declaration type reset => declaration already transformed by a parameter name so there is a duplicate
1048                                // parameter name attempting a second transformation
[a16764a6]1049                                if ( ! decl->type ) SemanticError( param->location, string( "duplicate parameter name " ) + *param->name );
[a7c4921]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
[3a5131ed]1053                        } // if
1054                } // for
[a7c4921]1055                // declaration type still set => type not moved to a matching parameter so there is a missing parameter name
[a16764a6]1056                if ( decl->type ) SemanticError( decl->location, string( "missing name in parameter list " ) + *decl->name );
[3a5131ed]1057        } // for
[a7c4921]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
[a7c90d4]1063        for ( DeclarationNode * param = function.idList; param != nullptr; param = dynamic_cast< DeclarationNode * >( param->get_next() ) ) {
[ca1a547]1064                if ( ! param->type ) {                                                  // generate type int for empty parameter type
[3a5131ed]1065                        param->type = new TypeData( TypeData::Basic );
1066                        param->type->basictype = DeclarationNode::Int;
1067                } // if
1068        } // for
1069
[a7c4921]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
[3a5131ed]1074} // buildKRFunction
1075
[b87a5ed]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.