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