Changeset 41c3e46


Ignore:
Timestamp:
Mar 31, 2025, 1:20:45 PM (3 weeks ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
fc8ec54
Parents:
52931c5
Message:

fix bug for incorrect kind (uLU) of character constants

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/Parser/ExpressionNode.cpp

    r52931c5 r41c3e46  
    1010// Created On       : Sat May 16 13:17:07 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Dec 16 20:50:27 2024
    13 // Update Count     : 1110
     12// Last Modified On : Mon Mar 31 12:08:13 2025
     13// Update Count     : 1121
    1414//
    1515
     
    443443} // sepString
    444444
    445 ast::Expr * build_constantChar( const CodeLocation & location, string & str ) {
    446         string units;                                                                           // units
    447         sepString( str, units, '\'' );                                          // separate constant from units
    448 
    449         ast::Expr * ret = new ast::ConstantExpr( location,
    450                 new ast::BasicType( ast::BasicKind::Char ),
    451                 str,
    452                 (unsigned long long int)(unsigned char)str[1] );
    453         if ( units.length() != 0 ) {
    454                 ret = new ast::UntypedExpr( location,
    455                         new ast::NameExpr( location, units ),
    456                         { ret } );
    457         } // if
    458 
    459         delete &str;                                                                            // created by lex
    460         return ret;
    461 } // build_constantChar
     445static ast::Type * charstrKind( string & str ) {
     446        ast::Type * kind;
     447        switch ( str[0] ) {                                                                     // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1
     448        case 'u':
     449                if ( str[1] == '8' ) goto Default;                              // utf-8 characters => array of char (save check for char as no 8 allowed)
     450                // lookup type of associated typedef
     451                kind = new ast::TypeInstType( "char16_t", ast::TypeDecl::Dtype );
     452                break;
     453        case 'U':
     454                kind = new ast::TypeInstType( "char32_t", ast::TypeDecl::Dtype );
     455                break;
     456        case 'L':
     457                kind = new ast::TypeInstType( "wchar_t", ast::TypeDecl::Dtype );
     458                break;
     459        Default:                                                                                        // char default string type
     460        default:
     461                kind = new ast::BasicType( ast::BasicKind::Char );
     462        } // switch
     463        return kind;
     464} // charstrKind
    462465
    463466static bool isoctal( char ch ) {
    464467        return ('0' <= ch && ch <= '7');
    465 }
     468} // isoctal
    466469
    467470// A "sequence" is the series of characters in a character/string literal that becomes a single
     
    498501                return 1;
    499502        } // switch
    500 }
     503} // sequenceLength
     504
     505ast::Expr * build_constantChar( const CodeLocation & location, string & str ) {
     506        string units;                                                                           // units
     507        sepString( str, units, '\'' );                                          // separate constant from units
     508
     509        ast::Expr * ret = new ast::ConstantExpr( location, charstrKind( str ), str, (unsigned long long int)(unsigned char)str[1] );
     510        if ( units.length() != 0 ) {
     511                ret = new ast::UntypedExpr( location, new ast::NameExpr( location, units ),     { ret } );
     512        } // if
     513
     514        delete &str;                                                                            // created by lex
     515        return ret;
     516} // build_constantChar
    501517
    502518ast::Expr * build_constantStr( const CodeLocation & location, string & str ) {
     
    504520        string units;                                                                           // units
    505521        sepString( str, units, '"' );                                           // separate constant from units
    506 
    507         ast::Type * strtype;
    508         switch ( str[0] ) {                                                                     // str has >= 2 characters, i.e, null string "" => safe to look at subscripts 0/1
    509         case 'u':
    510                 if ( str[1] == '8' ) goto Default;                              // utf-8 characters => array of char
    511                 // lookup type of associated typedef
    512                 strtype = new ast::TypeInstType( "char16_t", ast::TypeDecl::Dtype );
    513                 break;
    514         case 'U':
    515                 strtype = new ast::TypeInstType( "char32_t", ast::TypeDecl::Dtype );
    516                 break;
    517         case 'L':
    518                 strtype = new ast::TypeInstType( "wchar_t", ast::TypeDecl::Dtype );
    519                 break;
    520         Default:                                                                                        // char default string type
    521         default:
    522                 strtype = new ast::BasicType( ast::BasicKind::Char );
    523         } // switch
    524522
    525523        // The length value of the type is equal to the number of "sequences" not including the openning
     
    536534        } // for
    537535
    538         ast::ArrayType * at = new ast::ArrayType(
    539                 strtype,
    540                 ast::ConstantExpr::from_ulong( location, length ),
    541                 ast::FixedLen,
    542                 ast::DynamicDim );
     536        ast::ArrayType * at = new ast::ArrayType( charstrKind( str ), ast::ConstantExpr::from_ulong( location, length ), ast::FixedLen, ast::DynamicDim );
    543537        ast::Expr * ret = new ast::ConstantExpr( location, at, str, std::nullopt );
    544538        if ( units.length() != 0 ) {
Note: See TracChangeset for help on using the changeset viewer.