diff --git a/checker/internal/type_check_env.cc b/checker/internal/type_check_env.cc index d856a7230..e76621435 100644 --- a/checker/internal/type_check_env.cc +++ b/checker/internal/type_check_env.cc @@ -34,25 +34,18 @@ namespace cel::checker_internal { const VariableDecl* absl_nullable TypeCheckEnv::LookupVariable( absl::string_view name) const { - const TypeCheckEnv* scope = this; - while (scope != nullptr) { - if (auto it = scope->variables_.find(name); it != scope->variables_.end()) { - return &it->second; - } - scope = scope->parent_; + if (auto it = variables_.find(name); it != variables_.end()) { + return &it->second; } return nullptr; } const FunctionDecl* absl_nullable TypeCheckEnv::LookupFunction( absl::string_view name) const { - const TypeCheckEnv* scope = this; - while (scope != nullptr) { - if (auto it = scope->functions_.find(name); it != scope->functions_.end()) { - return &it->second; - } - scope = scope->parent_; + if (auto it = functions_.find(name); it != functions_.end()) { + return &it->second; } + return nullptr; } @@ -71,17 +64,13 @@ absl::StatusOr> TypeCheckEnv::LookupTypeName( return Type::Enum(enum_descriptor); } } - const TypeCheckEnv* scope = this; - do { - for (auto iter = type_providers_.rbegin(); iter != type_providers_.rend(); - ++iter) { - auto type = (*iter)->FindType(name); - if (!type.ok() || type->has_value()) { - return type; - } + for (auto iter = type_providers_.rbegin(); iter != type_providers_.rend(); + ++iter) { + auto type = (*iter)->FindType(name); + if (!type.ok() || type->has_value()) { + return type; } - scope = scope->parent_; - } while ((scope != nullptr)); + } return absl::nullopt; } @@ -106,26 +95,21 @@ absl::StatusOr> TypeCheckEnv::LookupEnumConstant( return decl; } } - const TypeCheckEnv* scope = this; - do { - for (auto iter = type_providers_.rbegin(); iter != type_providers_.rend(); - ++iter) { - auto enum_constant = (*iter)->FindEnumConstant(type, value); - if (!enum_constant.ok()) { - return enum_constant.status(); - } - if (enum_constant->has_value()) { - auto decl = - MakeVariableDecl(absl::StrCat((**enum_constant).type_full_name, ".", - (**enum_constant).value_name), - (**enum_constant).type); - decl.set_value( - Constant(static_cast((**enum_constant).number))); - return decl; - } + for (auto iter = type_providers_.rbegin(); iter != type_providers_.rend(); + ++iter) { + auto enum_constant = (*iter)->FindEnumConstant(type, value); + if (!enum_constant.ok()) { + return enum_constant.status(); } - scope = scope->parent_; - } while (scope != nullptr); + if (enum_constant->has_value()) { + auto decl = + MakeVariableDecl(absl::StrCat((**enum_constant).type_full_name, ".", + (**enum_constant).value_name), + (**enum_constant).type); + decl.set_value(Constant(static_cast((**enum_constant).number))); + return decl; + } + } return absl::nullopt; } @@ -165,22 +149,17 @@ absl::StatusOr> TypeCheckEnv::LookupStructField( return cel::MessageTypeField(field_descriptor); } } - const TypeCheckEnv* scope = this; - do { - // Check the type providers in reverse registration order. - // Note: this doesn't allow for shadowing a type with a subset type of the - // same name -- the parent type provider will still be considered when - // checking field accesses. - for (auto iter = type_providers_.rbegin(); iter != type_providers_.rend(); - ++iter) { - auto field_info = - (*iter)->FindStructTypeFieldByName(type_name, field_name); - if (!field_info.ok() || field_info->has_value()) { - return field_info; - } + // Check the type providers in reverse registration order. + // Note: this doesn't allow for shadowing a type with a subset type of the + // same name -- the prior type provider will still be considered when + // checking field accesses. + for (auto iter = type_providers_.rbegin(); iter != type_providers_.rend(); + ++iter) { + auto field_info = (*iter)->FindStructTypeFieldByName(type_name, field_name); + if (!field_info.ok() || field_info->has_value()) { + return field_info; } - scope = scope->parent_; - } while (scope != nullptr); + } return absl::nullopt; } diff --git a/checker/internal/type_check_env.h b/checker/internal/type_check_env.h index a4d242fdf..5c8b3629c 100644 --- a/checker/internal/type_check_env.h +++ b/checker/internal/type_check_env.h @@ -89,17 +89,14 @@ class TypeCheckEnv { explicit TypeCheckEnv( absl_nonnull std::shared_ptr descriptor_pool) - : descriptor_pool_(std::move(descriptor_pool)), - container_(""), - parent_(nullptr) {} + : descriptor_pool_(std::move(descriptor_pool)), container_("") {} TypeCheckEnv(absl_nonnull std::shared_ptr descriptor_pool, std::shared_ptr arena) : descriptor_pool_(std::move(descriptor_pool)), arena_(std::move(arena)), - container_(""), - parent_(nullptr) {} + container_("") {} // Move-only. TypeCheckEnv(TypeCheckEnv&&) = default; @@ -163,9 +160,6 @@ class TypeCheckEnv { functions_[decl.name()] = std::move(decl); } - const TypeCheckEnv* absl_nullable parent() const { return parent_; } - void set_parent(TypeCheckEnv* parent) { parent_ = parent; } - // Returns the declaration for the given name if it is found in the current // or any parent scope. // Note: the returned declaration ptr is only valid as long as no changes are @@ -184,10 +178,6 @@ class TypeCheckEnv { absl::StatusOr> LookupTypeConstant( google::protobuf::Arena* absl_nonnull arena, absl::string_view type_name) const; - TypeCheckEnv MakeExtendedEnvironment() const ABSL_ATTRIBUTE_LIFETIME_BOUND { - return TypeCheckEnv(this); - } - const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool() const { return descriptor_pool_.get(); } @@ -200,11 +190,6 @@ class TypeCheckEnv { } private: - explicit TypeCheckEnv(const TypeCheckEnv* absl_nonnull parent) - : descriptor_pool_(parent->descriptor_pool_), - container_(parent != nullptr ? parent->container() : ""), - parent_(parent) {} - absl::StatusOr> LookupEnumConstant( absl::string_view type, absl::string_view value) const; @@ -212,7 +197,6 @@ class TypeCheckEnv { // If set, an arena was needed to allocate types in the environment. absl_nullable std::shared_ptr arena_; std::string container_; - const TypeCheckEnv* absl_nullable parent_; // Maps fully qualified names to declarations. absl::flat_hash_map variables_;