mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-24 08:22:39 +00:00
Handle references to global lets and enclosing locals. (#8061)
When building an expression referencing the result of name lookup, if the lookup result is non-constant then check we can access it from the current scope. * If it is a global, then use a new `GlobalRef` instruction to get the value of that global. In lowering, create global variables to hold the values of global `let` bindings so that `GlobalRef` can access those values. * If it is a local, walk the lexical scopes between it and the point of use and ensure it is usable through from all of them. Error if there's an intervening declarative scope such as a class, add a TODO to handle captures for intervening functions, and allow `typeof(...)` to name things it can't read the value of since it's unevaluated. Global `let`s (which includes `let`s at class scope) now also have their initializers collected into the global inits block. If the initializer is non-constant, the wrapper binding also lives in the global inits block; otherwise it continues to live in the file block and the variable emitted for it is a global constant. Assisted-by: Gemini and Claude via Antigravity
This commit is contained in:
1 parent
bb6486b553
commit
d50576ca16
94 files changed
+3394
-1293
No files matched your search
@@ -43,9 +43,12 @@ class FullPatternStack {
|
||||
// A name-binding declaration, such as a `let` or `var` statement.
|
||||
NameBindingDecl,
|
||||
|
||||
// A `var` field declaration inside a class.
|
||||
// A `var` declaration of a field or static var inside a class.
|
||||
ClassScopeVarDecl,
|
||||
|
||||
// A `let` declaration inside a class.
|
||||
ClassScopeLetDecl,
|
||||
|
||||
// The implicit parameter list of a function or impl declaration.
|
||||
ImplicitParamList,
|
||||
|
||||
@@ -63,14 +66,20 @@ class FullPatternStack {
|
||||
NotInEitherParamList
|
||||
};
|
||||
|
||||
// Returns whether `kind` is the kind of a name binding declaration.
|
||||
static auto IsAnyNameBindingDeclKind(Kind kind) -> bool {
|
||||
return kind == Kind::NameBindingDecl || kind == Kind::ClassScopeVarDecl ||
|
||||
kind == Kind::ClassScopeLetDecl;
|
||||
}
|
||||
|
||||
auto empty() const -> bool { return kind_stack_.empty(); }
|
||||
|
||||
// The kind of the current full-pattern.
|
||||
auto CurrentKind() const -> Kind { return kind_stack_.back(); }
|
||||
|
||||
// Whether the kind of the current full-pattern is a class `var` decl.
|
||||
auto IsCurrentKindClassScopeVarDecl() -> bool {
|
||||
return !empty() && CurrentKind() == Kind::ClassScopeVarDecl;
|
||||
// Returns whether the kind of the current full-pattern is `kind`.
|
||||
auto CurrentKindIs(Kind kind) -> bool {
|
||||
return !empty() && CurrentKind() == kind;
|
||||
}
|
||||
|
||||
// Marks the start of a new full-pattern for a parameterized entity
|
||||
@@ -84,8 +93,8 @@ class FullPatternStack {
|
||||
}
|
||||
|
||||
// Marks the start of a new full-pattern for a name binding declaration.
|
||||
auto PushNameBindingDecl() -> void {
|
||||
kind_stack_.push_back(Kind::NameBindingDecl);
|
||||
auto PushNameBindingDecl(Kind kind = Kind::NameBindingDecl) -> void {
|
||||
kind_stack_.push_back(kind);
|
||||
bind_name_stack_.PushArray();
|
||||
var_pattern_stack_.PushArray();
|
||||
next_var_index_stack_.push_back(-1);
|
||||
@@ -93,10 +102,12 @@ class FullPatternStack {
|
||||
|
||||
// Marks the start of a new full-pattern for a class `var` declaration.
|
||||
auto PushClassScopeVarDecl() -> void {
|
||||
kind_stack_.push_back(Kind::ClassScopeVarDecl);
|
||||
bind_name_stack_.PushArray();
|
||||
var_pattern_stack_.PushArray();
|
||||
next_var_index_stack_.push_back(-1);
|
||||
PushNameBindingDecl(Kind::ClassScopeVarDecl);
|
||||
}
|
||||
|
||||
// Marks the start of a new full-pattern for a class `let` declaration.
|
||||
auto PushClassScopeLetDecl() -> void {
|
||||
PushNameBindingDecl(Kind::ClassScopeLetDecl);
|
||||
}
|
||||
|
||||
// Marks the start of the current parameterized entity's implicit parameter
|
||||
@@ -170,8 +181,7 @@ class FullPatternStack {
|
||||
// needs to be emitted early; otherwise the `VarStorage` should be emitted on
|
||||
// demand during pattern matching.
|
||||
auto AddLocalVarPattern(VarInfo var_info) -> void {
|
||||
CARBON_CHECK(kind_stack_.back() == Kind::ClassScopeVarDecl ||
|
||||
kind_stack_.back() == Kind::NameBindingDecl);
|
||||
CARBON_CHECK(IsAnyNameBindingDeclKind(kind_stack_.back()));
|
||||
CARBON_CHECK(next_var_index_stack_.back() < 0);
|
||||
var_pattern_stack_.AppendToTop(var_info);
|
||||
}
|
||||
@@ -183,8 +193,7 @@ class FullPatternStack {
|
||||
// exactly once for each inst pair passed to `AddLocalVarPattern`, and in the
|
||||
// same order.
|
||||
auto GetLocalVarStorage(SemIR::InstId var_pattern_id) -> SemIR::InstId {
|
||||
CARBON_CHECK(kind_stack_.back() == Kind::ClassScopeVarDecl ||
|
||||
kind_stack_.back() == Kind::NameBindingDecl);
|
||||
CARBON_CHECK(IsAnyNameBindingDeclKind(kind_stack_.back()));
|
||||
auto& index = next_var_index_stack_.back();
|
||||
CARBON_CHECK(index >= 0);
|
||||
auto var_info = var_pattern_stack_.PeekArray()[index];
|
||||
|
||||
Reference in new issue
Block a user