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