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