Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parser/ExpressionNode.cc

    r6611177 r46da46b  
    99// Author           : Peter A. Buhr
    1010// Created On       : Sat May 16 13:17:07 2015
    11 // Last Modified By : Andrew Beach
    12 // Last Modified On : Tue Apr  4 11:07:00 2023
    13 // Update Count     : 1083
    14 //
    15 
    16 #include "ExpressionNode.h"
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Sat Aug  7 09:18:56 2021
     13// Update Count     : 1077
     14//
    1715
    1816#include <cassert>                 // for assert
     
    2321#include <string>                  // for string, operator+, operator==
    2422
    25 #include "AST/Expr.hpp"            // for NameExpr
    26 #include "AST/Type.hpp"            // for BaseType, SueInstType
    2723#include "Common/SemanticError.h"  // for SemanticError
    2824#include "Common/utility.h"        // for maybeMoveBuild, maybeBuild, CodeLo...
    29 #include "DeclarationNode.h"       // for DeclarationNode
    30 #include "InitializerNode.h"       // for InitializerNode
     25#include "ParseNode.h"             // for ExpressionNode, maybeMoveBuildType
     26#include "SynTree/Constant.h"      // for Constant
     27#include "SynTree/Declaration.h"   // for EnumDecl, StructDecl, UnionDecl
     28#include "SynTree/Expression.h"    // for Expression, ConstantExpr, NameExpr
     29#include "SynTree/Statement.h"     // for CompoundStmt, Statement
     30#include "SynTree/Type.h"          // for BasicType, Type, Type::Qualifiers
    3131#include "parserutility.h"         // for notZeroExpr
     32
     33class Initializer;
    3234
    3335using namespace std;
     
    4648// because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their
    4749// type.
     50
     51extern const Type::Qualifiers noQualifiers;                             // no qualifiers on constants
    4852
    4953// static inline bool checkH( char c ) { return c == 'h' || c == 'H'; }
     
    6771        size_t end = str.length() - 1;
    6872        if ( posn == end ) { type = 3; return; }                        // no length after 'l' => long
    69 
     73       
    7074        string::size_type next = posn + 1;                                      // advance to length
    7175        if ( str[next] == '3' ) {                                                       // 32
     
    118122                if ( str[i] == '1' ) v |= 1;
    119123                i += 1;
    120                 if ( i == last - 1 || (str[i] != '0' && str[i] != '1') ) break;
     124          if ( i == last - 1 || (str[i] != '0' && str[i] != '1') ) break;
    121125                v <<= 1;
    122126        } // for
    123127} // scanbin
    124128
    125 ast::Expr * build_constantInteger(
    126                 const CodeLocation & location, string & str ) {
    127         static const ast::BasicType::Kind kind[2][6] = {
     129Expression * build_constantInteger( string & str ) {
     130        static const BasicType::Kind kind[2][6] = {
    128131                // short (h) must be before char (hh) because shorter type has the longer suffix
    129                 { ast::BasicType::ShortSignedInt, ast::BasicType::SignedChar, ast::BasicType::SignedInt, ast::BasicType::LongSignedInt, ast::BasicType::LongLongSignedInt, /* BasicType::SignedInt128 */ ast::BasicType::LongLongSignedInt, },
    130                 { ast::BasicType::ShortUnsignedInt, ast::BasicType::UnsignedChar, ast::BasicType::UnsignedInt, ast::BasicType::LongUnsignedInt, ast::BasicType::LongLongUnsignedInt, /* BasicType::UnsignedInt128 */ ast::BasicType::LongLongUnsignedInt, },
     132                { BasicType::ShortSignedInt, BasicType::SignedChar, BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt, /* BasicType::SignedInt128 */ BasicType::LongLongSignedInt, },
     133                { BasicType::ShortUnsignedInt, BasicType::UnsignedChar, BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, /* BasicType::UnsignedInt128 */ BasicType::LongLongUnsignedInt, },
    131134        };
    132135
     
    138141        string str2( "0x0" );
    139142        unsigned long long int v, v2 = 0;                                       // converted integral value
    140         ast::Expr * ret, * ret2;
     143        Expression * ret, * ret2;
    141144
    142145        int type = -1;                                                                          // 0 => short, 1 => char, 2 => int, 3 => long int, 4 => long long int, 5 => int128
     
    146149        // special constants
    147150        if ( str == "0" ) {
    148                 ret = new ast::ConstantExpr( location, new ast::ZeroType(), str, 0 );
     151                ret = new ConstantExpr( Constant( (Type *)new ZeroType( noQualifiers ), str, (unsigned long long int)0 ) );
    149152                goto CLEANUP;
    150153        } // if
    151154        if ( str == "1" ) {
    152                 ret = new ast::ConstantExpr( location, new ast::OneType(), str, 1 );
     155                ret = new ConstantExpr( Constant( (Type *)new OneType( noQualifiers ), str, (unsigned long long int)1 ) );
    153156                goto CLEANUP;
    154157        } // if
     158
     159        string::size_type posn;
    155160
    156161        // 'u' can appear before or after length suffix
     
    161166        } else {
    162167                // At least one digit in integer constant, so safe to backup while looking for suffix.
    163                 // This declaration and the comma expressions in the conditions mimic
    164                 // the declare and check pattern allowed in later compiler versions.
    165                 // (Only some early compilers/C++ standards do not support it.)
    166                 string::size_type posn;
    167                 // pointer value
    168                 if ( posn = str.find_last_of( "pP" ), posn != string::npos ) {
    169                         ltype = 5; str.erase( posn, 1 );
    170                 // size_t
    171                 } else if ( posn = str.find_last_of( "zZ" ), posn != string::npos ) {
    172                         Unsigned = true; type = 2; ltype = 4; str.erase( posn, 1 );
    173                 // signed char
    174                 } else if ( posn = str.rfind( "hh" ), posn != string::npos ) {
    175                         type = 1; str.erase( posn, 2 );
    176                 // signed char
    177                 } else if ( posn = str.rfind( "HH" ), posn != string::npos ) {
    178                         type = 1; str.erase( posn, 2 );
    179                 // short
    180                 } else if ( posn = str.find_last_of( "hH" ), posn != string::npos ) {
    181                         type = 0; str.erase( posn, 1 );
    182                 // int (natural number)
    183                 } else if ( posn = str.find_last_of( "nN" ), posn != string::npos ) {
    184                         type = 2; str.erase( posn, 1 );
    185                 } else if ( str.rfind( "ll" ) != string::npos || str.rfind( "LL" ) != string::npos ) {
    186                         type = 4;
    187                 } else {
    188                         lnthSuffix( str, type, ltype );
    189                 } // if
     168
     169                posn = str.find_last_of( "pP" );                                // pointer value
     170                if ( posn != string::npos ) { ltype = 5; str.erase( posn, 1 ); goto FINI; }
     171
     172                posn = str.find_last_of( "zZ" );                                // size_t
     173                if ( posn != string::npos ) { Unsigned = true; type = 2; ltype = 4; str.erase( posn, 1 ); goto FINI; }
     174
     175                posn = str.rfind( "hh" );                                               // char
     176                if ( posn != string::npos ) { type = 1; str.erase( posn, 2 ); goto FINI; }
     177
     178                posn = str.rfind( "HH" );                                               // char
     179                if ( posn != string::npos ) { type = 1; str.erase( posn, 2 ); goto FINI; }
     180
     181                posn = str.find_last_of( "hH" );                                // short
     182                if ( posn != string::npos ) { type = 0; str.erase( posn, 1 ); goto FINI; }
     183
     184                posn = str.find_last_of( "nN" );                                // int (natural number)
     185                if ( posn != string::npos ) { type = 2; str.erase( posn, 1 ); goto FINI; }
     186
     187                if ( str.rfind( "ll" ) != string::npos || str.rfind( "LL" ) != string::npos ) { type = 4; goto FINI; }
     188
     189                lnthSuffix( str, type, ltype );                                 // must be after check for "ll"
     190          FINI: ;
    190191        } // if
    191192
     
    195196        if ( type == 5 ) SemanticError( yylloc, "int128 constant is not supported on this target " + str );
    196197#endif // ! __SIZEOF_INT128__
    197 
     198       
    198199        if ( str[0] == '0' ) {                                                          // radix character ?
    199200                dec = false;
     
    205206                                unsigned int len = str.length();
    206207                                if ( len > (2 + 16 + 16) ) SemanticError( yylloc, "128-bit hexadecimal constant to large " + str );
    207                                 // hex digits < 2^64
    208                                 if ( len > (2 + 16) ) {
    209                                         str2 = "0x" + str.substr( len - 16 );
    210                                         sscanf( (char *)str2.c_str(), "%llx", &v2 );
    211                                         str = str.substr( 0, len - 16 );
    212                                 } // if
     208                          if ( len <= (2 + 16) ) goto FHEX1;            // hex digits < 2^64
     209                                str2 = "0x" + str.substr( len - 16 );
     210                                sscanf( (char *)str2.c_str(), "%llx", &v2 );
     211                                str = str.substr( 0, len - 16 );
     212                          FHEX1: ;
    213213                                sscanf( (char *)str.c_str(), "%llx", &v );
    214214#endif // __SIZEOF_INT128__
     
    301301
    302302        // Constant type is correct for overload resolving.
    303         ret = new ast::ConstantExpr( location,
    304                 new ast::BasicType( kind[Unsigned][type] ), str, v );
     303        ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[Unsigned][type] ), str, v ) );
    305304        if ( Unsigned && type < 2 ) {                                           // hh or h, less than int ?
    306305                // int i = -1uh => 65535 not -1, so cast is necessary for unsigned, which unfortunately eliminates warnings for large values.
    307                 ret = new ast::CastExpr( location,
    308                         ret,
    309                         new ast::BasicType( kind[Unsigned][type] ),
    310                         ast::ExplicitCast );
     306                ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false );
    311307        } else if ( ltype != -1 ) {                                                     // explicit length ?
    312308                if ( ltype == 6 ) {                                                             // int128, (int128)constant
    313                         ret2 = new ast::ConstantExpr( location,
    314                                 new ast::BasicType( ast::BasicType::LongLongSignedInt ),
    315                                 str2,
    316                                 v2 );
    317                         ret = build_compoundLiteral( location,
    318                                 DeclarationNode::newBasicType(
    319                                         DeclarationNode::Int128
    320                                 )->addType(
    321                                         DeclarationNode::newSignedNess( DeclarationNode::Unsigned ) ),
    322                                 new InitializerNode(
    323                                         (InitializerNode *)(new InitializerNode( new ExpressionNode( v2 == 0 ? ret2 : ret ) ))->set_last( new InitializerNode( new ExpressionNode( v2 == 0 ? ret : ret2 ) ) ), true )
    324                         );
     309//                      ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[Unsigned][type] ), false );
     310                        ret2 = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::LongLongSignedInt ), str2, v2 ) );
     311                        ret = build_compoundLiteral( DeclarationNode::newBasicType( DeclarationNode::Int128 )->addType( DeclarationNode::newSignedNess( DeclarationNode::Unsigned ) ),
     312                                                                                 new InitializerNode( (InitializerNode *)(new InitializerNode( new ExpressionNode( v2 == 0 ? ret2 : ret ) ))->set_last( new InitializerNode( new ExpressionNode( v2 == 0 ? ret : ret2 ) ) ), true ) );
    325313                } else {                                                                                // explicit length, (length_type)constant
    326                         ret = new ast::CastExpr( location,
    327                                 ret,
    328                                 new ast::TypeInstType( lnthsInt[Unsigned][ltype], ast::TypeDecl::Dtype ),
    329                                 ast::ExplicitCast );
     314                        ret = new CastExpr( ret, new TypeInstType( Type::Qualifiers(), lnthsInt[Unsigned][ltype], false ), false );
    330315                        if ( ltype == 5 ) {                                                     // pointer, intptr( (uintptr_t)constant )
    331                                 ret = build_func( location,
    332                                         new ExpressionNode(
    333                                                 build_varref( location, new string( "intptr" ) ) ),
    334                                         new ExpressionNode( ret ) );
     316                                ret = build_func( new ExpressionNode( build_varref( new string( "intptr" ) ) ), new ExpressionNode( ret ) );
    335317                        } // if
    336318                } // if
     
    376358
    377359
    378 ast::Expr * build_constantFloat(
    379                 const CodeLocation & location, string & str ) {
    380         static const ast::BasicType::Kind kind[2][12] = {
    381                 { ast::BasicType::Float, ast::BasicType::Double, ast::BasicType::LongDouble, ast::BasicType::uuFloat80, ast::BasicType::uuFloat128, ast::BasicType::uFloat16, ast::BasicType::uFloat32, ast::BasicType::uFloat32x, ast::BasicType::uFloat64, ast::BasicType::uFloat64x, ast::BasicType::uFloat128, ast::BasicType::uFloat128x },
    382                 { ast::BasicType::FloatComplex, ast::BasicType::DoubleComplex, ast::BasicType::LongDoubleComplex, ast::BasicType::NUMBER_OF_BASIC_TYPES, ast::BasicType::NUMBER_OF_BASIC_TYPES, ast::BasicType::uFloat16Complex, ast::BasicType::uFloat32Complex, ast::BasicType::uFloat32xComplex, ast::BasicType::uFloat64Complex, ast::BasicType::uFloat64xComplex, ast::BasicType::uFloat128Complex, ast::BasicType::uFloat128xComplex },
     360Expression * build_constantFloat( string & str ) {
     361        static const BasicType::Kind kind[2][12] = {
     362                { BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::uuFloat80, BasicType::uuFloat128, BasicType::uFloat16, BasicType::uFloat32, BasicType::uFloat32x, BasicType::uFloat64, BasicType::uFloat64x, BasicType::uFloat128, BasicType::uFloat128x },
     363                { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::NUMBER_OF_BASIC_TYPES, BasicType::NUMBER_OF_BASIC_TYPES, BasicType::uFloat16Complex, BasicType::uFloat32Complex, BasicType::uFloat32xComplex, BasicType::uFloat64Complex, BasicType::uFloat64xComplex, BasicType::uFloat128Complex, BasicType::uFloat128xComplex },
    383364        };
    384365
     
    417398
    418399        assert( 0 <= type && type < 12 );
    419         ast::Expr * ret = new ast::ConstantExpr( location,
    420                 new ast::BasicType( kind[complx][type] ),
    421                 str,
    422                 v );
    423         // explicit length ?
    424         if ( explnth ) {
    425                 ret = new ast::CastExpr( location,
    426                         ret,
    427                         new ast::BasicType( kind[complx][type] ),
    428                         ast::ExplicitCast );
     400        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, kind[complx][type] ), str, v ) );
     401        if ( explnth ) {                                                                        // explicit length ?
     402                ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[complx][type] ), false );
    429403        } // if
    430404
     
    441415} // sepString
    442416
    443 ast::Expr * build_constantChar( const CodeLocation & location, string & str ) {
     417Expression * build_constantChar( string & str ) {
    444418        string units;                                                                           // units
    445419        sepString( str, units, '\'' );                                          // separate constant from units
    446420
    447         ast::Expr * ret = new ast::ConstantExpr( location,
    448                 new ast::BasicType( ast::BasicType::Char ),
    449                 str,
    450                 (unsigned long long int)(unsigned char)str[1] );
     421        Expression * ret = new ConstantExpr( Constant( new BasicType( noQualifiers, BasicType::Char ), str, (unsigned long long int)(unsigned char)str[1] ) );
    451422        if ( units.length() != 0 ) {
    452                 ret = new ast::UntypedExpr( location,
    453                         new ast::NameExpr( location, units ),
    454                         { ret } );
     423                ret = new UntypedExpr( new NameExpr( units ), { ret } );
    455424        } // if
    456425
     
    459428} // build_constantChar
    460429
    461 ast::Expr * build_constantStr(
    462                 const CodeLocation & location,
    463                 string & str ) {
     430Expression * build_constantStr( string & str ) {
    464431        assert( str.length() > 0 );
    465432        string units;                                                                           // units
    466433        sepString( str, units, '"' );                                           // separate constant from units
    467434
    468         ast::Type * strtype;
     435        Type * strtype;
    469436        switch ( str[0] ) {                                                                     // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1
    470         case 'u':
     437          case 'u':
    471438                if ( str[1] == '8' ) goto Default;                              // utf-8 characters => array of char
    472439                // lookup type of associated typedef
    473                 strtype = new ast::TypeInstType( "char16_t", ast::TypeDecl::Dtype );
     440                strtype = new TypeInstType( Type::Qualifiers( ), "char16_t", false );
    474441                break;
    475         case 'U':
    476                 strtype = new ast::TypeInstType( "char32_t", ast::TypeDecl::Dtype );
     442          case 'U':
     443                strtype = new TypeInstType( Type::Qualifiers( ), "char32_t", false );
    477444                break;
    478         case 'L':
    479                 strtype = new ast::TypeInstType( "wchar_t", ast::TypeDecl::Dtype );
     445          case 'L':
     446                strtype = new TypeInstType( Type::Qualifiers( ), "wchar_t", false );
    480447                break;
    481         Default:                                                                                        // char default string type
    482         default:
    483                 strtype = new ast::BasicType( ast::BasicType::Char );
     448          Default:                                                                                      // char default string type
     449          default:
     450                strtype = new BasicType( Type::Qualifiers( ), BasicType::Char );
    484451        } // switch
    485         ast::ArrayType * at = new ast::ArrayType(
    486                 strtype,
    487                 // Length is adjusted: +1 for '\0' and -2 for '"'
    488                 ast::ConstantExpr::from_ulong( location, str.size() + 1 - 2 ),
    489                 ast::FixedLen,
    490                 ast::DynamicDim );
    491         ast::Expr * ret = new ast::ConstantExpr( location, at, str, std::nullopt );
     452        ArrayType * at = new ArrayType( noQualifiers, strtype,
     453                                                                        new ConstantExpr( Constant::from_ulong( str.size() + 1 - 2 ) ), // +1 for '\0' and -2 for '"'
     454                                                                        false, false );
     455        Expression * ret = new ConstantExpr( Constant( at, str, std::nullopt ) );
    492456        if ( units.length() != 0 ) {
    493                 ret = new ast::UntypedExpr( location,
    494                         new ast::NameExpr( location, units ),
    495                         { ret } );
     457                ret = new UntypedExpr( new NameExpr( units ), { ret } );
    496458        } // if
    497459
     
    500462} // build_constantStr
    501463
    502 ast::Expr * build_field_name_FLOATING_FRACTIONconstant(
    503                 const CodeLocation & location, const string & str ) {
     464Expression * build_field_name_FLOATING_FRACTIONconstant( const string & str ) {
    504465        if ( str.find_first_not_of( "0123456789", 1 ) != string::npos ) SemanticError( yylloc, "invalid tuple index " + str );
    505         ast::Expr * ret = build_constantInteger( location,
    506                 *new string( str.substr(1) ) );
     466        Expression * ret = build_constantInteger( *new string( str.substr(1) ) );
    507467        delete &str;
    508468        return ret;
    509469} // build_field_name_FLOATING_FRACTIONconstant
    510470
    511 ast::Expr * build_field_name_FLOATING_DECIMALconstant(
    512                 const CodeLocation & location, const string & str ) {
     471Expression * build_field_name_FLOATING_DECIMALconstant( const string & str ) {
    513472        if ( str[str.size() - 1] != '.' ) SemanticError( yylloc, "invalid tuple index " + str );
    514         ast::Expr * ret = build_constantInteger(
    515                 location, *new string( str.substr( 0, str.size()-1 ) ) );
     473        Expression * ret = build_constantInteger( *new string( str.substr( 0, str.size()-1 ) ) );
    516474        delete &str;
    517475        return ret;
    518476} // build_field_name_FLOATING_DECIMALconstant
    519477
    520 ast::Expr * build_field_name_FLOATINGconstant( const CodeLocation & location,
    521                 const string & str ) {
     478Expression * build_field_name_FLOATINGconstant( const string & str ) {
    522479        // str is of the form A.B -> separate at the . and return member expression
    523480        int a, b;
     
    525482        stringstream ss( str );
    526483        ss >> a >> dot >> b;
    527         auto ret = new ast::UntypedMemberExpr( location,
    528                 ast::ConstantExpr::from_int( location, b ),
    529                 ast::ConstantExpr::from_int( location, a )
    530         );
     484        UntypedMemberExpr * ret = new UntypedMemberExpr( new ConstantExpr( Constant::from_int( b ) ), new ConstantExpr( Constant::from_int( a ) ) );
    531485        delete &str;
    532486        return ret;
    533487} // build_field_name_FLOATINGconstant
    534488
    535 ast::Expr * make_field_name_fraction_constants( const CodeLocation & location,
    536                 ast::Expr * fieldName,
    537                 ast::Expr * fracts ) {
    538         if ( nullptr == fracts ) {
    539                 return fieldName;
    540         } else if ( auto memberExpr = dynamic_cast<ast::UntypedMemberExpr *>( fracts ) ) {
    541                 memberExpr->member = make_field_name_fraction_constants( location,
    542                         fieldName,
    543                         ast::mutate( memberExpr->aggregate.get() ) );
    544                 return memberExpr;
    545         } else {
    546                 return new ast::UntypedMemberExpr( location, fracts, fieldName );
    547         } // if
     489Expression * make_field_name_fraction_constants( Expression * fieldName, Expression * fracts ) {
     490        if ( fracts ) {
     491                if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( fracts ) ) {
     492                        memberExpr->set_member( make_field_name_fraction_constants( fieldName, memberExpr->get_aggregate() ) );
     493                        return memberExpr;
     494                } else {
     495                        return new UntypedMemberExpr( fracts, fieldName );
     496                } // if
     497        } // if
     498        return fieldName;
    548499} // make_field_name_fraction_constants
    549500
    550 ast::Expr * build_field_name_fraction_constants( const CodeLocation & location,
    551                 ast::Expr * fieldName,
    552                 ExpressionNode * fracts ) {
    553         return make_field_name_fraction_constants( location, fieldName, maybeMoveBuild( fracts ) );
     501Expression * build_field_name_fraction_constants( Expression * fieldName, ExpressionNode * fracts ) {
     502        return make_field_name_fraction_constants( fieldName, maybeMoveBuild< Expression >( fracts ) );
    554503} // build_field_name_fraction_constants
    555504
    556 ast::NameExpr * build_varref( const CodeLocation & location,
    557                 const string * name ) {
    558         ast::NameExpr * expr = new ast::NameExpr( location, *name );
     505NameExpr * build_varref( const string * name ) {
     506        NameExpr * expr = new NameExpr( *name );
    559507        delete name;
    560508        return expr;
    561509} // build_varref
    562510
    563 ast::QualifiedNameExpr * build_qualified_expr( const CodeLocation & location,
    564                 const DeclarationNode * decl_node,
    565                 const ast::NameExpr * name ) {
    566         ast::Decl * newDecl = maybeBuild( decl_node );
    567         if ( ast::DeclWithType * newDeclWithType = dynamic_cast<ast::DeclWithType *>( newDecl ) ) {
    568                 if ( const ast::Type * t = newDeclWithType->get_type() ) {
    569                         if ( auto typeInst = dynamic_cast<const ast::TypeInstType *>( t ) ) {
    570                                 newDecl = new ast::EnumDecl( location, typeInst->name );
     511QualifiedNameExpr * build_qualified_expr( const DeclarationNode * decl_node, const NameExpr * name ) {
     512        Declaration * newDecl = maybeBuild< Declaration >(decl_node);
     513        if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) {
     514                const Type * t = newDeclWithType->get_type();
     515                if ( t ) {
     516                        if ( const TypeInstType * typeInst = dynamic_cast<const TypeInstType *>( t ) ) {
     517                                newDecl= new EnumDecl( typeInst->name );
    571518                        }
    572519                }
    573520        }
    574         return new ast::QualifiedNameExpr( location, newDecl, name->name );
     521        return new QualifiedNameExpr( newDecl, name->name );
    575522}
    576523
    577 ast::QualifiedNameExpr * build_qualified_expr( const CodeLocation & location,
    578                 const ast::EnumDecl * decl,
    579                 const ast::NameExpr * name ) {
    580         return new ast::QualifiedNameExpr( location, decl, name->name );
     524QualifiedNameExpr * build_qualified_expr( const EnumDecl * decl_node, const NameExpr * name ) {
     525        EnumDecl * newDecl = const_cast< EnumDecl * >( decl_node );
     526        return new QualifiedNameExpr( newDecl, name->name );
    581527}
    582528
    583 ast::DimensionExpr * build_dimensionref( const CodeLocation & location,
    584                 const string * name ) {
    585         ast::DimensionExpr * expr = new ast::DimensionExpr( location, *name );
     529DimensionExpr * build_dimensionref( const string * name ) {
     530        DimensionExpr * expr = new DimensionExpr( *name );
    586531        delete name;
    587532        return expr;
     
    599544}; // OperName
    600545
    601 ast::Expr * build_cast( const CodeLocation & location,
    602                 DeclarationNode * decl_node,
    603                 ExpressionNode * expr_node ) {
    604         ast::Type * targetType = maybeMoveBuildType( decl_node );
    605         if ( dynamic_cast<ast::VoidType *>( targetType ) ) {
     546Expression * build_cast( DeclarationNode * decl_node, ExpressionNode * expr_node, CastExpr::CastKind kind ) {
     547        Type * targetType = maybeMoveBuildType( decl_node );
     548        if ( dynamic_cast< VoidType * >( targetType ) ) {
    606549                delete targetType;
    607                 return new ast::CastExpr( location,
    608                         maybeMoveBuild( expr_node ),
    609                         ast::ExplicitCast );
     550                return new CastExpr( maybeMoveBuild< Expression >(expr_node), false, kind );
    610551        } else {
    611                 return new ast::CastExpr( location,
    612                         maybeMoveBuild( expr_node ),
    613                         targetType,
    614                         ast::ExplicitCast );
     552                return new CastExpr( maybeMoveBuild< Expression >(expr_node), targetType, false, kind );
    615553        } // if
    616554} // build_cast
    617555
    618 ast::Expr * build_keyword_cast( const CodeLocation & location,
    619                 ast::AggregateDecl::Aggregate target,
    620                 ExpressionNode * expr_node ) {
    621         return new ast::KeywordCastExpr( location,
    622                 maybeMoveBuild( expr_node ),
    623                 target
    624         );
     556Expression * build_keyword_cast( AggregateDecl::Aggregate target, ExpressionNode * expr_node ) {
     557        return new KeywordCastExpr( maybeMoveBuild< Expression >(expr_node), target );
    625558}
    626559
    627 ast::Expr * build_virtual_cast( const CodeLocation & location,
    628                 DeclarationNode * decl_node,
    629                 ExpressionNode * expr_node ) {
    630         return new ast::VirtualCastExpr( location,
    631                 maybeMoveBuild( expr_node ),
    632                 maybeMoveBuildType( decl_node )
    633         );
     560Expression * build_virtual_cast( DeclarationNode * decl_node, ExpressionNode * expr_node ) {
     561        return new VirtualCastExpr( maybeMoveBuild< Expression >( expr_node ), maybeMoveBuildType( decl_node ) );
    634562} // build_virtual_cast
    635563
    636 ast::Expr * build_fieldSel( const CodeLocation & location,
    637                 ExpressionNode * expr_node,
    638                 ast::Expr * member ) {
    639         return new ast::UntypedMemberExpr( location,
    640                 member,
    641                 maybeMoveBuild( expr_node )
    642         );
     564Expression * build_fieldSel( ExpressionNode * expr_node, Expression * member ) {
     565        return new UntypedMemberExpr( member, maybeMoveBuild< Expression >(expr_node) );
    643566} // build_fieldSel
    644567
    645 ast::Expr * build_pfieldSel( const CodeLocation & location,
    646                 ExpressionNode * expr_node,
    647                 ast::Expr * member ) {
    648         auto deref = new ast::UntypedExpr( location,
    649                 new ast::NameExpr( location, "*?" )
    650         );
     568Expression * build_pfieldSel( ExpressionNode * expr_node, Expression * member ) {
     569        UntypedExpr * deref = new UntypedExpr( new NameExpr( "*?" ) );
    651570        deref->location = expr_node->location;
    652         deref->args.push_back( maybeMoveBuild( expr_node ) );
    653         auto ret = new ast::UntypedMemberExpr( location, member, deref );
     571        deref->get_args().push_back( maybeMoveBuild< Expression >(expr_node) );
     572        UntypedMemberExpr * ret = new UntypedMemberExpr( member, deref );
    654573        return ret;
    655574} // build_pfieldSel
    656575
    657 ast::Expr * build_offsetOf( const CodeLocation & location,
    658                 DeclarationNode * decl_node,
    659                 ast::NameExpr * member ) {
    660         ast::Expr * ret = new ast::UntypedOffsetofExpr( location,
    661                 maybeMoveBuildType( decl_node ),
    662                 member->name
    663         );
    664         ret->result = new ast::BasicType( ast::BasicType::LongUnsignedInt );
     576Expression * build_offsetOf( DeclarationNode * decl_node, NameExpr * member ) {
     577        Expression * ret = new UntypedOffsetofExpr( maybeMoveBuildType( decl_node ), member->get_name() );
    665578        delete member;
    666579        return ret;
    667580} // build_offsetOf
    668581
    669 ast::Expr * build_and_or( const CodeLocation & location,
    670                 ExpressionNode * expr_node1,
    671                 ExpressionNode * expr_node2,
    672                 ast::LogicalFlag flag ) {
    673         return new ast::LogicalExpr( location,
    674                 notZeroExpr( maybeMoveBuild( expr_node1 ) ),
    675                 notZeroExpr( maybeMoveBuild( expr_node2 ) ),
    676                 flag
    677         );
     582Expression * build_and_or( ExpressionNode * expr_node1, ExpressionNode * expr_node2, bool kind ) {
     583        return new LogicalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), notZeroExpr( maybeMoveBuild< Expression >(expr_node2) ), kind );
    678584} // build_and_or
    679585
    680 ast::Expr * build_unary_val( const CodeLocation & location,
    681                 OperKinds op,
    682                 ExpressionNode * expr_node ) {
    683         std::vector<ast::ptr<ast::Expr>> args;
    684         args.push_back( maybeMoveBuild( expr_node ) );
    685         return new ast::UntypedExpr( location,
    686                 new ast::NameExpr( location, OperName[ (int)op ] ),
    687                 std::move( args )
    688         );
     586Expression * build_unary_val( OperKinds op, ExpressionNode * expr_node ) {
     587        list< Expression * > args;
     588        args.push_back( maybeMoveBuild< Expression >(expr_node) );
     589        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
    689590} // build_unary_val
    690591
    691 ast::Expr * build_binary_val( const CodeLocation & location,
    692                 OperKinds op,
    693                 ExpressionNode * expr_node1,
    694                 ExpressionNode * expr_node2 ) {
    695         std::vector<ast::ptr<ast::Expr>> args;
    696         args.push_back( maybeMoveBuild( expr_node1 ) );
    697         args.push_back( maybeMoveBuild( expr_node2 ) );
    698         return new ast::UntypedExpr( location,
    699                 new ast::NameExpr( location, OperName[ (int)op ] ),
    700                 std::move( args )
    701         );
     592Expression * build_unary_ptr( OperKinds op, ExpressionNode * expr_node ) {
     593        list< Expression * > args;
     594        args.push_back(  maybeMoveBuild< Expression >(expr_node) ); // xxx -- this is exactly the same as the val case now, refactor this code.
     595        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
     596} // build_unary_ptr
     597
     598Expression * build_binary_val( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
     599        list< Expression * > args;
     600        args.push_back( maybeMoveBuild< Expression >(expr_node1) );
     601        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
     602        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
    702603} // build_binary_val
    703604
    704 ast::Expr * build_cond( const CodeLocation & location,
    705                 ExpressionNode * expr_node1,
    706                 ExpressionNode * expr_node2,
    707                 ExpressionNode * expr_node3 ) {
    708         return new ast::ConditionalExpr( location,
    709                 notZeroExpr( maybeMoveBuild( expr_node1 ) ),
    710                 maybeMoveBuild( expr_node2 ),
    711                 maybeMoveBuild( expr_node3 )
    712         );
     605Expression * build_binary_ptr( OperKinds op, ExpressionNode * expr_node1, ExpressionNode * expr_node2 ) {
     606        list< Expression * > args;
     607        args.push_back( maybeMoveBuild< Expression >(expr_node1) );
     608        args.push_back( maybeMoveBuild< Expression >(expr_node2) );
     609        return new UntypedExpr( new NameExpr( OperName[ (int)op ] ), args );
     610} // build_binary_ptr
     611
     612Expression * build_cond( ExpressionNode * expr_node1, ExpressionNode * expr_node2, ExpressionNode * expr_node3 ) {
     613        return new ConditionalExpr( notZeroExpr( maybeMoveBuild< Expression >(expr_node1) ), maybeMoveBuild< Expression >(expr_node2), maybeMoveBuild< Expression >(expr_node3) );
    713614} // build_cond
    714615
    715 ast::Expr * build_tuple( const CodeLocation & location,
    716                 ExpressionNode * expr_node ) {
    717         std::vector<ast::ptr<ast::Expr>> exprs;
     616Expression * build_tuple( ExpressionNode * expr_node ) {
     617        list< Expression * > exprs;
    718618        buildMoveList( expr_node, exprs );
    719         return new ast::UntypedTupleExpr( location, std::move( exprs ) );
     619        return new UntypedTupleExpr( exprs );;
    720620} // build_tuple
    721621
    722 ast::Expr * build_func( const CodeLocation & location,
    723                 ExpressionNode * function,
    724                 ExpressionNode * expr_node ) {
    725         std::vector<ast::ptr<ast::Expr>> args;
     622Expression * build_func( ExpressionNode * function, ExpressionNode * expr_node ) {
     623        list< Expression * > args;
    726624        buildMoveList( expr_node, args );
    727         return new ast::UntypedExpr( location,
    728                 maybeMoveBuild( function ),
    729                 std::move( args )
    730         );
     625        return new UntypedExpr( maybeMoveBuild< Expression >(function), args );
    731626} // build_func
    732627
    733 ast::Expr * build_compoundLiteral( const CodeLocation & location,
    734                 DeclarationNode * decl_node,
    735                 InitializerNode * kids ) {
    736         // compound literal type
    737         ast::Decl * newDecl = maybeBuild( decl_node );
    738         // non-sue compound-literal type
    739         if ( ast::DeclWithType * newDeclWithType = dynamic_cast<ast::DeclWithType *>( newDecl ) ) {
    740                 return new ast::CompoundLiteralExpr( location,
    741                         newDeclWithType->get_type(),
    742                         maybeMoveBuild( kids ) );
     628Expression * build_compoundLiteral( DeclarationNode * decl_node, InitializerNode * kids ) {
     629        Declaration * newDecl = maybeBuild< Declaration >(decl_node); // compound literal type
     630        if ( DeclarationWithType * newDeclWithType = dynamic_cast< DeclarationWithType * >( newDecl ) ) { // non-sue compound-literal type
     631                return new CompoundLiteralExpr( newDeclWithType->get_type(), maybeMoveBuild< Initializer >(kids) );
    743632        // these types do not have associated type information
    744         } else if ( auto newDeclStructDecl = dynamic_cast<ast::StructDecl *>( newDecl ) ) {
    745                 if ( newDeclStructDecl->body ) {
    746                         return new ast::CompoundLiteralExpr( location,
    747                                 new ast::StructInstType( newDeclStructDecl ),
    748                                 maybeMoveBuild( kids ) );
     633        } else if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( newDecl )  ) {
     634                if ( newDeclStructDecl->has_body() ) {
     635                        return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl ), maybeMoveBuild< Initializer >(kids) );
    749636                } else {
    750                         return new ast::CompoundLiteralExpr( location,
    751                                 new ast::StructInstType( newDeclStructDecl->name ),
    752                                 maybeMoveBuild( kids ) );
    753                 } // if
    754         } else if ( auto newDeclUnionDecl = dynamic_cast<ast::UnionDecl *>( newDecl )  ) {
    755                 if ( newDeclUnionDecl->body ) {
    756                         return new ast::CompoundLiteralExpr( location,
    757                                 new ast::UnionInstType( newDeclUnionDecl ),
    758                                 maybeMoveBuild( kids ) );
     637                        return new CompoundLiteralExpr( new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
     638                } // if
     639        } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( newDecl )  ) {
     640                if ( newDeclUnionDecl->has_body() ) {
     641                        return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl ), maybeMoveBuild< Initializer >(kids) );
    759642                } else {
    760                         return new ast::CompoundLiteralExpr( location,
    761                                 new ast::UnionInstType( newDeclUnionDecl->name ),
    762                                 maybeMoveBuild( kids ) );
    763                 } // if
    764         } else if ( auto newDeclEnumDecl = dynamic_cast<ast::EnumDecl *>( newDecl )  ) {
    765                 if ( newDeclEnumDecl->body ) {
    766                         return new ast::CompoundLiteralExpr( location,
    767                                 new ast::EnumInstType( newDeclEnumDecl ),
    768                                 maybeMoveBuild( kids ) );
     643                        return new CompoundLiteralExpr( new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
     644                } // if
     645        } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( newDecl )  ) {
     646                if ( newDeclEnumDecl->has_body() ) {
     647                        return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl ), maybeMoveBuild< Initializer >(kids) );
    769648                } else {
    770                         return new ast::CompoundLiteralExpr( location,
    771                                 new ast::EnumInstType( newDeclEnumDecl->name ),
    772                                 maybeMoveBuild( kids ) );
     649                        return new CompoundLiteralExpr( new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ), maybeMoveBuild< Initializer >(kids) );
    773650                } // if
    774651        } else {
     
    779656// Local Variables: //
    780657// tab-width: 4 //
     658// mode: c++ //
     659// compile-command: "make install" //
    781660// End: //
Note: See TracChangeset for help on using the changeset viewer.