source: src/Parser/TypeData.cc @ 8d7bef2

new-envwith_gc
Last change on this file since 8d7bef2 was a16764a6, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Changed warning system to prepare for toggling warnings

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