@@ -161,7 +161,7 @@ EnvFrame* Context::PushLambdaFormals(Value Formals,
161161 }
162162 }
163163
164- return PushEnvFrame (Ids);
164+ return PushEnvFrame (Ids, /* IsLambdaScope= */ true );
165165}
166166
167167std::pair<uintptr_t , uintptr_t > Context::GetIdentifierUniqueId (Value V) {
@@ -175,40 +175,38 @@ std::pair<uintptr_t, uintptr_t> Context::GetIdentifierUniqueId(Value V) {
175175 Env.getOpaqueValue ()};
176176}
177177
178+ // Get the localiest EnvFrame.
179+ EnvFrame* Context::GetLocalEnvFrame (Value Stack, bool IsLambdaScope) {
180+ EnvFrame* MostLocalEF = nullptr ;
181+ for (Value EnvNode : Stack) {
182+ if (auto * EF = dyn_cast<EnvFrame>(EnvNode)) {
183+ if (auto * NestedEF = GetLocalEnvFrame (EF->getLocalStack (),
184+ IsLambdaScope))
185+ return NestedEF;
186+ if (!IsLambdaScope || EF->isLambdaScope ())
187+ MostLocalEF = EF;
188+ }
189+ }
190+ return MostLocalEF;
191+ }
192+
178193// Get the localiest EnvFrame->LocalStack or this->EnvStack.
179194Value& Context::GetLocalEnvStack (Value Stack) {
180195 if (!Stack)
181196 Stack = this ->EnvStack ;
182- EnvFrame* CurEF = nullptr ;
183- if (auto * EnvPair = dyn_cast<Pair>(Stack)) {
184- if (auto * EF = dyn_cast<EnvFrame>(EnvPair->Car )) {
185- Stack = EF->LocalStack ;
186- CurEF = EF;
187- }
188- }
189- return CurEF ? CurEF->LocalStack : this ->EnvStack ;
197+ EnvFrame* MostLocalEF = GetLocalEnvFrame (Stack);
198+ return MostLocalEF ? MostLocalEF->LocalStack : this ->EnvStack ;
190199}
191200
192- EnvFrame* Context::PushEnvFrame (llvm::ArrayRef<Value> Ids) {
193- EnvFrame* E = CreateEnvFrame (Ids);
201+ EnvFrame* Context::PushEnvFrame (llvm::ArrayRef<Value> Ids,
202+ bool IsLambdaScope) {
203+ EnvFrame* E = CreateEnvFrame (Ids, IsLambdaScope);
194204 PushEnv (E, this ->EnvStack );
195-
196- // Push to any syntax closures not in the current EnvStack.
197- for (Value Id : Ids) {
198- if (auto * SC = dyn_cast<SyntaxClosure>(Id)) {
199- if (this ->EnvStack != SC->Env )
200- PushEnv (E, SC->Env );
201- }
202- }
203-
204205 return E;
205206}
206207
207208void Context::PushLocalBinding (Binding* B) {
208209 PushEnv (B, this ->EnvStack );
209- if (auto * SC = dyn_cast<SyntaxClosure>(B->getIdentifier ()))
210- if (this ->EnvStack != SC->Env )
211- PushEnv (B, SC->Env );
212210}
213211
214212void Context::PushEnv (Value V, Value Env) {
@@ -480,15 +478,18 @@ class Writer : public ValueVisitor<Writer> {
480478 }
481479
482480 void VisitEnvFrame (EnvFrame* E) {
483- OS << " #<EnvFrame {Bindings: {" ;
481+ OS << " #<EnvFrame {" ;
482+ if (E->isLambdaScope ())
483+ OS << " IsLambda, " ;
484+ OS << " Bindings: {" ;
484485 // Print the name of each binding.
485486 llvm::interleaveComma (E->getBindings (), OS,
486487 [&](Value B) {
487488 VisitBinding (cast<Binding>(B));
488489 });
489490 OS << " } LocalStack: {" ;
490491 Visit (E->getLocalStack ());
491- OS << " }>" ;
492+ OS << " }} >" ;
492493
493494 }
494495
@@ -1636,14 +1637,14 @@ ByteVector* Context::CreateByteVector(ArrayRef<Value> Xs) {
16361637 return BV;
16371638}
16381639
1639- EnvFrame* Context::CreateEnvFrame (unsigned N) {
1640+ EnvFrame* Context::CreateEnvFrame (unsigned N, bool IsLambdaScope ) {
16401641 unsigned MemSize = EnvFrame::sizeToAlloc (N);
16411642 void * Mem = Allocate (MemSize, alignof (EnvFrame));
1642- return new (Mem) EnvFrame (N);
1643+ return new (Mem) EnvFrame (N, IsLambdaScope );
16431644}
16441645
1645- EnvFrame* Context::CreateEnvFrame (llvm::ArrayRef<Value> Ids) {
1646- EnvFrame* E = CreateEnvFrame (Ids.size ());
1646+ EnvFrame* Context::CreateEnvFrame (llvm::ArrayRef<Value> Ids, bool IsLambdaScope ) {
1647+ EnvFrame* E = CreateEnvFrame (Ids.size (), IsLambdaScope );
16471648 auto Bindings = E->getBindings ();
16481649 for (unsigned i = 0 ; i < Bindings.size (); i++)
16491650 Bindings[i] = CreateBinding (Ids[i], CreateUndefined ());
0 commit comments