Changeset 7bf7fb9 for src/Parser/ParseNode.cc
- Timestamp:
- Aug 7, 2016, 9:47:37 AM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 04273e9, d1625f8
- Parents:
- 35f9114
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/ParseNode.cc
r35f9114 r7bf7fb9 10 10 // Created On : Sat May 16 13:26:29 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : S un Jul 24 02:17:01 201613 // Update Count : 9 012 // Last Modified On : Sat Aug 6 08:26:11 2016 13 // Update Count : 93 14 14 // 15 15 16 #include <climits>17 16 #include "ParseNode.h" 18 17 using namespace std; 19 20 // Difficult to separate extra parts of constants during lexing because actions are not allow in the middle of patterns:21 //22 // prefix action constant action suffix23 //24 // Alternatively, breaking a pattern using BEGIN does not work if the following pattern can be empty:25 //26 // constant BEGIN CONT ...27 // <CONT>(...)? BEGIN 0 ... // possible empty suffix28 //29 // because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their30 // type.31 32 static Type::Qualifiers emptyQualifiers; // no qualifiers on constants33 34 static inline bool checkU( char c ) { return c == 'u' || c == 'U'; }35 static inline bool checkL( char c ) { return c == 'l' || c == 'L'; }36 static inline bool checkF( char c ) { return c == 'f' || c == 'F'; }37 static inline bool checkD( char c ) { return c == 'd' || c == 'D'; }38 static inline bool checkI( char c ) { return c == 'i' || c == 'I'; }39 static inline bool checkX( char c ) { return c == 'x' || c == 'X'; }40 41 ConstantNode *makeConstantInteger( std::string & str ) {42 static const BasicType::Kind kind[2][3] = {43 { BasicType::SignedInt, BasicType::LongSignedInt, BasicType::LongLongSignedInt },44 { BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt },45 };46 bool dec = true, Unsigned = false; // decimal, unsigned constant47 int size; // 0 => int, 1 => long, 2 => long long48 unsigned long long v; // converted integral value49 size_t last = str.length() - 1; // last character of constant50 51 if ( str[0] == '0' ) { // octal/hex constant ?52 dec = false;53 if ( last != 0 && checkX( str[1] ) ) { // hex constant ?54 sscanf( (char *)str.c_str(), "%llx", &v );55 //printf( "%llx %llu\n", v, v );56 } else { // octal constant57 sscanf( (char *)str.c_str(), "%llo", &v );58 //printf( "%llo %llu\n", v, v );59 } // if60 } else { // decimal constant ?61 sscanf( (char *)str.c_str(), "%llu", &v );62 //printf( "%llu %llu\n", v, v );63 } // if64 65 if ( v <= INT_MAX ) { // signed int66 size = 0;67 } else if ( v <= UINT_MAX && ! dec ) { // unsigned int68 size = 0;69 Unsigned = true; // unsigned70 } else if ( v <= LONG_MAX ) { // signed long int71 size = 1;72 } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int73 size = 1;74 Unsigned = true; // unsigned long int75 } else if ( v <= LLONG_MAX ) { // signed long long int76 size = 2;77 } else { // unsigned long long int78 size = 2;79 Unsigned = true; // unsigned long long int80 } // if81 82 if ( checkU( str[last] ) ) { // suffix 'u' ?83 Unsigned = true;84 if ( last > 0 && checkL( str[last - 1] ) ) { // suffix 'l' ?85 size = 1;86 if ( last > 1 && checkL( str[last - 2] ) ) { // suffix 'll' ?87 size = 2;88 } // if89 } // if90 } else if ( checkL( str[ last ] ) ) { // suffix 'l' ?91 size = 1;92 if ( last > 0 && checkL( str[last - 1] ) ) { // suffix 'll' ?93 size = 2;94 if ( last > 1 && checkU( str[last - 2] ) ) { // suffix 'u' ?95 Unsigned = true;96 } // if97 } else {98 if ( last > 0 && checkU( str[last - 1] ) ) { // suffix 'u' ?99 Unsigned = true;100 } // if101 } // if102 } // if103 104 return new ConstantNode( new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[Unsigned][size] ), str ), nullptr ) );105 } // makeConstantInteger106 107 ConstantNode *makeConstantFloat( std::string & str ) {108 static const BasicType::Kind kind[2][3] = {109 { BasicType::Float, BasicType::Double, BasicType::LongDouble },110 { BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex },111 };112 113 bool complx = false; // real, complex114 int size = 1; // 0 => float, 1 => double (default), 2 => long double115 // floating-point constant has minimum of 2 characters: 1. or .1116 size_t last = str.length() - 1;117 118 if ( checkI( str[last] ) ) { // imaginary ?119 complx = true;120 last -= 1; // backup one character121 } // if122 123 if ( checkF( str[last] ) ) { // float ?124 size = 0;125 } else if ( checkD( str[last] ) ) { // double ?126 size = 1;127 } else if ( checkL( str[last] ) ) { // long double ?128 size = 2;129 } // if130 if ( ! complx && checkI( str[last - 1] ) ) { // imaginary ?131 complx = true;132 } // if133 134 return new ConstantNode( new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[complx][size] ), str ), nullptr ) );135 } // makeConstantFloat136 137 ConstantNode *makeConstantChar( std::string & str ) {138 return new ConstantNode( new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::Char ), str ), nullptr ) );139 } // makeConstantChar140 141 ConstantNode *makeConstantStr( std::string & str ) {142 // string should probably be a primitive type143 ArrayType *at = new ArrayType( emptyQualifiers, new BasicType( emptyQualifiers, BasicType::Char ),144 new ConstantExpr(145 Constant( new BasicType( emptyQualifiers, BasicType::UnsignedInt ),146 toString( str.size()+1-2 ) ) ), // +1 for '\0' and -2 for '"'147 false, false );148 return new ConstantNode( new ConstantExpr( Constant( at, str ), nullptr ) );149 } // makeConstantStr150 151 18 152 19 // Builder
Note: See TracChangeset
for help on using the changeset viewer.