Changes in src/AST/Expr.cpp [c36298d:cf32116]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Expr.cpp
rc36298d rcf32116 10 10 // Created On : Wed May 15 17:00:00 2019 11 11 // Last Modified By : Andrew Beach 12 // Created On : Thr Jun 13 13:38:00 201913 // Update Count : 212 // Created On : Thr Jun 26 12:12:00 2019 13 // Update Count : 3 14 14 // 15 15 … … 20 20 #include <vector> 21 21 22 #include "Copy.hpp" // for shallowCopy 23 #include "Eval.hpp" // for call 22 24 #include "GenericSubstitution.hpp" 25 #include "LinkageSpec.hpp" 23 26 #include "Stmt.hpp" 24 27 #include "Type.hpp" … … 27 30 #include "Common/SemanticError.h" 28 31 #include "GenPoly/Lvalue.h" // for referencesPermissable 29 #include "InitTweak/InitTweak.h" // for get PointerBase32 #include "InitTweak/InitTweak.h" // for getFunction, getPointerBase 30 33 #include "ResolvExpr/typeops.h" // for extractResultType 31 34 #include "Tuples/Tuples.h" // for makeTupleType 32 35 33 36 namespace ast { 37 38 namespace { 39 std::set<std::string> const lvalueFunctionNames = {"*?", "?[?]"}; 40 } 41 42 // --- Expr 43 bool Expr::get_lvalue() const { 44 return false; 45 } 34 46 35 47 // --- ApplicationExpr … … 46 58 } 47 59 60 bool ApplicationExpr::get_lvalue() const { 61 if ( const DeclWithType * func = InitTweak::getFunction( this ) ) { 62 return func->linkage == Linkage::Intrinsic && lvalueFunctionNames.count( func->name ); 63 } 64 return false; 65 } 66 48 67 // --- UntypedExpr 49 68 … … 51 70 assert( arg ); 52 71 53 UntypedExpr * ret = new UntypedExpr{ 54 loc, new NameExpr{loc, "*?"}, std::vector<ptr<Expr>>{ ptr<Expr>{ arg } } 55 }; 72 UntypedExpr * ret = call( loc, "*?", arg ); 56 73 if ( const Type * ty = arg->result ) { 57 74 const Type * base = InitTweak::getPointerBase( ty ); … … 71 88 } 72 89 90 bool UntypedExpr::get_lvalue() const { 91 std::string fname = InitTweak::getFunctionName( this ); 92 return lvalueFunctionNames.count( fname ); 93 } 94 73 95 UntypedExpr * UntypedExpr::createAssign( const CodeLocation & loc, Expr * lhs, Expr * rhs ) { 74 96 assert( lhs && rhs ); 75 97 76 UntypedExpr * ret = new UntypedExpr{ 77 loc, new NameExpr{loc, "?=?"}, std::vector<ptr<Expr>>{ ptr<Expr>{ lhs }, ptr<Expr>{ rhs } } 78 }; 98 UntypedExpr * ret = call( loc, "?=?", lhs, rhs ); 79 99 if ( lhs->result && rhs->result ) { 80 100 // if both expressions are typed, assumes that this assignment is a C bitwise assignment, … … 108 128 AddressExpr::AddressExpr( const CodeLocation & loc, const Expr * a ) : Expr( loc ), arg( a ) { 109 129 if ( arg->result ) { 110 if ( arg-> result->is_lvalue() ) {130 if ( arg->get_lvalue() ) { 111 131 // lvalue, retains all levels of reference, and gains a pointer inside the references 112 132 Type * res = addrType( arg->result ); … … 139 159 : Expr( loc, new VoidType{} ), arg( a ), isGenerated( g ) {} 140 160 161 bool CastExpr::get_lvalue() const { 162 // This is actually wrong by C, but it works with our current set-up. 163 return arg->get_lvalue(); 164 } 165 141 166 // --- KeywordCastExpr 142 167 … … 152 177 } 153 178 179 // --- UntypedMemberExpr 180 181 bool UntypedMemberExpr::get_lvalue() const { 182 return aggregate->get_lvalue(); 183 } 184 154 185 // --- MemberExpr 155 186 … … 160 191 assert( aggregate->result ); 161 192 162 // take ownership of member type 163 result = mem->get_type(); 193 // Deep copy on result type avoids mutation on transitively multiply referenced object. 194 // 195 // Example, adapted from parts of builtins and bootloader: 196 // 197 // forall(dtype T) 198 // struct __Destructor { 199 // T * object; 200 // void (*dtor)(T *); 201 // }; 202 // 203 // forall(dtype S) 204 // void foo(__Destructor(S) &d) { 205 // if (d.dtor) { // here 206 // } 207 // } 208 // 209 // Let e be the "d.dtor" guard espression, which is MemberExpr after resolve. Let d be the 210 // declaration of member __Destructor.dtor (an ObjectDecl), as accessed via the top-level 211 // declaration of __Destructor. Consider the types e.result and d.type. In the old AST, one 212 // is a clone of the other. Ordinary new-AST use would set them up as a multiply-referenced 213 // object. 214 // 215 // e.result: PointerType 216 // .base: FunctionType 217 // .params.front(): ObjectDecl, the anonymous parameter of type T* 218 // .type: PointerType 219 // .base: TypeInstType 220 // let x = that 221 // let y = similar, except start from d.type 222 // 223 // Consider two code lines down, genericSubstitution(...).apply(result). 224 // 225 // Applying this chosen-candidate's type substitution means modifying x, substituting 226 // S for T. This mutation should affect x and not y. 227 228 result = deepCopy(mem->get_type()); 229 164 230 // substitute aggregate generic parameters into member type 165 231 genericSubstitution( aggregate->result ).apply( result ); … … 168 234 } 169 235 236 MemberExpr::MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg, 237 MemberExpr::NoOpConstruction overloadSelector ) 238 : Expr( loc ), member( mem ), aggregate( agg ) { 239 assert( member ); 240 assert( aggregate ); 241 assert( aggregate->result ); 242 (void) overloadSelector; 243 } 244 245 bool MemberExpr::get_lvalue() const { 246 // This is actually wrong by C, but it works with our current set-up. 247 return true; 248 } 249 170 250 // --- VariableExpr 171 251 … … 177 257 assert( var ); 178 258 assert( var->get_type() ); 179 result = var->get_type(); 180 add_qualifiers( result, CV::Lvalue ); 259 auto r = shallowCopy( var->get_type() ); 260 r->qualifiers |= CV::Lvalue; 261 result = r; 262 } 263 264 bool VariableExpr::get_lvalue() const { 265 // It isn't always an lvalue, but it is never an rvalue. 266 return true; 181 267 } 182 268 … … 265 351 : Expr( loc, new BasicType{ BasicType::SignedInt } ), arg1( a1 ), arg2( a2 ), isAnd( ia ) {} 266 352 353 // --- CommaExpr 354 bool CommaExpr::get_lvalue() const { 355 // This is wrong by C, but the current implementation uses it. 356 // (ex: Specialize, Lvalue and Box) 357 return arg2->get_lvalue(); 358 } 359 267 360 // --- ConstructorExpr 268 361 … … 286 379 } 287 380 381 bool CompoundLiteralExpr::get_lvalue() const { 382 return true; 383 } 384 288 385 // --- TupleExpr 289 386 … … 301 398 result = type->types[ index ]; 302 399 add_qualifiers( result, CV::Lvalue ); 400 } 401 402 bool TupleIndexExpr::get_lvalue() const { 403 return tuple->get_lvalue(); 303 404 } 304 405
Note: See TracChangeset
for help on using the changeset viewer.