source: src/Parser/TypeData.cc @ 3f8dd01

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 3f8dd01 was e612146c, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

third attempt at user-defined literals

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