[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 | //
|
---|
| 7 | // ParseNode.cc --
|
---|
| 8 | //
|
---|
| 9 | // Author : Rodolfo G. Esteves
|
---|
| 10 | // Created On : Sat May 16 13:26:29 2015
|
---|
[ca35c51] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[4e05d27] | 12 | // Last Modified On : Sun Jul 24 02:17:01 2016
|
---|
| 13 | // Update Count : 90
|
---|
[b87a5ed] | 14 | //
|
---|
| 15 |
|
---|
[ca35c51] | 16 | #include <climits>
|
---|
[51b73452] | 17 | #include "ParseNode.h"
|
---|
| 18 | using namespace std;
|
---|
| 19 |
|
---|
[ca35c51] | 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 suffix
|
---|
| 23 | //
|
---|
| 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 suffix
|
---|
| 28 | //
|
---|
| 29 | // because the CONT rule is NOT triggered if the pattern is empty. Hence, constants are reparsed here to determine their
|
---|
| 30 | // type.
|
---|
| 31 |
|
---|
[4e05d27] | 32 | static Type::Qualifiers emptyQualifiers; // no qualifiers on constants
|
---|
| 33 |
|
---|
[ca35c51] | 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 |
|
---|
[4e05d27] | 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 constant
|
---|
| 47 | int size; // 0 => int, 1 => long, 2 => long long
|
---|
| 48 | unsigned long long v; // converted integral value
|
---|
| 49 | size_t last = str.length() - 1; // last character of constant
|
---|
| 50 |
|
---|
| 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 constant
|
---|
| 57 | sscanf( (char *)str.c_str(), "%llo", &v );
|
---|
| 58 | //printf( "%llo %llu\n", v, v );
|
---|
| 59 | } // if
|
---|
| 60 | } else { // decimal constant ?
|
---|
| 61 | sscanf( (char *)str.c_str(), "%llu", &v );
|
---|
| 62 | //printf( "%llu %llu\n", v, v );
|
---|
| 63 | } // if
|
---|
| 64 |
|
---|
| 65 | if ( v <= INT_MAX ) { // signed int
|
---|
| 66 | size = 0;
|
---|
| 67 | } else if ( v <= UINT_MAX && ! dec ) { // unsigned int
|
---|
| 68 | size = 0;
|
---|
| 69 | Unsigned = true; // unsigned
|
---|
| 70 | } else if ( v <= LONG_MAX ) { // signed long int
|
---|
| 71 | size = 1;
|
---|
| 72 | } else if ( v <= ULONG_MAX && ( ! dec || LONG_MAX == LLONG_MAX ) ) { // signed long int
|
---|
| 73 | size = 1;
|
---|
| 74 | Unsigned = true; // unsigned long int
|
---|
| 75 | } else if ( v <= LLONG_MAX ) { // signed long long int
|
---|
| 76 | size = 2;
|
---|
| 77 | } else { // unsigned long long int
|
---|
| 78 | size = 2;
|
---|
| 79 | Unsigned = true; // unsigned long long int
|
---|
| 80 | } // if
|
---|
| 81 |
|
---|
| 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' ?
|
---|
[ca35c51] | 87 | size = 2;
|
---|
| 88 | } // if
|
---|
[4e05d27] | 89 | } // if
|
---|
| 90 | } 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' ?
|
---|
[ca35c51] | 95 | Unsigned = true;
|
---|
| 96 | } // if
|
---|
[4e05d27] | 97 | } else {
|
---|
| 98 | if ( last > 0 && checkU( str[last - 1] ) ) { // suffix 'u' ?
|
---|
| 99 | Unsigned = true;
|
---|
[ca35c51] | 100 | } // if
|
---|
| 101 | } // if
|
---|
[4e05d27] | 102 | } // if
|
---|
| 103 |
|
---|
| 104 | return new ConstantNode( new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[Unsigned][size] ), str ), nullptr ) );
|
---|
| 105 | } // makeConstantInteger
|
---|
| 106 |
|
---|
| 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, complex
|
---|
| 114 | int size = 1; // 0 => float, 1 => double (default), 2 => long double
|
---|
| 115 | // floating-point constant has minimum of 2 characters: 1. or .1
|
---|
| 116 | size_t last = str.length() - 1;
|
---|
| 117 |
|
---|
| 118 | if ( checkI( str[last] ) ) { // imaginary ?
|
---|
| 119 | complx = true;
|
---|
| 120 | last -= 1; // backup one character
|
---|
| 121 | } // if
|
---|
| 122 |
|
---|
| 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 | } // if
|
---|
| 130 | if ( ! complx && checkI( str[last - 1] ) ) { // imaginary ?
|
---|
| 131 | complx = true;
|
---|
| 132 | } // if
|
---|
| 133 |
|
---|
| 134 | return new ConstantNode( new ConstantExpr( Constant( new BasicType( emptyQualifiers, kind[complx][size] ), str ), nullptr ) );
|
---|
| 135 | } // makeConstantFloat
|
---|
| 136 |
|
---|
| 137 | ConstantNode *makeConstantChar( std::string & str ) {
|
---|
| 138 | return new ConstantNode( new ConstantExpr( Constant( new BasicType( emptyQualifiers, BasicType::Char ), str ), nullptr ) );
|
---|
| 139 | } // makeConstantChar
|
---|
| 140 |
|
---|
| 141 | ConstantNode *makeConstantStr( std::string & str ) {
|
---|
[ca35c51] | 142 | // string should probably be a primitive type
|
---|
| 143 | ArrayType *at = new ArrayType( emptyQualifiers, new BasicType( emptyQualifiers, BasicType::Char ),
|
---|
| 144 | new ConstantExpr(
|
---|
| 145 | Constant( new BasicType( emptyQualifiers, BasicType::UnsignedInt ),
|
---|
[4e05d27] | 146 | toString( str.size()+1-2 ) ) ), // +1 for '\0' and -2 for '"'
|
---|
[ca35c51] | 147 | false, false );
|
---|
[4e05d27] | 148 | return new ConstantNode( new ConstantExpr( Constant( at, str ), nullptr ) );
|
---|
| 149 | } // makeConstantStr
|
---|
[ca35c51] | 150 |
|
---|
| 151 |
|
---|
[51b73452] | 152 | // Builder
|
---|
| 153 | int ParseNode::indent_by = 4;
|
---|
| 154 |
|
---|
[e869d663] | 155 | ParseNode::ParseNode() : next( 0 ) {};
|
---|
| 156 | ParseNode::ParseNode( const string *name ) : name( *name ), next( 0 ) { delete name; }
|
---|
| 157 | ParseNode::ParseNode( const string &name ) : name( name ), next( 0 ) { }
|
---|
[51b73452] | 158 |
|
---|
[59db689] | 159 | ParseNode::~ParseNode() {
|
---|
[e869d663] | 160 | delete next;
|
---|
[51b73452] | 161 | };
|
---|
| 162 |
|
---|
[59db689] | 163 | ParseNode *ParseNode::get_last() {
|
---|
[a08ba92] | 164 | ParseNode *current = this;
|
---|
[51b73452] | 165 |
|
---|
[a08ba92] | 166 | while ( current->get_link() != 0 )
|
---|
[3848e0e] | 167 | current = current->get_link();
|
---|
[51b73452] | 168 |
|
---|
[a08ba92] | 169 | return current;
|
---|
[51b73452] | 170 | }
|
---|
| 171 |
|
---|
[59db689] | 172 | ParseNode *ParseNode::set_link( ParseNode *next_ ) {
|
---|
[4e06c1e] | 173 | if ( next_ != 0 ) get_last()->next = next_;
|
---|
[a08ba92] | 174 | return this;
|
---|
[51b73452] | 175 | }
|
---|
| 176 |
|
---|
[a61fea9a] | 177 | void ParseNode::print( std::ostream &os, int indent ) const {}
|
---|
[51b73452] | 178 |
|
---|
| 179 |
|
---|
[3848e0e] | 180 | void ParseNode::printList( std::ostream &os, int indent ) const {
|
---|
[a08ba92] | 181 | print( os, indent );
|
---|
[51b73452] | 182 |
|
---|
[a08ba92] | 183 | if ( next ) {
|
---|
[a61fea9a] | 184 | next->printList( os, indent );
|
---|
[59db689] | 185 | } // if
|
---|
[51b73452] | 186 | }
|
---|
| 187 |
|
---|
[3848e0e] | 188 | ParseNode &ParseNode::operator,( ParseNode &p ) {
|
---|
[a08ba92] | 189 | set_link( &p );
|
---|
[51b73452] | 190 |
|
---|
[a08ba92] | 191 | return *this;
|
---|
[51b73452] | 192 | }
|
---|
| 193 |
|
---|
[bdd516a] | 194 | ParseNode *mkList( ParseNode &pn ) {
|
---|
[a08ba92] | 195 | // it just relies on `operator,' to take care of the "arguments" and provides a nice interface to an awful-looking
|
---|
| 196 | // address-of, rendering, for example (StatementNode *)(&(*$5 + *$7)) into (StatementNode *)mkList(($5, $7))
|
---|
| 197 | // (although "nice" is probably not the word)
|
---|
| 198 | return &pn;
|
---|
[51b73452] | 199 | }
|
---|
| 200 |
|
---|
| 201 | // Local Variables: //
|
---|
[b87a5ed] | 202 | // tab-width: 4 //
|
---|
| 203 | // mode: c++ //
|
---|
| 204 | // compile-command: "make install" //
|
---|
[51b73452] | 205 | // End: //
|
---|