Changes in src/AST/Expr.cpp [490fb92e:312029a]
- File:
-
- 1 edited
-
src/AST/Expr.cpp (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Expr.cpp
r490fb92e r312029a 20 20 #include <vector> 21 21 22 #include "Copy.hpp" // for shallowCopy23 #include "Eval.hpp" // for call24 22 #include "GenericSubstitution.hpp" 25 #include "LinkageSpec.hpp"26 23 #include "Stmt.hpp" 27 24 #include "Type.hpp" … … 30 27 #include "Common/SemanticError.h" 31 28 #include "GenPoly/Lvalue.h" // for referencesPermissable 32 #include "InitTweak/InitTweak.h" // for get Function, getPointerBase29 #include "InitTweak/InitTweak.h" // for getPointerBase 33 30 #include "ResolvExpr/typeops.h" // for extractResultType 34 31 #include "Tuples/Tuples.h" // for makeTupleType 35 32 36 33 namespace ast { 37 38 namespace {39 std::set<std::string> const lvalueFunctionNames = {"*?", "?[?]"};40 }41 42 // --- Expr43 bool Expr::get_lvalue() const {44 return false;45 }46 34 47 35 // --- ApplicationExpr … … 58 46 } 59 47 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 67 48 // --- UntypedExpr 68 49 69 UntypedExpr * UntypedExpr::createDeref( const CodeLocation & loc, constExpr * arg ) {50 UntypedExpr * UntypedExpr::createDeref( const CodeLocation & loc, Expr * arg ) { 70 51 assert( arg ); 71 52 72 UntypedExpr * ret = call( loc, "*?", arg ); 53 UntypedExpr * ret = new UntypedExpr{ 54 loc, new NameExpr{loc, "*?"}, std::vector<ptr<Expr>>{ ptr<Expr>{ arg } } 55 }; 73 56 if ( const Type * ty = arg->result ) { 74 57 const Type * base = InitTweak::getPointerBase( ty ); … … 82 65 // base type 83 66 ret->result = base; 67 add_qualifiers( ret->result, CV::Lvalue ); 84 68 } 85 69 } … … 87 71 } 88 72 89 bool UntypedExpr::get_lvalue() const { 90 std::string fname = InitTweak::getFunctionName( this ); 91 return lvalueFunctionNames.count( fname ); 92 } 93 94 UntypedExpr * UntypedExpr::createAssign( const CodeLocation & loc, const Expr * lhs, const Expr * rhs ) { 73 UntypedExpr * UntypedExpr::createAssign( const CodeLocation & loc, Expr * lhs, Expr * rhs ) { 95 74 assert( lhs && rhs ); 96 75 97 UntypedExpr * ret = call( loc, "?=?", lhs, rhs ); 76 UntypedExpr * ret = new UntypedExpr{ 77 loc, new NameExpr{loc, "?=?"}, std::vector<ptr<Expr>>{ ptr<Expr>{ lhs }, ptr<Expr>{ rhs } } 78 }; 98 79 if ( lhs->result && rhs->result ) { 99 80 // if both expressions are typed, assumes that this assignment is a C bitwise assignment, … … 127 108 AddressExpr::AddressExpr( const CodeLocation & loc, const Expr * a ) : Expr( loc ), arg( a ) { 128 109 if ( arg->result ) { 129 if ( arg-> get_lvalue() ) {110 if ( arg->result->is_lvalue() ) { 130 111 // lvalue, retains all levels of reference, and gains a pointer inside the references 131 112 Type * res = addrType( arg->result ); 113 res->set_lvalue( false ); // result of & is never an lvalue 132 114 result = res; 133 115 } else { … … 136 118 dynamic_cast< const ReferenceType * >( arg->result.get() ) ) { 137 119 Type * res = addrType( refType->base ); 120 res->set_lvalue( false ); // result of & is never an lvalue 138 121 result = res; 139 122 } else { … … 156 139 : Expr( loc, new VoidType{} ), arg( a ), isGenerated( g ) {} 157 140 158 bool CastExpr::get_lvalue() const {159 // This is actually wrong by C, but it works with our current set-up.160 return arg->get_lvalue();161 }162 163 141 // --- KeywordCastExpr 164 142 165 143 const char * KeywordCastExpr::targetString() const { 166 144 return AggregateDecl::aggrString( target ); 167 }168 169 // --- UntypedMemberExpr170 171 bool UntypedMemberExpr::get_lvalue() const {172 return aggregate->get_lvalue();173 145 } 174 146 … … 181 153 assert( aggregate->result ); 182 154 183 // Deep copy on result type avoids mutation on transitively multiply referenced object. 184 // 185 // Example, adapted from parts of builtins and bootloader: 186 // 187 // forall(dtype T) 188 // struct __Destructor { 189 // T * object; 190 // void (*dtor)(T *); 191 // }; 192 // 193 // forall(dtype S) 194 // void foo(__Destructor(S) &d) { 195 // if (d.dtor) { // here 196 // } 197 // } 198 // 199 // Let e be the "d.dtor" guard espression, which is MemberExpr after resolve. Let d be the 200 // declaration of member __Destructor.dtor (an ObjectDecl), as accessed via the top-level 201 // declaration of __Destructor. Consider the types e.result and d.type. In the old AST, one 202 // is a clone of the other. Ordinary new-AST use would set them up as a multiply-referenced 203 // object. 204 // 205 // e.result: PointerType 206 // .base: FunctionType 207 // .params.front(): ObjectDecl, the anonymous parameter of type T* 208 // .type: PointerType 209 // .base: TypeInstType 210 // let x = that 211 // let y = similar, except start from d.type 212 // 213 // Consider two code lines down, genericSubstitution(...).apply(result). 214 // 215 // Applying this chosen-candidate's type substitution means modifying x, substituting 216 // S for T. This mutation should affect x and not y. 217 218 result = deepCopy(mem->get_type()); 219 155 // take ownership of member type 156 result = mem->get_type(); 220 157 // substitute aggregate generic parameters into member type 221 158 genericSubstitution( aggregate->result ).apply( result ); 222 // ensure appropriate restrictions from aggregate type 223 add_qualifiers( result, aggregate->result->qualifiers ); 224 } 225 226 MemberExpr::MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg, 227 MemberExpr::NoOpConstruction overloadSelector ) 228 : Expr( loc ), member( mem ), aggregate( agg ) { 229 assert( member ); 230 assert( aggregate ); 231 assert( aggregate->result ); 232 (void) overloadSelector; 233 } 234 235 bool MemberExpr::get_lvalue() const { 236 // This is actually wrong by C, but it works with our current set-up. 237 return true; 159 // ensure lvalue and appropriate restrictions from aggregate type 160 add_qualifiers( result, aggregate->result->qualifiers | CV::Lvalue ); 238 161 } 239 162 … … 247 170 assert( var ); 248 171 assert( var->get_type() ); 249 result = shallowCopy( var->get_type() ); 250 } 251 252 bool VariableExpr::get_lvalue() const { 253 // It isn't always an lvalue, but it is never an rvalue. 254 return true; 172 result = var->get_type(); 173 add_qualifiers( result, CV::Lvalue ); 255 174 } 256 175 … … 338 257 const CodeLocation & loc, const Expr * a1, const Expr * a2, LogicalFlag ia ) 339 258 : Expr( loc, new BasicType{ BasicType::SignedInt } ), arg1( a1 ), arg2( a2 ), isAnd( ia ) {} 340 341 // --- CommaExpr342 bool CommaExpr::get_lvalue() const {343 // This is wrong by C, but the current implementation uses it.344 // (ex: Specialize, Lvalue and Box)345 return arg2->get_lvalue();346 }347 259 348 260 // --- ConstructorExpr … … 364 276 assert( t && i ); 365 277 result = t; 366 } 367 368 bool CompoundLiteralExpr::get_lvalue() const { 369 return true; 278 add_qualifiers( result, CV::Lvalue ); 370 279 } 371 280 … … 384 293 // like MemberExpr, TupleIndexExpr is always an lvalue 385 294 result = type->types[ index ]; 386 } 387 388 bool TupleIndexExpr::get_lvalue() const { 389 return tuple->get_lvalue(); 295 add_qualifiers( result, CV::Lvalue ); 390 296 } 391 297
Note:
See TracChangeset
for help on using the changeset viewer.