| [51b73452] | 1 | #include <cassert>
|
|---|
| 2 | #include <cctype>
|
|---|
| 3 | #include <algorithm>
|
|---|
| 4 |
|
|---|
| 5 | #include "ParseNode.h"
|
|---|
| 6 | #include "SynTree/Type.h"
|
|---|
| 7 | #include "SynTree/Constant.h"
|
|---|
| 8 | #include "SynTree/Expression.h"
|
|---|
| 9 | #include "SynTree/Declaration.h"
|
|---|
| 10 | #include "UnimplementedError.h"
|
|---|
| 11 | #include "parseutility.h"
|
|---|
| 12 | #include "utility.h"
|
|---|
| 13 |
|
|---|
| 14 | using namespace std;
|
|---|
| 15 |
|
|---|
| [3848e0e] | 16 | ExpressionNode::ExpressionNode() : ParseNode(), argName( 0 ) {}
|
|---|
| [51b73452] | 17 |
|
|---|
| [bdd516a] | 18 | ExpressionNode::ExpressionNode( string *name_) : ParseNode( *name_ ), argName( 0 ) {
|
|---|
| [3848e0e] | 19 | delete name_;
|
|---|
| [51b73452] | 20 | }
|
|---|
| 21 |
|
|---|
| [3848e0e] | 22 | ExpressionNode::ExpressionNode( const ExpressionNode &other ) : ParseNode( other.name ) {
|
|---|
| 23 | if ( other.argName ) {
|
|---|
| 24 | argName = other.argName->clone();
|
|---|
| 25 | } else {
|
|---|
| 26 | argName = 0;
|
|---|
| [bdd516a] | 27 | } // if
|
|---|
| [51b73452] | 28 | }
|
|---|
| 29 |
|
|---|
| 30 | ExpressionNode * ExpressionNode::set_asArgName( std::string *aName ) {
|
|---|
| [bdd516a] | 31 | argName = new VarRefNode( aName );
|
|---|
| [3848e0e] | 32 | return this;
|
|---|
| [51b73452] | 33 | }
|
|---|
| 34 |
|
|---|
| 35 | ExpressionNode * ExpressionNode::set_asArgName( ExpressionNode *aDesignator ) {
|
|---|
| [3848e0e] | 36 | argName = aDesignator;
|
|---|
| 37 | return this;
|
|---|
| [51b73452] | 38 | }
|
|---|
| 39 |
|
|---|
| 40 | void ExpressionNode::printDesignation( std::ostream &os, int indent ) const {
|
|---|
| [3848e0e] | 41 | if ( argName ) {
|
|---|
| [bdd516a] | 42 | os << string(' ', indent ) << "(designated by: ";
|
|---|
| 43 | argName->printOneLine( os, indent );
|
|---|
| [3848e0e] | 44 | os << ")" << std::endl;
|
|---|
| [bdd516a] | 45 | } // if
|
|---|
| [51b73452] | 46 | }
|
|---|
| 47 |
|
|---|
| [3848e0e] | 48 | NullExprNode::NullExprNode() {}
|
|---|
| [51b73452] | 49 |
|
|---|
| [3848e0e] | 50 | NullExprNode *NullExprNode::clone() const {
|
|---|
| 51 | return new NullExprNode();
|
|---|
| [51b73452] | 52 | }
|
|---|
| 53 |
|
|---|
| [bdd516a] | 54 | void NullExprNode::print( std::ostream & os, int indent ) const {
|
|---|
| 55 | printDesignation( os );
|
|---|
| [3848e0e] | 56 | os << "null expression";
|
|---|
| [51b73452] | 57 | }
|
|---|
| 58 |
|
|---|
| [bdd516a] | 59 | void NullExprNode::printOneLine( std::ostream & os, int indent ) const {
|
|---|
| 60 | printDesignation( os );
|
|---|
| [3848e0e] | 61 | os << "null";
|
|---|
| [51b73452] | 62 | }
|
|---|
| 63 |
|
|---|
| [3848e0e] | 64 | Expression *NullExprNode::build() const {
|
|---|
| 65 | return 0;
|
|---|
| [51b73452] | 66 | }
|
|---|
| 67 |
|
|---|
| [bdd516a] | 68 | CommaExprNode *ExpressionNode::add_to_list( ExpressionNode *exp ){
|
|---|
| 69 | return new CommaExprNode( this, exp );
|
|---|
| [51b73452] | 70 | }
|
|---|
| 71 |
|
|---|
| 72 | // enum ConstantNode::Type = { Integer, Float, Character, String, Range }
|
|---|
| 73 |
|
|---|
| [bdd516a] | 74 | ConstantNode::ConstantNode( void ) : ExpressionNode(), sign( true ), longs(0), size(0) {}
|
|---|
| [51b73452] | 75 |
|
|---|
| [bdd516a] | 76 | ConstantNode::ConstantNode( string *name_) : ExpressionNode( name_), sign( true ), longs(0), size(0) {}
|
|---|
| [51b73452] | 77 |
|
|---|
| [bdd516a] | 78 | ConstantNode::ConstantNode( Type t, string *inVal ) : type( t ), sign( true ), longs(0), size(0) {
|
|---|
| [3848e0e] | 79 | if ( inVal ) {
|
|---|
| 80 | value = *inVal;
|
|---|
| 81 | delete inVal;
|
|---|
| 82 | } else {
|
|---|
| 83 | value = "";
|
|---|
| [bdd516a] | 84 | } // if
|
|---|
| [51b73452] | 85 |
|
|---|
| [bdd516a] | 86 | classify( value );
|
|---|
| [51b73452] | 87 | }
|
|---|
| 88 |
|
|---|
| 89 | ConstantNode::ConstantNode( const ConstantNode &other )
|
|---|
| [3848e0e] | 90 | : ExpressionNode( other ), type( other.type ), value( other.value ), sign( other.sign ), base( other.base ), longs( other.longs ), size( other.size ) {
|
|---|
| [51b73452] | 91 | }
|
|---|
| 92 |
|
|---|
| 93 | // for some reason, std::tolower doesn't work as an argument to std::transform in g++ 3.1
|
|---|
| [3848e0e] | 94 | inline char tolower_hack( char c ) {
|
|---|
| 95 | return std::tolower( c );
|
|---|
| [51b73452] | 96 | }
|
|---|
| 97 |
|
|---|
| [bdd516a] | 98 | void ConstantNode::classify( std::string &str ){
|
|---|
| 99 | switch ( type ){
|
|---|
| [3848e0e] | 100 | case Integer:
|
|---|
| 101 | case Float:
|
|---|
| 102 | {
|
|---|
| 103 | std::string sfx("");
|
|---|
| 104 | char c;
|
|---|
| 105 | int i = str.length() - 1;
|
|---|
| [51b73452] | 106 |
|
|---|
| [bdd516a] | 107 | while ( i >= 0 && !isxdigit( c = str.at( i--)) )
|
|---|
| [3848e0e] | 108 | sfx += c;
|
|---|
| [51b73452] | 109 |
|
|---|
| [3848e0e] | 110 | value = str.substr( 0, i + 2 );
|
|---|
| [51b73452] | 111 |
|
|---|
| [3848e0e] | 112 | // get rid of underscores
|
|---|
| [bdd516a] | 113 | value.erase( remove( value.begin(), value.end(), '_'), value.end());
|
|---|
| [51b73452] | 114 |
|
|---|
| [bdd516a] | 115 | std::transform( sfx.begin(), sfx.end(), sfx.begin(), tolower_hack );
|
|---|
| [51b73452] | 116 |
|
|---|
| [3848e0e] | 117 | if ( sfx.find("ll") != string::npos ){
|
|---|
| 118 | longs = 2;
|
|---|
| [bdd516a] | 119 | } else if ( sfx.find("l") != string::npos ){
|
|---|
| [3848e0e] | 120 | longs = 1;
|
|---|
| [bdd516a] | 121 | } // if
|
|---|
| [51b73452] | 122 |
|
|---|
| [bdd516a] | 123 | assert(( longs >= 0) && ( longs <= 2));
|
|---|
| [51b73452] | 124 |
|
|---|
| [3848e0e] | 125 | if ( sfx.find("u") != string::npos )
|
|---|
| 126 | sign = false;
|
|---|
| [51b73452] | 127 |
|
|---|
| [3848e0e] | 128 | break;
|
|---|
| 129 | }
|
|---|
| 130 | case Character:
|
|---|
| 131 | {
|
|---|
| 132 | // remove underscores from hex and oct escapes
|
|---|
| [bdd516a] | 133 | if ( str.substr(1,2) == "\\x")
|
|---|
| 134 | value.erase( remove( value.begin(), value.end(), '_'), value.end());
|
|---|
| [51b73452] | 135 |
|
|---|
| [3848e0e] | 136 | break;
|
|---|
| 137 | }
|
|---|
| 138 | default:
|
|---|
| 139 | // shouldn't be here
|
|---|
| 140 | ;
|
|---|
| 141 | }
|
|---|
| [51b73452] | 142 | }
|
|---|
| 143 |
|
|---|
| [bdd516a] | 144 | ConstantNode::Type ConstantNode::get_type( void ) const {
|
|---|
| [3848e0e] | 145 | return type;
|
|---|
| [51b73452] | 146 | }
|
|---|
| 147 |
|
|---|
| [3848e0e] | 148 | ConstantNode *ConstantNode::append( std::string *newValue ) {
|
|---|
| 149 | if ( newValue ) {
|
|---|
| [bdd516a] | 150 | if ( type == String ){
|
|---|
| [3848e0e] | 151 | std::string temp = *newValue;
|
|---|
| 152 | value.resize( value.size() - 1 );
|
|---|
| 153 | value += newValue->substr(1, newValue->size());
|
|---|
| 154 | } else
|
|---|
| 155 | value += *newValue;
|
|---|
| [51b73452] | 156 |
|
|---|
| [3848e0e] | 157 | delete newValue;
|
|---|
| [bdd516a] | 158 | } // if
|
|---|
| [3848e0e] | 159 | return this;
|
|---|
| [51b73452] | 160 | }
|
|---|
| 161 |
|
|---|
| [bdd516a] | 162 | void ConstantNode::printOneLine( std::ostream &os, int indent ) const {
|
|---|
| 163 | os << string( indent, ' ');
|
|---|
| 164 | printDesignation( os );
|
|---|
| [51b73452] | 165 |
|
|---|
| [3848e0e] | 166 | switch ( type ) {
|
|---|
| 167 | /* integers */
|
|---|
| 168 | case Integer:
|
|---|
| 169 | os << value ;
|
|---|
| 170 | break;
|
|---|
| 171 | case Float:
|
|---|
| 172 | os << value ;
|
|---|
| 173 | break;
|
|---|
| [51b73452] | 174 |
|
|---|
| [3848e0e] | 175 | case Character:
|
|---|
| 176 | os << "'" << value << "'";
|
|---|
| 177 | break;
|
|---|
| [51b73452] | 178 |
|
|---|
| [3848e0e] | 179 | case String:
|
|---|
| 180 | os << '"' << value << '"';
|
|---|
| 181 | break;
|
|---|
| 182 | }
|
|---|
| [51b73452] | 183 |
|
|---|
| [3848e0e] | 184 | os << ' ';
|
|---|
| [51b73452] | 185 | }
|
|---|
| 186 |
|
|---|
| [bdd516a] | 187 | void ConstantNode::print( std::ostream &os, int indent ) const {
|
|---|
| [3848e0e] | 188 | printOneLine( os, indent );
|
|---|
| 189 | os << endl;
|
|---|
| [51b73452] | 190 | }
|
|---|
| 191 |
|
|---|
| 192 | Expression *ConstantNode::build() const {
|
|---|
| [3848e0e] | 193 | ::Type::Qualifiers q;
|
|---|
| 194 | BasicType *bt;
|
|---|
| 195 |
|
|---|
| [bdd516a] | 196 | switch ( get_type()){
|
|---|
| [3848e0e] | 197 | case Integer:
|
|---|
| 198 | /* Cfr. standard 6.4.4.1 */
|
|---|
| [bdd516a] | 199 | //bt.set_kind( BasicType::SignedInt );
|
|---|
| 200 | bt = new BasicType( q, BasicType::SignedInt );
|
|---|
| [3848e0e] | 201 | break;
|
|---|
| 202 | case Float:
|
|---|
| [bdd516a] | 203 | bt = new BasicType( q, BasicType::Float );
|
|---|
| [3848e0e] | 204 | break;
|
|---|
| 205 | case Character:
|
|---|
| [bdd516a] | 206 | bt = new BasicType( q, BasicType::Char );
|
|---|
| [3848e0e] | 207 | break;
|
|---|
| 208 | case String:
|
|---|
| 209 | // string should probably be a primitive type
|
|---|
| 210 | ArrayType *at;
|
|---|
| 211 | std::string value = get_value();
|
|---|
| [bdd516a] | 212 | at = new ArrayType( q, new BasicType( q, BasicType::Char ),
|
|---|
| 213 | new ConstantExpr( Constant( new BasicType( q, BasicType::SignedInt ),
|
|---|
| [3848e0e] | 214 | toString( value.size() - 1 ) ) ), // account for '\0'
|
|---|
| 215 | false, false );
|
|---|
| [bdd516a] | 216 | return new ConstantExpr( Constant( at, value ), maybeBuild< Expression >( get_argName() ) );
|
|---|
| [3848e0e] | 217 | }
|
|---|
| [bdd516a] | 218 | return new ConstantExpr( Constant( bt, get_value()), maybeBuild< Expression >( get_argName() ) );
|
|---|
| [51b73452] | 219 | }
|
|---|
| 220 |
|
|---|
| [bdd516a] | 221 | VarRefNode::VarRefNode() : isLabel( false ) {}
|
|---|
| [51b73452] | 222 |
|
|---|
| [bdd516a] | 223 | VarRefNode::VarRefNode( string *name_, bool labelp ) : ExpressionNode( name_), isLabel( labelp ) {}
|
|---|
| [51b73452] | 224 |
|
|---|
| [3848e0e] | 225 | VarRefNode::VarRefNode( const VarRefNode &other ) : ExpressionNode( other ), isLabel( other.isLabel ) {
|
|---|
| [51b73452] | 226 | }
|
|---|
| 227 |
|
|---|
| 228 | Expression *VarRefNode::build() const {
|
|---|
| [3848e0e] | 229 | return new NameExpr( get_name(), maybeBuild< Expression >( get_argName() ) );
|
|---|
| [51b73452] | 230 | }
|
|---|
| 231 |
|
|---|
| [bdd516a] | 232 | void VarRefNode::printOneLine( std::ostream &os, int indent ) const {
|
|---|
| 233 | printDesignation( os );
|
|---|
| [3848e0e] | 234 | os << get_name() << ' ';
|
|---|
| [51b73452] | 235 | }
|
|---|
| 236 |
|
|---|
| [bdd516a] | 237 | void VarRefNode::print( std::ostream &os, int indent ) const {
|
|---|
| 238 | printDesignation( os );
|
|---|
| 239 | os << '\r' << string( indent, ' ') << "Referencing: ";
|
|---|
| [3848e0e] | 240 | os << "Variable: " << get_name();
|
|---|
| 241 | os << endl;
|
|---|
| [51b73452] | 242 | }
|
|---|
| 243 |
|
|---|
| [bdd516a] | 244 | OperatorNode::OperatorNode( Type t ) : type( t ) {}
|
|---|
| [51b73452] | 245 |
|
|---|
| [3848e0e] | 246 | OperatorNode::OperatorNode( const OperatorNode &other ) : ExpressionNode( other ), type( other.type ) {
|
|---|
| [51b73452] | 247 | }
|
|---|
| 248 |
|
|---|
| 249 | OperatorNode::~OperatorNode() {}
|
|---|
| 250 |
|
|---|
| [bdd516a] | 251 | OperatorNode::Type OperatorNode::get_type( void ) const{
|
|---|
| [3848e0e] | 252 | return type;
|
|---|
| [51b73452] | 253 | }
|
|---|
| 254 |
|
|---|
| [3848e0e] | 255 | void OperatorNode::printOneLine( std::ostream &os, int indent ) const {
|
|---|
| [bdd516a] | 256 | printDesignation( os );
|
|---|
| [3848e0e] | 257 | os << OpName[ type ] << ' ';
|
|---|
| [51b73452] | 258 | }
|
|---|
| 259 |
|
|---|
| 260 | void OperatorNode::print( std::ostream &os, int indent ) const{
|
|---|
| [bdd516a] | 261 | printDesignation( os );
|
|---|
| 262 | os << '\r' << string( indent, ' ') << "Operator: " << OpName[type] << endl;
|
|---|
| [3848e0e] | 263 | return;
|
|---|
| [51b73452] | 264 | }
|
|---|
| 265 |
|
|---|
| [bdd516a] | 266 | std::string OperatorNode::get_typename( void ) const{
|
|---|
| 267 | return string( OpName[ type ]);
|
|---|
| [51b73452] | 268 | }
|
|---|
| 269 |
|
|---|
| [3848e0e] | 270 | const char *OperatorNode::OpName[] = {
|
|---|
| 271 | "TupleC", "Comma", "TupleFieldSel",// "TuplePFieldSel", //n-adic
|
|---|
| [51b73452] | 272 | // triadic
|
|---|
| 273 | "Cond", "NCond",
|
|---|
| 274 | // diadic
|
|---|
| 275 | "SizeOf", "AlignOf", "Attr", "CompLit", "Plus", "Minus", "Mul", "Div", "Mod", "Or",
|
|---|
| [3848e0e] | 276 | "And", "BitOr", "BitAnd", "Xor", "Cast", "LShift", "RShift", "LThan", "GThan",
|
|---|
| 277 | "LEThan", "GEThan", "Eq", "Neq", "Assign", "MulAssn", "DivAssn", "ModAssn", "PlusAssn",
|
|---|
| 278 | "MinusAssn", "LSAssn", "RSAssn", "AndAssn", "ERAssn", "OrAssn", "Index", "FieldSel","PFieldSel",
|
|---|
| 279 | "Range",
|
|---|
| [51b73452] | 280 | // monadic
|
|---|
| 281 | "UnPlus", "UnMinus", "AddressOf", "PointTo", "Neg", "BitNeg", "Incr", "IncrPost", "Decr", "DecrPost", "LabelAddress"
|
|---|
| [3848e0e] | 282 | };
|
|---|
| [51b73452] | 283 |
|
|---|
| [bdd516a] | 284 | CompositeExprNode::CompositeExprNode( void ) : ExpressionNode(), function( 0 ), arguments( 0 ) {
|
|---|
| [51b73452] | 285 | }
|
|---|
| 286 |
|
|---|
| [bdd516a] | 287 | CompositeExprNode::CompositeExprNode( string *name_) : ExpressionNode( name_), function( 0 ), arguments( 0 ) {
|
|---|
| [51b73452] | 288 | }
|
|---|
| 289 |
|
|---|
| [bdd516a] | 290 | CompositeExprNode::CompositeExprNode( ExpressionNode *f, ExpressionNode *args ):
|
|---|
| 291 | function( f ), arguments( args ) {
|
|---|
| [51b73452] | 292 | }
|
|---|
| 293 |
|
|---|
| [bdd516a] | 294 | CompositeExprNode::CompositeExprNode( ExpressionNode *f, ExpressionNode *arg1, ExpressionNode *arg2):
|
|---|
| 295 | function( f ), arguments( arg1) {
|
|---|
| 296 | arguments->set_link( arg2);
|
|---|
| [51b73452] | 297 | }
|
|---|
| 298 |
|
|---|
| [3848e0e] | 299 | CompositeExprNode::CompositeExprNode( const CompositeExprNode &other ) : ExpressionNode( other ), function( maybeClone( other.function ) ) {
|
|---|
| 300 | ParseNode *cur = other.arguments;
|
|---|
| 301 | while ( cur ) {
|
|---|
| 302 | if ( arguments ) {
|
|---|
| 303 | arguments->set_link( cur->clone() );
|
|---|
| 304 | } else {
|
|---|
| [bdd516a] | 305 | arguments = ( ExpressionNode*)cur->clone();
|
|---|
| 306 | } // if
|
|---|
| [3848e0e] | 307 | cur = cur->get_link();
|
|---|
| [51b73452] | 308 | }
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| [3848e0e] | 311 | CompositeExprNode::~CompositeExprNode() {
|
|---|
| 312 | delete function;
|
|---|
| 313 | delete arguments;
|
|---|
| [51b73452] | 314 | }
|
|---|
| 315 |
|
|---|
| 316 | // the names that users use to define operator functions
|
|---|
| [3848e0e] | 317 | static const char *opFuncName[] = {
|
|---|
| 318 | "", "", "",
|
|---|
| [51b73452] | 319 | "", "",
|
|---|
| 320 | // diadic
|
|---|
| 321 | "", "", "", "", "?+?", "?-?", "?*?", "?/?", "?%?", "", "",
|
|---|
| [3848e0e] | 322 | "?|?", "?&?", "?^?", "", "?<<?", "?>>?", "?<?", "?>?", "?<=?",
|
|---|
| 323 | "?>=?", "?==?", "?!=?", "?=?", "?*=?", "?/=?", "?%=?", "?+=?", "?-=?",
|
|---|
| 324 | "?<<=?", "?>>=?", "?&=?", "?^=?", "?|=?", "?[?]", "","","Range",
|
|---|
| [51b73452] | 325 | // monadic
|
|---|
| 326 | "+?", "-?", "", "*?", "!?", "~?", "++?", "?++", "--?", "?--", "LabAddress"
|
|---|
| [3848e0e] | 327 | };
|
|---|
| [51b73452] | 328 |
|
|---|
| 329 | #include "utility.h"
|
|---|
| 330 |
|
|---|
| [3848e0e] | 331 | Expression *CompositeExprNode::build() const {
|
|---|
| 332 | OperatorNode *op;
|
|---|
| 333 | std::list<Expression *> args;
|
|---|
| [51b73452] | 334 |
|
|---|
| [bdd516a] | 335 | buildList( get_args(), args );
|
|---|
| [51b73452] | 336 |
|
|---|
| [bdd516a] | 337 | if ( ! ( op = dynamic_cast<OperatorNode *>( function )) ) {
|
|---|
| [3848e0e] | 338 | // a function as opposed to an operator
|
|---|
| [bdd516a] | 339 | return new UntypedExpr( function->build(), args, maybeBuild< Expression >( get_argName() ));
|
|---|
| [3848e0e] | 340 | } else {
|
|---|
| [bdd516a] | 341 | switch ( op->get_type()){
|
|---|
| [3848e0e] | 342 | case OperatorNode::Incr:
|
|---|
| 343 | case OperatorNode::Decr:
|
|---|
| 344 | case OperatorNode::IncrPost:
|
|---|
| 345 | case OperatorNode::DecrPost:
|
|---|
| 346 | case OperatorNode::Assign:
|
|---|
| 347 | case OperatorNode::MulAssn:
|
|---|
| 348 | case OperatorNode::DivAssn:
|
|---|
| 349 | case OperatorNode::ModAssn:
|
|---|
| 350 | case OperatorNode::PlusAssn:
|
|---|
| 351 | case OperatorNode::MinusAssn:
|
|---|
| 352 | case OperatorNode::LSAssn:
|
|---|
| 353 | case OperatorNode::RSAssn:
|
|---|
| 354 | case OperatorNode::AndAssn:
|
|---|
| 355 | case OperatorNode::ERAssn:
|
|---|
| 356 | case OperatorNode::OrAssn:
|
|---|
| 357 | // the rewrite rules for these expressions specify that the first argument has its address taken
|
|---|
| 358 | assert( !args.empty() );
|
|---|
| 359 | args.front() = new AddressExpr( args.front() );
|
|---|
| 360 | break;
|
|---|
| 361 | default:
|
|---|
| 362 | /* do nothing */
|
|---|
| 363 | ;
|
|---|
| 364 | }
|
|---|
| [51b73452] | 365 |
|
|---|
| [3848e0e] | 366 | switch ( op->get_type() ) {
|
|---|
| 367 | case OperatorNode::Incr:
|
|---|
| 368 | case OperatorNode::Decr:
|
|---|
| 369 | case OperatorNode::IncrPost:
|
|---|
| 370 | case OperatorNode::DecrPost:
|
|---|
| 371 | case OperatorNode::Assign:
|
|---|
| 372 | case OperatorNode::MulAssn:
|
|---|
| 373 | case OperatorNode::DivAssn:
|
|---|
| 374 | case OperatorNode::ModAssn:
|
|---|
| 375 | case OperatorNode::PlusAssn:
|
|---|
| 376 | case OperatorNode::MinusAssn:
|
|---|
| 377 | case OperatorNode::LSAssn:
|
|---|
| 378 | case OperatorNode::RSAssn:
|
|---|
| 379 | case OperatorNode::AndAssn:
|
|---|
| 380 | case OperatorNode::ERAssn:
|
|---|
| 381 | case OperatorNode::OrAssn:
|
|---|
| 382 | case OperatorNode::Plus:
|
|---|
| 383 | case OperatorNode::Minus:
|
|---|
| 384 | case OperatorNode::Mul:
|
|---|
| 385 | case OperatorNode::Div:
|
|---|
| 386 | case OperatorNode::Mod:
|
|---|
| 387 | case OperatorNode::BitOr:
|
|---|
| 388 | case OperatorNode::BitAnd:
|
|---|
| 389 | case OperatorNode::Xor:
|
|---|
| 390 | case OperatorNode::LShift:
|
|---|
| 391 | case OperatorNode::RShift:
|
|---|
| 392 | case OperatorNode::LThan:
|
|---|
| 393 | case OperatorNode::GThan:
|
|---|
| 394 | case OperatorNode::LEThan:
|
|---|
| 395 | case OperatorNode::GEThan:
|
|---|
| 396 | case OperatorNode::Eq:
|
|---|
| 397 | case OperatorNode::Neq:
|
|---|
| 398 | case OperatorNode::Index:
|
|---|
| 399 | case OperatorNode::Range:
|
|---|
| 400 | case OperatorNode::UnPlus:
|
|---|
| 401 | case OperatorNode::UnMinus:
|
|---|
| 402 | case OperatorNode::PointTo:
|
|---|
| 403 | case OperatorNode::Neg:
|
|---|
| 404 | case OperatorNode::BitNeg:
|
|---|
| 405 | case OperatorNode::LabelAddress:
|
|---|
| 406 | return new UntypedExpr( new NameExpr( opFuncName[ op->get_type() ] ), args );
|
|---|
| 407 | case OperatorNode::AddressOf:
|
|---|
| 408 | assert( args.size() == 1 );
|
|---|
| 409 | assert( args.front() );
|
|---|
| 410 |
|
|---|
| 411 | return new AddressExpr( args.front() );
|
|---|
| 412 | case OperatorNode::Cast:
|
|---|
| 413 | {
|
|---|
| [bdd516a] | 414 | TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args());
|
|---|
| [3848e0e] | 415 | assert( arg );
|
|---|
| 416 |
|
|---|
| 417 | DeclarationNode *decl_node = arg->get_decl();
|
|---|
| [bdd516a] | 418 | ExpressionNode *expr_node = dynamic_cast<ExpressionNode *>( arg->get_link());
|
|---|
| [3848e0e] | 419 |
|
|---|
| 420 | Type *targetType = decl_node->buildType();
|
|---|
| 421 | if ( dynamic_cast< VoidType* >( targetType ) ) {
|
|---|
| 422 | delete targetType;
|
|---|
| 423 | return new CastExpr( expr_node->build(), maybeBuild< Expression >( get_argName() ) );
|
|---|
| 424 | } else {
|
|---|
| [bdd516a] | 425 | return new CastExpr( expr_node->build(),targetType, maybeBuild< Expression >( get_argName() ) );
|
|---|
| 426 | } // if
|
|---|
| [3848e0e] | 427 | }
|
|---|
| 428 | case OperatorNode::FieldSel:
|
|---|
| 429 | {
|
|---|
| 430 | assert( args.size() == 2 );
|
|---|
| 431 |
|
|---|
| [bdd516a] | 432 | NameExpr *member = dynamic_cast<NameExpr *>( args.back());
|
|---|
| 433 | // TupleExpr *memberTup = dynamic_cast<TupleExpr *>( args.back());
|
|---|
| [3848e0e] | 434 |
|
|---|
| [bdd516a] | 435 | if ( member != 0 ) {
|
|---|
| 436 | UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), args.front());
|
|---|
| 437 | delete member;
|
|---|
| 438 | return ret;
|
|---|
| [3848e0e] | 439 | /* else if ( memberTup != 0 )
|
|---|
| 440 | {
|
|---|
| [bdd516a] | 441 | UntypedMemberExpr *ret = new UntypedMemberExpr( memberTup->get_name(), args.front());
|
|---|
| [3848e0e] | 442 | delete member;
|
|---|
| 443 | return ret;
|
|---|
| 444 | } */
|
|---|
| [bdd516a] | 445 | } else
|
|---|
| [3848e0e] | 446 | assert( false );
|
|---|
| 447 | }
|
|---|
| 448 | case OperatorNode::PFieldSel:
|
|---|
| 449 | {
|
|---|
| 450 | assert( args.size() == 2 );
|
|---|
| 451 |
|
|---|
| [bdd516a] | 452 | NameExpr *member = dynamic_cast<NameExpr *>( args.back()); // modify for Tuples xxx
|
|---|
| [3848e0e] | 453 | assert( member != 0 );
|
|---|
| 454 |
|
|---|
| 455 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
|
|---|
| 456 | deref->get_args().push_back( args.front() );
|
|---|
| 457 |
|
|---|
| [bdd516a] | 458 | UntypedMemberExpr *ret = new UntypedMemberExpr( member->get_name(), deref );
|
|---|
| [3848e0e] | 459 | delete member;
|
|---|
| 460 | return ret;
|
|---|
| 461 | }
|
|---|
| 462 | case OperatorNode::AlignOf:
|
|---|
| 463 | case OperatorNode::SizeOf:
|
|---|
| 464 | {
|
|---|
| [bdd516a] | 465 | /// bool isSizeOf = ( op->get_type() == OperatorNode::SizeOf );
|
|---|
| [51b73452] | 466 |
|
|---|
| [bdd516a] | 467 | if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()) ) {
|
|---|
| 468 | return new SizeofExpr( arg->get_decl()->buildType());
|
|---|
| [3848e0e] | 469 | } else {
|
|---|
| [bdd516a] | 470 | return new SizeofExpr( args.front());
|
|---|
| 471 | } // if
|
|---|
| [3848e0e] | 472 | }
|
|---|
| 473 | case OperatorNode::Attr:
|
|---|
| 474 | {
|
|---|
| [bdd516a] | 475 | VarRefNode *var = dynamic_cast<VarRefNode *>( get_args());
|
|---|
| [3848e0e] | 476 | assert( var );
|
|---|
| 477 | if ( !get_args()->get_link() ) {
|
|---|
| [bdd516a] | 478 | return new AttrExpr( var->build(), ( Expression*)0);
|
|---|
| 479 | } else if ( TypeValueNode * arg = dynamic_cast<TypeValueNode *>( get_args()->get_link()) ) {
|
|---|
| 480 | return new AttrExpr( var->build(), arg->get_decl()->buildType());
|
|---|
| [3848e0e] | 481 | } else {
|
|---|
| [bdd516a] | 482 | return new AttrExpr( var->build(), args.back());
|
|---|
| 483 | } // if
|
|---|
| [3848e0e] | 484 | }
|
|---|
| 485 | case OperatorNode::CompLit:
|
|---|
| 486 | throw UnimplementedError( "C99 compound literals" );
|
|---|
| 487 | // the short-circuited operators
|
|---|
| 488 | case OperatorNode::Or:
|
|---|
| 489 | case OperatorNode::And:
|
|---|
| [bdd516a] | 490 | assert( args.size() == 2);
|
|---|
| 491 | return new LogicalExpr( notZeroExpr( args.front() ), notZeroExpr( args.back() ), ( op->get_type() == OperatorNode::And ) );
|
|---|
| [3848e0e] | 492 | case OperatorNode::Cond:
|
|---|
| 493 | {
|
|---|
| [bdd516a] | 494 | assert( args.size() == 3);
|
|---|
| [3848e0e] | 495 | std::list< Expression* >::const_iterator i = args.begin();
|
|---|
| 496 | Expression *arg1 = notZeroExpr( *i++ );
|
|---|
| 497 | Expression *arg2 = *i++;
|
|---|
| 498 | Expression *arg3 = *i++;
|
|---|
| 499 | return new ConditionalExpr( arg1, arg2, arg3 );
|
|---|
| 500 | }
|
|---|
| 501 | case OperatorNode::NCond:
|
|---|
| 502 | throw UnimplementedError( "GNU 2-argument conditional expression" );
|
|---|
| 503 | case OperatorNode::Comma:
|
|---|
| 504 | {
|
|---|
| [bdd516a] | 505 | assert( args.size() == 2);
|
|---|
| [3848e0e] | 506 | std::list< Expression* >::const_iterator i = args.begin();
|
|---|
| 507 | Expression *ret = *i++;
|
|---|
| 508 | while ( i != args.end() ) {
|
|---|
| 509 | ret = new CommaExpr( ret, *i++ );
|
|---|
| 510 | }
|
|---|
| 511 | return ret;
|
|---|
| 512 | }
|
|---|
| 513 | // Tuples
|
|---|
| 514 | case OperatorNode::TupleC:
|
|---|
| 515 | {
|
|---|
| 516 | TupleExpr *ret = new TupleExpr();
|
|---|
| 517 | std::copy( args.begin(), args.end(), back_inserter( ret->get_exprs() ) );
|
|---|
| 518 | return ret;
|
|---|
| 519 | }
|
|---|
| 520 | default:
|
|---|
| 521 | // shouldn't happen
|
|---|
| 522 | return 0;
|
|---|
| 523 | }
|
|---|
| [51b73452] | 524 | }
|
|---|
| 525 | }
|
|---|
| 526 |
|
|---|
| [bdd516a] | 527 | void CompositeExprNode::printOneLine( std::ostream &os, int indent ) const {
|
|---|
| 528 | printDesignation( os );
|
|---|
| [3848e0e] | 529 | os << "( ";
|
|---|
| 530 | function->printOneLine( os, indent );
|
|---|
| [bdd516a] | 531 | for ( ExpressionNode *cur = arguments; cur != 0; cur = dynamic_cast< ExpressionNode* >( cur->get_link() ) ) {
|
|---|
| [3848e0e] | 532 | cur->printOneLine( os, indent );
|
|---|
| 533 | }
|
|---|
| 534 | os << ") ";
|
|---|
| [51b73452] | 535 | }
|
|---|
| 536 |
|
|---|
| [bdd516a] | 537 | void CompositeExprNode::print( std::ostream &os, int indent ) const {
|
|---|
| 538 | printDesignation( os );
|
|---|
| 539 | os << '\r' << string( indent, ' ') << "Application of: " << endl;
|
|---|
| [3848e0e] | 540 | function->print( os, indent + ParseNode::indent_by );
|
|---|
| [51b73452] | 541 |
|
|---|
| [bdd516a] | 542 | os << '\r' << string( indent, ' ') ;
|
|---|
| [3848e0e] | 543 | if ( arguments ) {
|
|---|
| 544 | os << "... on arguments: " << endl;
|
|---|
| [bdd516a] | 545 | arguments->printList( os, indent + ParseNode::indent_by );
|
|---|
| [3848e0e] | 546 | } else
|
|---|
| 547 | os << "... on no arguments: " << endl;
|
|---|
| [51b73452] | 548 | }
|
|---|
| 549 |
|
|---|
| [bdd516a] | 550 | void CompositeExprNode::set_function( ExpressionNode *f ){
|
|---|
| [3848e0e] | 551 | function = f;
|
|---|
| [51b73452] | 552 | }
|
|---|
| 553 |
|
|---|
| [bdd516a] | 554 | void CompositeExprNode::set_args( ExpressionNode *args ){
|
|---|
| [3848e0e] | 555 | arguments = args;
|
|---|
| [51b73452] | 556 | }
|
|---|
| 557 |
|
|---|
| [bdd516a] | 558 | ExpressionNode *CompositeExprNode::get_function( void ) const {
|
|---|
| [3848e0e] | 559 | return function;
|
|---|
| [51b73452] | 560 | }
|
|---|
| 561 |
|
|---|
| [bdd516a] | 562 | ExpressionNode *CompositeExprNode::get_args( void ) const {
|
|---|
| [3848e0e] | 563 | return arguments;
|
|---|
| [51b73452] | 564 | }
|
|---|
| 565 |
|
|---|
| [bdd516a] | 566 | void CompositeExprNode::add_arg( ExpressionNode *arg ){
|
|---|
| 567 | if ( arguments )
|
|---|
| 568 | arguments->set_link( arg );
|
|---|
| [3848e0e] | 569 | else
|
|---|
| [bdd516a] | 570 | set_args( arg );
|
|---|
| [51b73452] | 571 | }
|
|---|
| 572 |
|
|---|
| [bdd516a] | 573 | CommaExprNode::CommaExprNode(): CompositeExprNode( new OperatorNode( OperatorNode::Comma )) {}
|
|---|
| [51b73452] | 574 |
|
|---|
| [bdd516a] | 575 | CommaExprNode::CommaExprNode( ExpressionNode *exp ) : CompositeExprNode( new OperatorNode( OperatorNode::Comma ), exp ) {
|
|---|
| [3848e0e] | 576 | }
|
|---|
| [51b73452] | 577 |
|
|---|
| [bdd516a] | 578 | CommaExprNode::CommaExprNode( ExpressionNode *exp1, ExpressionNode *exp2) : CompositeExprNode( new OperatorNode( OperatorNode::Comma ), exp1, exp2) {
|
|---|
| [51b73452] | 579 | }
|
|---|
| 580 |
|
|---|
| [bdd516a] | 581 | CommaExprNode *CommaExprNode::add_to_list( ExpressionNode *exp ){
|
|---|
| 582 | add_arg( exp );
|
|---|
| [51b73452] | 583 |
|
|---|
| [3848e0e] | 584 | return this;
|
|---|
| [51b73452] | 585 | }
|
|---|
| 586 |
|
|---|
| [3848e0e] | 587 | CommaExprNode::CommaExprNode( const CommaExprNode &other ) : CompositeExprNode( other ) {
|
|---|
| [51b73452] | 588 | }
|
|---|
| 589 |
|
|---|
| [bdd516a] | 590 | ValofExprNode::ValofExprNode( StatementNode *s ): body( s ) {}
|
|---|
| [51b73452] | 591 |
|
|---|
| [3848e0e] | 592 | ValofExprNode::ValofExprNode( const ValofExprNode &other ) : ExpressionNode( other ), body( maybeClone( body ) ) {
|
|---|
| [51b73452] | 593 | }
|
|---|
| 594 |
|
|---|
| 595 | ValofExprNode::~ValofExprNode() {
|
|---|
| [3848e0e] | 596 | delete body;
|
|---|
| [51b73452] | 597 | }
|
|---|
| 598 |
|
|---|
| 599 | void ValofExprNode::print( std::ostream &os, int indent ) const {
|
|---|
| [bdd516a] | 600 | printDesignation( os );
|
|---|
| 601 | os << string( indent, ' ') << "Valof Expression:" << std::endl;
|
|---|
| 602 | get_body()->print( os, indent + 4);
|
|---|
| [51b73452] | 603 | }
|
|---|
| 604 |
|
|---|
| [3848e0e] | 605 | void ValofExprNode::printOneLine( std::ostream &, int indent ) const {
|
|---|
| 606 | assert( false );
|
|---|
| [51b73452] | 607 | }
|
|---|
| 608 |
|
|---|
| 609 | Expression *ValofExprNode::build() const {
|
|---|
| [3848e0e] | 610 | return new UntypedValofExpr ( get_body()->build(), maybeBuild< Expression >( get_argName() ) );
|
|---|
| 611 | }
|
|---|
| 612 |
|
|---|
| [bdd516a] | 613 | ForCtlExprNode::ForCtlExprNode( ParseNode *init_, ExpressionNode *cond, ExpressionNode *incr ) throw ( SemanticError ) : condition( cond ), change( incr ) {
|
|---|
| [3848e0e] | 614 | if ( init_ == 0 )
|
|---|
| 615 | init = 0;
|
|---|
| 616 | else {
|
|---|
| 617 | DeclarationNode *decl;
|
|---|
| 618 | ExpressionNode *exp;
|
|---|
| 619 |
|
|---|
| [bdd516a] | 620 | if (( decl = dynamic_cast<DeclarationNode *>( init_)) != 0)
|
|---|
| 621 | init = new StatementNode( decl );
|
|---|
| 622 | else if (( exp = dynamic_cast<ExpressionNode *>( init_)) != 0)
|
|---|
| 623 | init = new StatementNode( StatementNode::Exp, exp );
|
|---|
| [3848e0e] | 624 | else
|
|---|
| 625 | throw SemanticError("Error in for control expression");
|
|---|
| 626 | }
|
|---|
| [51b73452] | 627 | }
|
|---|
| 628 |
|
|---|
| 629 | ForCtlExprNode::ForCtlExprNode( const ForCtlExprNode &other )
|
|---|
| [3848e0e] | 630 | : ExpressionNode( other ), init( maybeClone( other.init ) ), condition( maybeClone( other.condition ) ), change( maybeClone( other.change ) ) {
|
|---|
| [51b73452] | 631 | }
|
|---|
| 632 |
|
|---|
| 633 | ForCtlExprNode::~ForCtlExprNode(){
|
|---|
| [3848e0e] | 634 | delete init;
|
|---|
| 635 | delete condition;
|
|---|
| 636 | delete change;
|
|---|
| [51b73452] | 637 | }
|
|---|
| 638 |
|
|---|
| 639 | Expression *ForCtlExprNode::build() const {
|
|---|
| [3848e0e] | 640 | // this shouldn't be used!
|
|---|
| 641 | assert( false );
|
|---|
| 642 | return 0;
|
|---|
| [51b73452] | 643 | }
|
|---|
| 644 |
|
|---|
| 645 | void ForCtlExprNode::print( std::ostream &os, int indent ) const{
|
|---|
| [bdd516a] | 646 | os << string( indent,' ') << "For Control Expression -- : " << endl;
|
|---|
| [51b73452] | 647 |
|
|---|
| [bdd516a] | 648 | os << "\r" << string( indent + 2,' ') << "initialization: ";
|
|---|
| 649 | if ( init != 0)
|
|---|
| 650 | init->print( os, indent + 4);
|
|---|
| [51b73452] | 651 |
|
|---|
| [bdd516a] | 652 | os << "\n\r" << string( indent + 2,' ') << "condition: ";
|
|---|
| 653 | if ( condition != 0)
|
|---|
| 654 | condition->print( os, indent + 4);
|
|---|
| 655 | os << "\n\r" << string( indent + 2,' ') << "increment: ";
|
|---|
| 656 | if ( change != 0)
|
|---|
| 657 | change->print( os, indent + 4);
|
|---|
| [51b73452] | 658 | }
|
|---|
| 659 |
|
|---|
| [3848e0e] | 660 | void ForCtlExprNode::printOneLine( std::ostream &, int indent ) const {
|
|---|
| 661 | assert( false );
|
|---|
| [51b73452] | 662 | }
|
|---|
| 663 |
|
|---|
| [bdd516a] | 664 | TypeValueNode::TypeValueNode( DeclarationNode *decl )
|
|---|
| [3848e0e] | 665 | : decl( decl ) {
|
|---|
| [51b73452] | 666 | }
|
|---|
| 667 |
|
|---|
| 668 | TypeValueNode::TypeValueNode( const TypeValueNode &other )
|
|---|
| [3848e0e] | 669 | : ExpressionNode( other ), decl( maybeClone( other.decl ) ) {
|
|---|
| [51b73452] | 670 | }
|
|---|
| 671 |
|
|---|
| [3848e0e] | 672 | Expression *TypeValueNode::build() const {
|
|---|
| 673 | return new TypeExpr( decl->buildType() );
|
|---|
| [51b73452] | 674 | }
|
|---|
| 675 |
|
|---|
| [bdd516a] | 676 | void TypeValueNode::print( std::ostream &os, int indent ) const {
|
|---|
| [3848e0e] | 677 | os << std::string( indent, ' ' ) << "Type:";
|
|---|
| [bdd516a] | 678 | get_decl()->print( os, indent + 2);
|
|---|
| [51b73452] | 679 | }
|
|---|
| 680 |
|
|---|
| [bdd516a] | 681 | void TypeValueNode::printOneLine( std::ostream &os, int indent ) const {
|
|---|
| [3848e0e] | 682 | os << "Type:";
|
|---|
| [bdd516a] | 683 | get_decl()->print( os, indent + 2);
|
|---|
| [51b73452] | 684 | }
|
|---|
| 685 |
|
|---|
| [3848e0e] | 686 | ExpressionNode *flattenCommas( ExpressionNode *list ) {
|
|---|
| 687 | if ( CompositeExprNode *composite = dynamic_cast< CompositeExprNode * >( list ) )
|
|---|
| 688 | {
|
|---|
| 689 | OperatorNode *op;
|
|---|
| [bdd516a] | 690 | if ( ( op = dynamic_cast< OperatorNode * >( composite->get_function() )) && ( op->get_type() == OperatorNode::Comma ) )
|
|---|
| [3848e0e] | 691 | {
|
|---|
| 692 | if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) )
|
|---|
| 693 | composite->add_arg( next );
|
|---|
| 694 | return flattenCommas( composite->get_args() );
|
|---|
| 695 | }
|
|---|
| 696 | }
|
|---|
| [51b73452] | 697 |
|
|---|
| [3848e0e] | 698 | if ( ExpressionNode *next = dynamic_cast< ExpressionNode * >( list->get_link() ) )
|
|---|
| 699 | list->set_next( flattenCommas( next ) );
|
|---|
| [51b73452] | 700 |
|
|---|
| [3848e0e] | 701 | return list;
|
|---|
| [51b73452] | 702 | }
|
|---|
| 703 |
|
|---|
| [3848e0e] | 704 | ExpressionNode *tupleContents( ExpressionNode *tuple ) {
|
|---|
| 705 | if ( CompositeExprNode *composite = dynamic_cast< CompositeExprNode * >( tuple ) ) {
|
|---|
| 706 | OperatorNode *op = 0;
|
|---|
| [bdd516a] | 707 | if ( ( op = dynamic_cast< OperatorNode * >( composite->get_function() )) && ( op->get_type() == OperatorNode::TupleC ) )
|
|---|
| [3848e0e] | 708 | return composite->get_args();
|
|---|
| 709 | }
|
|---|
| 710 | return tuple;
|
|---|
| [51b73452] | 711 | }
|
|---|