[0dd3a2f] | 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 | // |
---|
[3be261a] | 7 | // Expression.cc -- |
---|
[0dd3a2f] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[e04ef3a] | 11 | // Last Modified By : Peter A. Buhr |
---|
[a5f0529] | 12 | // Last Modified On : Tue Jul 25 14:15:47 2017 |
---|
| 13 | // Update Count : 54 |
---|
[0dd3a2f] | 14 | // |
---|
| 15 | |
---|
[ea6332d] | 16 | #include "SynTree/Expression.h" |
---|
| 17 | |
---|
| 18 | #include <cassert> // for assert, assertf |
---|
| 19 | #include <iostream> // for ostream, operator<<, basic_ostream |
---|
| 20 | #include <list> // for list, _List_iterator, list<>::co... |
---|
| 21 | |
---|
| 22 | #include "Common/utility.h" // for maybeClone, cloneAll, deleteAll |
---|
| 23 | #include "Declaration.h" // for ObjectDecl, DeclarationWithType |
---|
| 24 | #include "Expression.h" // for Expression, ImplicitCopyCtorExpr |
---|
| 25 | #include "InitTweak/InitTweak.h" // for getCallArg, getPointerBase |
---|
| 26 | #include "Initializer.h" // for Designation, Initializer |
---|
| 27 | #include "Statement.h" // for CompoundStmt, ExprStmt, Statement |
---|
| 28 | #include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode |
---|
| 29 | #include "SynTree/Constant.h" // for Constant |
---|
| 30 | #include "Type.h" // for Type, BasicType, Type::Qualifiers |
---|
| 31 | #include "TypeSubstitution.h" // for TypeSubstitution |
---|
[51b7345] | 32 | |
---|
| 33 | |
---|
[906e24d] | 34 | Expression::Expression( Expression *_aname ) : result( 0 ), env( 0 ), argName( _aname ) {} |
---|
[0dd3a2f] | 35 | |
---|
[64ac636] | 36 | Expression::Expression( const Expression &other ) : BaseSyntaxNode( other ), result( maybeClone( other.result ) ), env( maybeClone( other.env ) ), argName( maybeClone( other.get_argName() ) ), extension( other.extension ) { |
---|
[0dd3a2f] | 37 | } |
---|
| 38 | |
---|
| 39 | Expression::~Expression() { |
---|
| 40 | delete env; |
---|
[7f5566b] | 41 | delete argName; // xxx -- there's a problem in cloning ConstantExpr I still don't know how to fix |
---|
[906e24d] | 42 | delete result; |
---|
[51b7345] | 43 | } |
---|
| 44 | |
---|
[59db689] | 45 | void Expression::print( std::ostream &os, int indent ) const { |
---|
[0dd3a2f] | 46 | if ( env ) { |
---|
[cf0941d] | 47 | os << std::string( indent, ' ' ) << "with environment:" << std::endl; |
---|
[0dd3a2f] | 48 | env->print( os, indent+2 ); |
---|
| 49 | } // if |
---|
[51b7345] | 50 | |
---|
[0dd3a2f] | 51 | if ( argName ) { |
---|
[cf0941d] | 52 | os << std::string( indent, ' ' ) << "with designator:"; |
---|
[0dd3a2f] | 53 | argName->print( os, indent+2 ); |
---|
| 54 | } // if |
---|
[e04ef3a] | 55 | |
---|
| 56 | if ( extension ) { |
---|
| 57 | os << std::string( indent, ' ' ) << "with extension:"; |
---|
| 58 | } // if |
---|
[51b7345] | 59 | } |
---|
| 60 | |
---|
[0dd3a2f] | 61 | ConstantExpr::ConstantExpr( Constant _c, Expression *_aname ) : Expression( _aname ), constant( _c ) { |
---|
[906e24d] | 62 | set_result( constant.get_type()->clone() ); |
---|
[51b7345] | 63 | } |
---|
| 64 | |
---|
[0dd3a2f] | 65 | ConstantExpr::ConstantExpr( const ConstantExpr &other) : Expression( other ), constant( other.constant ) { |
---|
| 66 | } |
---|
[51b7345] | 67 | |
---|
| 68 | ConstantExpr::~ConstantExpr() {} |
---|
| 69 | |
---|
[0dd3a2f] | 70 | void ConstantExpr::print( std::ostream &os, int indent ) const { |
---|
[60089f4] | 71 | os << "constant expression " ; |
---|
[cf0941d] | 72 | constant.print( os ); |
---|
[0dd3a2f] | 73 | Expression::print( os, indent ); |
---|
[51b7345] | 74 | } |
---|
| 75 | |
---|
[0dd3a2f] | 76 | VariableExpr::VariableExpr( DeclarationWithType *_var, Expression *_aname ) : Expression( _aname ), var( _var ) { |
---|
[db4ecc5] | 77 | assert( var ); |
---|
| 78 | assert( var->get_type() ); |
---|
[906e24d] | 79 | Type * type = var->get_type()->clone(); |
---|
[615a096] | 80 | type->set_lvalue( true ); |
---|
[906e24d] | 81 | set_result( type ); |
---|
[51b7345] | 82 | } |
---|
| 83 | |
---|
[0dd3a2f] | 84 | VariableExpr::VariableExpr( const VariableExpr &other ) : Expression( other ), var( other.var ) { |
---|
[51b7345] | 85 | } |
---|
| 86 | |
---|
[0dd3a2f] | 87 | VariableExpr::~VariableExpr() { |
---|
| 88 | // don't delete the declaration, since it points somewhere else in the tree |
---|
[51b7345] | 89 | } |
---|
| 90 | |
---|
[0dd3a2f] | 91 | void VariableExpr::print( std::ostream &os, int indent ) const { |
---|
[89231bc] | 92 | os << "Variable Expression: "; |
---|
[51b7345] | 93 | |
---|
[0dd3a2f] | 94 | Declaration *decl = get_var(); |
---|
| 95 | if ( decl != 0) decl->printShort(os, indent + 2); |
---|
| 96 | os << std::endl; |
---|
| 97 | Expression::print( os, indent ); |
---|
[51b7345] | 98 | } |
---|
| 99 | |
---|
| 100 | SizeofExpr::SizeofExpr( Expression *expr_, Expression *_aname ) : |
---|
[0dd3a2f] | 101 | Expression( _aname ), expr(expr_), type(0), isType(false) { |
---|
[906e24d] | 102 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
[51b7345] | 103 | } |
---|
| 104 | |
---|
| 105 | SizeofExpr::SizeofExpr( Type *type_, Expression *_aname ) : |
---|
[0dd3a2f] | 106 | Expression( _aname ), expr(0), type(type_), isType(true) { |
---|
[906e24d] | 107 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
[51b7345] | 108 | } |
---|
| 109 | |
---|
[0dd3a2f] | 110 | SizeofExpr::SizeofExpr( const SizeofExpr &other ) : |
---|
| 111 | Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { |
---|
[51b7345] | 112 | } |
---|
| 113 | |
---|
[0dd3a2f] | 114 | SizeofExpr::~SizeofExpr() { |
---|
| 115 | delete expr; |
---|
| 116 | delete type; |
---|
[51b7345] | 117 | } |
---|
| 118 | |
---|
| 119 | void SizeofExpr::print( std::ostream &os, int indent) const { |
---|
[89231bc] | 120 | os << "Sizeof Expression on: "; |
---|
[51b7345] | 121 | |
---|
[47534159] | 122 | if (isType) |
---|
| 123 | type->print(os, indent + 2); |
---|
| 124 | else |
---|
| 125 | expr->print(os, indent + 2); |
---|
| 126 | |
---|
| 127 | os << std::endl; |
---|
| 128 | Expression::print( os, indent ); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | AlignofExpr::AlignofExpr( Expression *expr_, Expression *_aname ) : |
---|
| 132 | Expression( _aname ), expr(expr_), type(0), isType(false) { |
---|
[906e24d] | 133 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
[47534159] | 134 | } |
---|
| 135 | |
---|
| 136 | AlignofExpr::AlignofExpr( Type *type_, Expression *_aname ) : |
---|
| 137 | Expression( _aname ), expr(0), type(type_), isType(true) { |
---|
[906e24d] | 138 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
[47534159] | 139 | } |
---|
| 140 | |
---|
| 141 | AlignofExpr::AlignofExpr( const AlignofExpr &other ) : |
---|
| 142 | Expression( other ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | AlignofExpr::~AlignofExpr() { |
---|
| 146 | delete expr; |
---|
| 147 | delete type; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | void AlignofExpr::print( std::ostream &os, int indent) const { |
---|
| 151 | os << std::string( indent, ' ' ) << "Alignof Expression on: "; |
---|
| 152 | |
---|
[0dd3a2f] | 153 | if (isType) |
---|
| 154 | type->print(os, indent + 2); |
---|
| 155 | else |
---|
| 156 | expr->print(os, indent + 2); |
---|
[51b7345] | 157 | |
---|
[0dd3a2f] | 158 | os << std::endl; |
---|
| 159 | Expression::print( os, indent ); |
---|
[51b7345] | 160 | } |
---|
| 161 | |
---|
[2a4b088] | 162 | UntypedOffsetofExpr::UntypedOffsetofExpr( Type *type_, const std::string &member_, Expression *_aname ) : |
---|
| 163 | Expression( _aname ), type(type_), member(member_) { |
---|
[906e24d] | 164 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
[2a4b088] | 165 | } |
---|
| 166 | |
---|
| 167 | UntypedOffsetofExpr::UntypedOffsetofExpr( const UntypedOffsetofExpr &other ) : |
---|
| 168 | Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {} |
---|
| 169 | |
---|
| 170 | UntypedOffsetofExpr::~UntypedOffsetofExpr() { |
---|
| 171 | delete type; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | void UntypedOffsetofExpr::print( std::ostream &os, int indent) const { |
---|
| 175 | os << std::string( indent, ' ' ) << "Untyped Offsetof Expression on member " << member << " of "; |
---|
| 176 | |
---|
| 177 | if ( type ) { |
---|
| 178 | type->print(os, indent + 2); |
---|
| 179 | } else { |
---|
| 180 | os << "<NULL>"; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | os << std::endl; |
---|
| 184 | Expression::print( os, indent ); |
---|
| 185 | } |
---|
| 186 | |
---|
[25a054f] | 187 | OffsetofExpr::OffsetofExpr( Type *type_, DeclarationWithType *member_, Expression *_aname ) : |
---|
| 188 | Expression( _aname ), type(type_), member(member_) { |
---|
[906e24d] | 189 | set_result( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ); |
---|
[25a054f] | 190 | } |
---|
| 191 | |
---|
| 192 | OffsetofExpr::OffsetofExpr( const OffsetofExpr &other ) : |
---|
[79970ed] | 193 | Expression( other ), type( maybeClone( other.type ) ), member( other.member ) {} |
---|
[25a054f] | 194 | |
---|
| 195 | OffsetofExpr::~OffsetofExpr() { |
---|
| 196 | delete type; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | void OffsetofExpr::print( std::ostream &os, int indent) const { |
---|
| 200 | os << std::string( indent, ' ' ) << "Offsetof Expression on member "; |
---|
| 201 | |
---|
| 202 | if ( member ) { |
---|
| 203 | os << member->get_name(); |
---|
| 204 | } else { |
---|
| 205 | os << "<NULL>"; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | os << " of "; |
---|
| 209 | |
---|
| 210 | if ( type ) { |
---|
| 211 | type->print(os, indent + 2); |
---|
| 212 | } else { |
---|
| 213 | os << "<NULL>"; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | os << std::endl; |
---|
| 217 | Expression::print( os, indent ); |
---|
| 218 | } |
---|
| 219 | |
---|
[afc1045] | 220 | OffsetPackExpr::OffsetPackExpr( StructInstType *type_, Expression *aname_ ) : Expression( aname_ ), type( type_ ) { |
---|
[906e24d] | 221 | set_result( new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0, false, false ) ); |
---|
[afc1045] | 222 | } |
---|
| 223 | |
---|
| 224 | OffsetPackExpr::OffsetPackExpr( const OffsetPackExpr &other ) : Expression( other ), type( maybeClone( other.type ) ) {} |
---|
| 225 | |
---|
| 226 | OffsetPackExpr::~OffsetPackExpr() { delete type; } |
---|
| 227 | |
---|
| 228 | void OffsetPackExpr::print( std::ostream &os, int indent ) const { |
---|
| 229 | os << std::string( indent, ' ' ) << "Offset pack expression on "; |
---|
| 230 | |
---|
| 231 | if ( type ) { |
---|
| 232 | type->print(os, indent + 2); |
---|
| 233 | } else { |
---|
| 234 | os << "<NULL>"; |
---|
| 235 | } |
---|
| 236 | |
---|
[25a054f] | 237 | os << std::endl; |
---|
| 238 | Expression::print( os, indent ); |
---|
| 239 | } |
---|
| 240 | |
---|
[51b7345] | 241 | AttrExpr::AttrExpr( Expression *attr, Expression *expr_, Expression *_aname ) : |
---|
[0dd3a2f] | 242 | Expression( _aname ), attr( attr ), expr(expr_), type(0), isType(false) { |
---|
[51b7345] | 243 | } |
---|
| 244 | |
---|
| 245 | AttrExpr::AttrExpr( Expression *attr, Type *type_, Expression *_aname ) : |
---|
[0dd3a2f] | 246 | Expression( _aname ), attr( attr ), expr(0), type(type_), isType(true) { |
---|
[51b7345] | 247 | } |
---|
| 248 | |
---|
[0dd3a2f] | 249 | AttrExpr::AttrExpr( const AttrExpr &other ) : |
---|
| 250 | Expression( other ), attr( maybeClone( other.attr ) ), expr( maybeClone( other.expr ) ), type( maybeClone( other.type ) ), isType( other.isType ) { |
---|
[51b7345] | 251 | } |
---|
| 252 | |
---|
[0dd3a2f] | 253 | AttrExpr::~AttrExpr() { |
---|
| 254 | delete attr; |
---|
| 255 | delete expr; |
---|
| 256 | delete type; |
---|
[51b7345] | 257 | } |
---|
| 258 | |
---|
| 259 | void AttrExpr::print( std::ostream &os, int indent) const { |
---|
[44b5ca0] | 260 | os << std::string( indent, ' ' ) << "Attr "; |
---|
[0dd3a2f] | 261 | attr->print( os, indent + 2 ); |
---|
| 262 | if ( isType || expr ) { |
---|
| 263 | os << "applied to: "; |
---|
[51b7345] | 264 | |
---|
[0dd3a2f] | 265 | if (isType) |
---|
| 266 | type->print(os, indent + 2); |
---|
| 267 | else |
---|
| 268 | expr->print(os, indent + 2); |
---|
| 269 | } // if |
---|
[51b7345] | 270 | |
---|
[0dd3a2f] | 271 | os << std::endl; |
---|
| 272 | Expression::print( os, indent ); |
---|
[51b7345] | 273 | } |
---|
| 274 | |
---|
[0dd3a2f] | 275 | CastExpr::CastExpr( Expression *arg_, Type *toType, Expression *_aname ) : Expression( _aname ), arg(arg_) { |
---|
[906e24d] | 276 | set_result(toType); |
---|
[51b7345] | 277 | } |
---|
| 278 | |
---|
| 279 | CastExpr::CastExpr( Expression *arg_, Expression *_aname ) : Expression( _aname ), arg(arg_) { |
---|
[906e24d] | 280 | set_result( new VoidType( Type::Qualifiers() ) ); |
---|
[51b7345] | 281 | } |
---|
| 282 | |
---|
[0dd3a2f] | 283 | CastExpr::CastExpr( const CastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) { |
---|
[51b7345] | 284 | } |
---|
| 285 | |
---|
| 286 | CastExpr::~CastExpr() { |
---|
[0dd3a2f] | 287 | delete arg; |
---|
[51b7345] | 288 | } |
---|
| 289 | |
---|
| 290 | void CastExpr::print( std::ostream &os, int indent ) const { |
---|
[89231bc] | 291 | os << "Cast of:" << std::endl << std::string( indent+2, ' ' ); |
---|
[0dd3a2f] | 292 | arg->print(os, indent+2); |
---|
[44b5ca0] | 293 | os << std::endl << std::string( indent, ' ' ) << "to:" << std::endl; |
---|
[906e24d] | 294 | os << std::string( indent+2, ' ' ); |
---|
| 295 | if ( result->isVoid() ) { |
---|
| 296 | os << "nothing"; |
---|
[0dd3a2f] | 297 | } else { |
---|
[906e24d] | 298 | result->print( os, indent+2 ); |
---|
[0dd3a2f] | 299 | } // if |
---|
[906e24d] | 300 | os << std::endl; |
---|
[0dd3a2f] | 301 | Expression::print( os, indent ); |
---|
| 302 | } |
---|
| 303 | |
---|
[a5f0529] | 304 | VirtualCastExpr::VirtualCastExpr( Expression *arg_, Type *toType ) : Expression(), arg(arg_) { |
---|
| 305 | set_result(toType); |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | VirtualCastExpr::VirtualCastExpr( const VirtualCastExpr &other ) : Expression( other ), arg( maybeClone( other.arg ) ) { |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | VirtualCastExpr::~VirtualCastExpr() { |
---|
| 312 | delete arg; |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | void VirtualCastExpr::print( std::ostream &os, int indent ) const { |
---|
| 316 | os << "Virtual Cast of:" << std::endl << std::string( indent+2, ' ' ); |
---|
| 317 | arg->print(os, indent+2); |
---|
| 318 | os << std::endl << std::string( indent, ' ' ) << "to:" << std::endl; |
---|
| 319 | os << std::string( indent+2, ' ' ); |
---|
| 320 | if ( ! result ) { |
---|
| 321 | os << "unknown"; |
---|
| 322 | } else { |
---|
| 323 | result->print( os, indent+2 ); |
---|
| 324 | } // if |
---|
| 325 | os << std::endl; |
---|
| 326 | Expression::print( os, indent ); |
---|
| 327 | } |
---|
| 328 | |
---|
[3b58d91] | 329 | UntypedMemberExpr::UntypedMemberExpr( Expression * _member, Expression *_aggregate, Expression *_aname ) : |
---|
[0dd3a2f] | 330 | Expression( _aname ), member(_member), aggregate(_aggregate) {} |
---|
[51b7345] | 331 | |
---|
[0dd3a2f] | 332 | UntypedMemberExpr::UntypedMemberExpr( const UntypedMemberExpr &other ) : |
---|
[3b58d91] | 333 | Expression( other ), member( maybeClone( other.member ) ), aggregate( maybeClone( other.aggregate ) ) { |
---|
[51b7345] | 334 | } |
---|
| 335 | |
---|
| 336 | UntypedMemberExpr::~UntypedMemberExpr() { |
---|
[0dd3a2f] | 337 | delete aggregate; |
---|
[3b58d91] | 338 | delete member; |
---|
[51b7345] | 339 | } |
---|
| 340 | |
---|
| 341 | void UntypedMemberExpr::print( std::ostream &os, int indent ) const { |
---|
[3b58d91] | 342 | os << "Untyped Member Expression, with field: " << std::endl; |
---|
[8c49c0e] | 343 | os << std::string( indent+2, ' ' ); |
---|
[3b58d91] | 344 | get_member()->print(os, indent+4); |
---|
| 345 | os << std::string( indent+2, ' ' ); |
---|
[51b7345] | 346 | |
---|
[0dd3a2f] | 347 | Expression *agg = get_aggregate(); |
---|
[3b58d91] | 348 | os << "from aggregate: " << std::endl; |
---|
[89231bc] | 349 | if (agg != 0) { |
---|
[3b58d91] | 350 | os << std::string( indent + 4, ' ' ); |
---|
| 351 | agg->print(os, indent + 4); |
---|
[89231bc] | 352 | } |
---|
| 353 | os << std::string( indent+2, ' ' ); |
---|
[0dd3a2f] | 354 | Expression::print( os, indent ); |
---|
[51b7345] | 355 | } |
---|
| 356 | |
---|
[d9fa60a] | 357 | namespace { |
---|
| 358 | TypeSubstitution makeSub( Type * t ) { |
---|
| 359 | if ( StructInstType * aggInst = dynamic_cast< StructInstType * >( t ) ) { |
---|
| 360 | return TypeSubstitution( aggInst->get_baseParameters()->begin(), aggInst->get_baseParameters()->end(), aggInst->get_parameters().begin() ); |
---|
| 361 | } else if ( UnionInstType * aggInst = dynamic_cast< UnionInstType * >( t ) ) { |
---|
| 362 | return TypeSubstitution( aggInst->get_baseParameters()->begin(), aggInst->get_baseParameters()->end(), aggInst->get_parameters().begin() ); |
---|
| 363 | } else { |
---|
[4b0f997] | 364 | assertf( false, "makeSub expects struct or union type for aggregate, but got: %s", toString( t ).c_str() ); |
---|
[d9fa60a] | 365 | } |
---|
| 366 | } |
---|
| 367 | } |
---|
| 368 | |
---|
[51b7345] | 369 | |
---|
| 370 | MemberExpr::MemberExpr( DeclarationWithType *_member, Expression *_aggregate, Expression *_aname ) : |
---|
[0dd3a2f] | 371 | Expression( _aname ), member(_member), aggregate(_aggregate) { |
---|
[d9fa60a] | 372 | |
---|
| 373 | TypeSubstitution sub( makeSub( aggregate->get_result() ) ); |
---|
| 374 | Type * res = member->get_type()->clone(); |
---|
| 375 | sub.apply( res ); |
---|
| 376 | set_result( res ); |
---|
[615a096] | 377 | get_result()->set_lvalue( true ); |
---|
[51b7345] | 378 | } |
---|
| 379 | |
---|
[0dd3a2f] | 380 | MemberExpr::MemberExpr( const MemberExpr &other ) : |
---|
[39f84a4] | 381 | Expression( other ), member( other.member ), aggregate( maybeClone( other.aggregate ) ) { |
---|
[51b7345] | 382 | } |
---|
| 383 | |
---|
| 384 | MemberExpr::~MemberExpr() { |
---|
[4551a6e] | 385 | // don't delete the member declaration, since it points somewhere else in the tree |
---|
[0dd3a2f] | 386 | delete aggregate; |
---|
[51b7345] | 387 | } |
---|
| 388 | |
---|
| 389 | void MemberExpr::print( std::ostream &os, int indent ) const { |
---|
[89231bc] | 390 | os << "Member Expression, with field: " << std::endl; |
---|
[51b7345] | 391 | |
---|
[0dd3a2f] | 392 | assert( member ); |
---|
[44b5ca0] | 393 | os << std::string( indent + 2, ' ' ); |
---|
[0dd3a2f] | 394 | member->print( os, indent + 2 ); |
---|
| 395 | os << std::endl; |
---|
[51b7345] | 396 | |
---|
[0dd3a2f] | 397 | Expression *agg = get_aggregate(); |
---|
[44b5ca0] | 398 | os << std::string( indent, ' ' ) << "from aggregate: " << std::endl; |
---|
[89231bc] | 399 | if (agg != 0) { |
---|
[bb8ea30] | 400 | os << std::string( indent + 2, ' ' ); |
---|
[89231bc] | 401 | agg->print(os, indent + 2); |
---|
| 402 | } |
---|
| 403 | os << std::string( indent+2, ' ' ); |
---|
[0dd3a2f] | 404 | Expression::print( os, indent ); |
---|
[51b7345] | 405 | } |
---|
| 406 | |
---|
[6eb8948] | 407 | UntypedExpr::UntypedExpr( Expression *_function, const std::list<Expression *> &_args, Expression *_aname ) : |
---|
| 408 | Expression( _aname ), function(_function), args(_args) {} |
---|
[51b7345] | 409 | |
---|
[0dd3a2f] | 410 | UntypedExpr::UntypedExpr( const UntypedExpr &other ) : |
---|
| 411 | Expression( other ), function( maybeClone( other.function ) ) { |
---|
| 412 | cloneAll( other.args, args ); |
---|
[51b7345] | 413 | } |
---|
| 414 | |
---|
[ac71a86] | 415 | UntypedExpr::~UntypedExpr() { |
---|
| 416 | delete function; |
---|
[03b812d2] | 417 | deleteAll( args ); |
---|
[ac71a86] | 418 | } |
---|
[51b7345] | 419 | |
---|
[b3b2077] | 420 | UntypedExpr * UntypedExpr::createDeref( Expression * expr ) { |
---|
| 421 | UntypedExpr * ret = new UntypedExpr( new NameExpr("*?"), std::list< Expression * >{ expr } ); |
---|
| 422 | if ( Type * type = expr->get_result() ) { |
---|
| 423 | Type * base = InitTweak::getPointerBase( type ); |
---|
| 424 | if ( ! base ) { |
---|
| 425 | std::cerr << type << std::endl; |
---|
| 426 | } |
---|
| 427 | assertf( base, "expected pointer type in dereference\n" ); |
---|
| 428 | ret->set_result( maybeClone( base ) ); |
---|
| 429 | } |
---|
| 430 | return ret; |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | UntypedExpr * UntypedExpr::createAssign( Expression * arg1, Expression * arg2 ) { |
---|
| 434 | assert( arg1 && arg2 ); |
---|
| 435 | UntypedExpr * ret = new UntypedExpr( new NameExpr( "?=?" ), std::list< Expression * >{ arg1, arg2 } ); |
---|
| 436 | if ( arg1->get_result() && arg2->get_result() ) { |
---|
| 437 | // if both expressions are typed, assumes that this assignment is a C bitwise assignment, |
---|
| 438 | // so the result is the type of the RHS |
---|
| 439 | ret->set_result( arg2->get_result()->clone() ); |
---|
| 440 | } |
---|
| 441 | return ret; |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | |
---|
[51b7345] | 445 | void UntypedExpr::print( std::ostream &os, int indent ) const { |
---|
[89231bc] | 446 | os << "Applying untyped: " << std::endl; |
---|
[bb8ea30] | 447 | os << std::string( indent+2, ' ' ); |
---|
| 448 | function->print(os, indent + 2); |
---|
[44b5ca0] | 449 | os << std::string( indent, ' ' ) << "...to: " << std::endl; |
---|
[bb8ea30] | 450 | printAll(args, os, indent + 2); |
---|
[0dd3a2f] | 451 | Expression::print( os, indent ); |
---|
[51b7345] | 452 | } |
---|
| 453 | |
---|
[0dd3a2f] | 454 | void UntypedExpr::printArgs( std::ostream &os, int indent ) const { |
---|
| 455 | std::list<Expression *>::const_iterator i; |
---|
[60089f4] | 456 | for (i = args.begin(); i != args.end(); i++) { |
---|
| 457 | os << std::string(indent, ' ' ); |
---|
[0dd3a2f] | 458 | (*i)->print(os, indent); |
---|
[60089f4] | 459 | } |
---|
[51b7345] | 460 | } |
---|
| 461 | |
---|
[4cb935e] | 462 | NameExpr::NameExpr( std::string _name, Expression *_aname ) : Expression( _aname ), name(_name) { |
---|
| 463 | assertf(_name != "0", "Zero is not a valid name\n"); |
---|
| 464 | assertf(_name != "1", "One is not a valid name\n"); |
---|
| 465 | } |
---|
[51b7345] | 466 | |
---|
[0dd3a2f] | 467 | NameExpr::NameExpr( const NameExpr &other ) : Expression( other ), name( other.name ) { |
---|
[51b7345] | 468 | } |
---|
| 469 | |
---|
| 470 | NameExpr::~NameExpr() {} |
---|
| 471 | |
---|
| 472 | void NameExpr::print( std::ostream &os, int indent ) const { |
---|
[89231bc] | 473 | os << "Name: " << get_name() << std::endl; |
---|
[0dd3a2f] | 474 | Expression::print( os, indent ); |
---|
[51b7345] | 475 | } |
---|
| 476 | |
---|
| 477 | LogicalExpr::LogicalExpr( Expression *arg1_, Expression *arg2_, bool andp, Expression *_aname ) : |
---|
[0dd3a2f] | 478 | Expression( _aname ), arg1(arg1_), arg2(arg2_), isAnd(andp) { |
---|
[906e24d] | 479 | set_result( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); |
---|
[51b7345] | 480 | } |
---|
| 481 | |
---|
[0dd3a2f] | 482 | LogicalExpr::LogicalExpr( const LogicalExpr &other ) : |
---|
| 483 | Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), isAnd( other.isAnd ) { |
---|
[51b7345] | 484 | } |
---|
| 485 | |
---|
[a08ba92] | 486 | LogicalExpr::~LogicalExpr() { |
---|
[0dd3a2f] | 487 | delete arg1; |
---|
| 488 | delete arg2; |
---|
[51b7345] | 489 | } |
---|
| 490 | |
---|
| 491 | void LogicalExpr::print( std::ostream &os, int indent )const { |
---|
[44b5ca0] | 492 | os << std::string( indent, ' ' ) << "Short-circuited operation (" << (isAnd?"and":"or") << ") on: "; |
---|
[0dd3a2f] | 493 | arg1->print(os); |
---|
| 494 | os << " and "; |
---|
| 495 | arg2->print(os); |
---|
| 496 | os << std::endl; |
---|
| 497 | Expression::print( os, indent ); |
---|
[51b7345] | 498 | } |
---|
| 499 | |
---|
| 500 | ConditionalExpr::ConditionalExpr( Expression *arg1_, Expression *arg2_, Expression *arg3_, Expression *_aname ) : |
---|
[0dd3a2f] | 501 | Expression( _aname ), arg1(arg1_), arg2(arg2_), arg3(arg3_) {} |
---|
[51b7345] | 502 | |
---|
[0dd3a2f] | 503 | ConditionalExpr::ConditionalExpr( const ConditionalExpr &other ) : |
---|
| 504 | Expression( other ), arg1( maybeClone( other.arg1 ) ), arg2( maybeClone( other.arg2 ) ), arg3( maybeClone( other.arg3 ) ) { |
---|
| 505 | } |
---|
[51b7345] | 506 | |
---|
| 507 | ConditionalExpr::~ConditionalExpr() { |
---|
[0dd3a2f] | 508 | delete arg1; |
---|
| 509 | delete arg2; |
---|
| 510 | delete arg3; |
---|
[51b7345] | 511 | } |
---|
| 512 | |
---|
| 513 | void ConditionalExpr::print( std::ostream &os, int indent ) const { |
---|
[b3b2077] | 514 | os << "Conditional expression on: " << std::endl; |
---|
| 515 | os << std::string( indent+2, ' ' ); |
---|
[0dd3a2f] | 516 | arg1->print( os, indent+2 ); |
---|
[44b5ca0] | 517 | os << std::string( indent, ' ' ) << "First alternative:" << std::endl; |
---|
[b3b2077] | 518 | os << std::string( indent+2, ' ' ); |
---|
[0dd3a2f] | 519 | arg2->print( os, indent+2 ); |
---|
[44b5ca0] | 520 | os << std::string( indent, ' ' ) << "Second alternative:" << std::endl; |
---|
[b3b2077] | 521 | os << std::string( indent+2, ' ' ); |
---|
[0dd3a2f] | 522 | arg3->print( os, indent+2 ); |
---|
| 523 | os << std::endl; |
---|
| 524 | Expression::print( os, indent ); |
---|
| 525 | } |
---|
| 526 | |
---|
[a5f0529] | 527 | AsmExpr::AsmExpr( const AsmExpr & other ) : Expression( other ), inout( maybeClone( other.inout ) ), constraint( maybeClone( other.constraint ) ), operand( maybeClone( other.operand ) ) {} |
---|
[3be261a] | 528 | |
---|
| 529 | |
---|
[7f5566b] | 530 | void AsmExpr::print( std::ostream &os, int indent ) const { |
---|
| 531 | os << "Asm Expression: " << std::endl; |
---|
| 532 | if ( inout ) inout->print( os, indent + 2 ); |
---|
| 533 | if ( constraint ) constraint->print( os, indent + 2 ); |
---|
| 534 | if ( operand ) operand->print( os, indent + 2 ); |
---|
| 535 | } |
---|
| 536 | |
---|
[db4ecc5] | 537 | |
---|
| 538 | ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( ApplicationExpr * callExpr ) : callExpr( callExpr ) { |
---|
| 539 | assert( callExpr ); |
---|
[906e24d] | 540 | assert( callExpr->has_result() ); |
---|
| 541 | set_result( callExpr->get_result()->clone() ); |
---|
[db4ecc5] | 542 | } |
---|
| 543 | |
---|
| 544 | ImplicitCopyCtorExpr::ImplicitCopyCtorExpr( const ImplicitCopyCtorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) { |
---|
| 545 | cloneAll( other.tempDecls, tempDecls ); |
---|
| 546 | cloneAll( other.returnDecls, returnDecls ); |
---|
| 547 | cloneAll( other.dtors, dtors ); |
---|
| 548 | } |
---|
| 549 | |
---|
| 550 | ImplicitCopyCtorExpr::~ImplicitCopyCtorExpr() { |
---|
[d5556a3] | 551 | set_env( nullptr ); // ImplicitCopyCtorExpr does not take ownership of an environment |
---|
[db4ecc5] | 552 | delete callExpr; |
---|
| 553 | deleteAll( tempDecls ); |
---|
| 554 | deleteAll( returnDecls ); |
---|
| 555 | deleteAll( dtors ); |
---|
| 556 | } |
---|
| 557 | |
---|
| 558 | void ImplicitCopyCtorExpr::print( std::ostream &os, int indent ) const { |
---|
[b6fe7e6] | 559 | os << "Implicit Copy Constructor Expression: " << std::endl; |
---|
[db4ecc5] | 560 | assert( callExpr ); |
---|
[d5556a3] | 561 | os << std::string( indent+2, ' ' ); |
---|
[db4ecc5] | 562 | callExpr->print( os, indent + 2 ); |
---|
| 563 | os << std::endl << std::string( indent, ' ' ) << "with temporaries:" << std::endl; |
---|
| 564 | printAll(tempDecls, os, indent+2); |
---|
[dc2e7e0] | 565 | os << std::endl << std::string( indent, ' ' ) << "with return temporaries:" << std::endl; |
---|
| 566 | printAll(returnDecls, os, indent+2); |
---|
[db4ecc5] | 567 | Expression::print( os, indent ); |
---|
| 568 | } |
---|
| 569 | |
---|
[3be261a] | 570 | |
---|
[b6fe7e6] | 571 | ConstructorExpr::ConstructorExpr( Expression * callExpr ) : callExpr( callExpr ) { |
---|
| 572 | // allow resolver to type a constructor used as an expression as if it has the same type as its first argument |
---|
| 573 | assert( callExpr ); |
---|
| 574 | Expression * arg = InitTweak::getCallArg( callExpr, 0 ); |
---|
| 575 | assert( arg ); |
---|
[906e24d] | 576 | set_result( maybeClone( arg->get_result() ) ); |
---|
[b6fe7e6] | 577 | } |
---|
[3be261a] | 578 | |
---|
[b6fe7e6] | 579 | ConstructorExpr::ConstructorExpr( const ConstructorExpr & other ) : Expression( other ), callExpr( maybeClone( other.callExpr ) ) { |
---|
| 580 | } |
---|
| 581 | |
---|
| 582 | ConstructorExpr::~ConstructorExpr() { |
---|
| 583 | delete callExpr; |
---|
| 584 | } |
---|
| 585 | |
---|
| 586 | void ConstructorExpr::print( std::ostream &os, int indent ) const { |
---|
| 587 | os << "Constructor Expression: " << std::endl; |
---|
| 588 | assert( callExpr ); |
---|
| 589 | os << std::string( indent+2, ' ' ); |
---|
| 590 | callExpr->print( os, indent + 2 ); |
---|
| 591 | Expression::print( os, indent ); |
---|
[0dd3a2f] | 592 | } |
---|
| 593 | |
---|
[3be261a] | 594 | |
---|
[fbcde64] | 595 | CompoundLiteralExpr::CompoundLiteralExpr( Type * type, Initializer * initializer ) : initializer( initializer ) { |
---|
[3c13c03] | 596 | assert( type && initializer ); |
---|
[fbcde64] | 597 | set_result( type ); |
---|
[630a82a] | 598 | } |
---|
| 599 | |
---|
[fbcde64] | 600 | CompoundLiteralExpr::CompoundLiteralExpr( const CompoundLiteralExpr &other ) : Expression( other ), initializer( other.initializer->clone() ) {} |
---|
[630a82a] | 601 | |
---|
| 602 | CompoundLiteralExpr::~CompoundLiteralExpr() { |
---|
| 603 | delete initializer; |
---|
| 604 | } |
---|
| 605 | |
---|
| 606 | void CompoundLiteralExpr::print( std::ostream &os, int indent ) const { |
---|
| 607 | os << "Compound Literal Expression: " << std::endl; |
---|
[3c13c03] | 608 | os << std::string( indent+2, ' ' ); |
---|
[fbcde64] | 609 | get_result()->print( os, indent + 2 ); |
---|
[3c13c03] | 610 | os << std::string( indent+2, ' ' ); |
---|
| 611 | initializer->print( os, indent + 2 ); |
---|
[d5556a3] | 612 | Expression::print( os, indent ); |
---|
[630a82a] | 613 | } |
---|
| 614 | |
---|
[d9e2280] | 615 | RangeExpr::RangeExpr( Expression *low, Expression *high ) : low( low ), high( high ) {} |
---|
[3c13c03] | 616 | RangeExpr::RangeExpr( const RangeExpr &other ) : Expression( other ), low( other.low->clone() ), high( other.high->clone() ) {} |
---|
[8688ce1] | 617 | void RangeExpr::print( std::ostream &os, int indent ) const { |
---|
[3c13c03] | 618 | os << "Range Expression: "; |
---|
[8688ce1] | 619 | low->print( os, indent ); |
---|
| 620 | os << " ... "; |
---|
| 621 | high->print( os, indent ); |
---|
[d5556a3] | 622 | Expression::print( os, indent ); |
---|
[8688ce1] | 623 | } |
---|
[3be261a] | 624 | |
---|
[6eb8948] | 625 | StmtExpr::StmtExpr( CompoundStmt *statements ) : statements( statements ) { |
---|
| 626 | assert( statements ); |
---|
| 627 | std::list< Statement * > & body = statements->get_kids(); |
---|
| 628 | if ( ! body.empty() ) { |
---|
| 629 | if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( body.back() ) ) { |
---|
[aa8f9df] | 630 | set_result( maybeClone( exprStmt->get_expr()->get_result() ) ); |
---|
[6eb8948] | 631 | } |
---|
| 632 | } |
---|
| 633 | } |
---|
[d5556a3] | 634 | StmtExpr::StmtExpr( const StmtExpr &other ) : Expression( other ), statements( other.statements->clone() ) { |
---|
| 635 | cloneAll( other.returnDecls, returnDecls ); |
---|
| 636 | cloneAll( other.dtors, dtors ); |
---|
| 637 | } |
---|
[6eb8948] | 638 | StmtExpr::~StmtExpr() { |
---|
| 639 | delete statements; |
---|
[d5556a3] | 640 | deleteAll( dtors ); |
---|
| 641 | deleteAll( returnDecls ); |
---|
[6eb8948] | 642 | } |
---|
| 643 | void StmtExpr::print( std::ostream &os, int indent ) const { |
---|
[3c13c03] | 644 | os << "Statement Expression: " << std::endl << std::string( indent, ' ' ); |
---|
[6eb8948] | 645 | statements->print( os, indent+2 ); |
---|
[d5556a3] | 646 | if ( ! returnDecls.empty() ) { |
---|
| 647 | os << std::string( indent+2, ' ' ) << "with returnDecls: "; |
---|
| 648 | printAll( returnDecls, os, indent+2 ); |
---|
| 649 | } |
---|
| 650 | if ( ! dtors.empty() ) { |
---|
| 651 | os << std::string( indent+2, ' ' ) << "with dtors: "; |
---|
| 652 | printAll( dtors, os, indent+2 ); |
---|
| 653 | } |
---|
| 654 | Expression::print( os, indent ); |
---|
[6eb8948] | 655 | } |
---|
| 656 | |
---|
[3c13c03] | 657 | |
---|
[bf32bb8] | 658 | long long UniqueExpr::count = 0; |
---|
[141b786] | 659 | UniqueExpr::UniqueExpr( Expression *expr, long long idVal ) : expr( expr ), object( nullptr ), var( nullptr ), id( idVal ) { |
---|
| 660 | assert( expr ); |
---|
[bf32bb8] | 661 | assert( count != -1 ); |
---|
| 662 | if ( id == -1 ) id = count++; |
---|
| 663 | if ( expr->get_result() ) { |
---|
| 664 | set_result( expr->get_result()->clone() ); |
---|
| 665 | } |
---|
[3c13c03] | 666 | } |
---|
[141b786] | 667 | UniqueExpr::UniqueExpr( const UniqueExpr &other ) : Expression( other ), expr( maybeClone( other.expr ) ), object( maybeClone( other.object ) ), var( maybeClone( other.var ) ), id( other.id ) { |
---|
[3c13c03] | 668 | } |
---|
| 669 | UniqueExpr::~UniqueExpr() { |
---|
[141b786] | 670 | delete expr; |
---|
| 671 | delete object; |
---|
| 672 | delete var; |
---|
[3c13c03] | 673 | } |
---|
| 674 | void UniqueExpr::print( std::ostream &os, int indent ) const { |
---|
[bf32bb8] | 675 | os << "Unique Expression with id:" << id << std::endl << std::string( indent+2, ' ' ); |
---|
[3c13c03] | 676 | get_expr()->print( os, indent+2 ); |
---|
[77971f6] | 677 | if ( get_object() ) { |
---|
[d5556a3] | 678 | os << std::string( indent+2, ' ' ) << "with decl: "; |
---|
[77971f6] | 679 | get_object()->printShort( os, indent+2 ); |
---|
| 680 | } |
---|
[d5556a3] | 681 | Expression::print( os, indent ); |
---|
[3c13c03] | 682 | } |
---|
| 683 | |
---|
[e4d829b] | 684 | InitAlternative::InitAlternative( Type * type, Designation * designation ) : type( type ), designation( designation ) {} |
---|
| 685 | InitAlternative::InitAlternative( const InitAlternative & other ) : type( maybeClone( other.type ) ), designation( maybeClone( other.designation ) ) {} |
---|
| 686 | InitAlternative::~InitAlternative() { |
---|
| 687 | delete type; |
---|
| 688 | delete designation; |
---|
| 689 | } |
---|
| 690 | |
---|
| 691 | UntypedInitExpr::UntypedInitExpr( Expression * expr, const std::list<InitAlternative> & initAlts ) : expr( expr ), initAlts( initAlts ) {} |
---|
| 692 | UntypedInitExpr::UntypedInitExpr( const UntypedInitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), initAlts( other.initAlts ) {} |
---|
| 693 | UntypedInitExpr::~UntypedInitExpr() { |
---|
| 694 | delete expr; |
---|
| 695 | } |
---|
| 696 | |
---|
| 697 | void UntypedInitExpr::print( std::ostream & os, int indent ) const { |
---|
| 698 | os << "Untyped Init Expression" << std::endl << std::string( indent+2, ' ' ); |
---|
| 699 | expr->print( os, indent+2 ); |
---|
| 700 | if ( ! initAlts.empty() ) { |
---|
| 701 | for ( const InitAlternative & alt : initAlts ) { |
---|
| 702 | os << std::string( indent+2, ' ' ) << "InitAlternative: "; |
---|
| 703 | alt.type->print( os, indent+2 ); |
---|
| 704 | alt.designation->print( os, indent+2 ); |
---|
| 705 | } |
---|
| 706 | } |
---|
| 707 | } |
---|
| 708 | |
---|
[62423350] | 709 | InitExpr::InitExpr( Expression * expr, Designation * designation ) : expr( expr ), designation( designation ) { |
---|
| 710 | set_result( expr->get_result()->clone() ); |
---|
[e4d829b] | 711 | } |
---|
| 712 | InitExpr::InitExpr( const InitExpr & other ) : Expression( other ), expr( maybeClone( other.expr ) ), designation( maybeClone( other.designation) ) {} |
---|
| 713 | InitExpr::~InitExpr() { |
---|
| 714 | delete expr; |
---|
| 715 | delete designation; |
---|
| 716 | } |
---|
| 717 | |
---|
| 718 | void InitExpr::print( std::ostream & os, int indent ) const { |
---|
| 719 | os << "Init Expression" << std::endl << std::string( indent+2, ' ' ); |
---|
| 720 | expr->print( os, indent+2 ); |
---|
| 721 | os << std::string( indent+2, ' ' ) << "with designation: "; |
---|
| 722 | designation->print( os, indent+2 ); |
---|
| 723 | } |
---|
| 724 | |
---|
| 725 | |
---|
[3906301] | 726 | std::ostream & operator<<( std::ostream & out, const Expression * expr ) { |
---|
[8bf784a] | 727 | if ( expr ) { |
---|
| 728 | expr->print( out ); |
---|
| 729 | } else { |
---|
| 730 | out << "nullptr"; |
---|
| 731 | } |
---|
[baf7fee] | 732 | return out; |
---|
| 733 | } |
---|
| 734 | |
---|
[0dd3a2f] | 735 | // Local Variables: // |
---|
| 736 | // tab-width: 4 // |
---|
| 737 | // mode: c++ // |
---|
| 738 | // compile-command: "make install" // |
---|
| 739 | // End: // |
---|