mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-22 17:02:37 +00:00
Change parse tree from `template (T:! type)` to `(template T):! type`, so that we have information about whether a binding is a template binding available when forming the representation of the binding pattern. This incidentally fixes a bug that we would accept `template addr A:! B` instead of the intended `addr template A:! B`. Track whether a symbolic binding is a template binding on the `EntityName` object. I'm borrowing a bit from the `CompileTimeBindIndex` for this in order to avoid making `EntityName`s larger. Longer-term, we should think about using a different representation for symbolic bindings, to avoid including these fields in all `EntityName`s, but that's out of scope for this change. So far, template bindings are treated as having the same phase as checked bindings, but that will change in a future PR.
102 lines
3.4 KiB
C++
102 lines
3.4 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#ifndef CARBON_TOOLCHAIN_SEM_IR_ENTITY_NAME_H_
|
|
#define CARBON_TOOLCHAIN_SEM_IR_ENTITY_NAME_H_
|
|
|
|
#include "common/hashing.h"
|
|
#include "common/set.h"
|
|
#include "toolchain/base/value_store.h"
|
|
#include "toolchain/sem_ir/ids.h"
|
|
|
|
namespace Carbon::SemIR {
|
|
|
|
struct EntityName : public Printable<EntityName> {
|
|
auto Print(llvm::raw_ostream& out) const -> void {
|
|
out << "{name: " << name_id << ", parent_scope: " << parent_scope_id
|
|
<< ", index: " << bind_index_value << ", is_template: " << is_template
|
|
<< "}";
|
|
}
|
|
|
|
friend auto CarbonHashtableEq(const EntityName& lhs, const EntityName& rhs)
|
|
-> bool {
|
|
return std::memcmp(&lhs, &rhs, sizeof(EntityName)) == 0;
|
|
}
|
|
|
|
// The index of the binding, if this is the name of a symbolic binding, or
|
|
// `None` otherwise. This is also `None` for a `.Self` symbolic binding,
|
|
// because such a binding is not assigned an index.
|
|
auto bind_index() const -> CompileTimeBindIndex {
|
|
return CompileTimeBindIndex(bind_index_value);
|
|
}
|
|
|
|
// The name.
|
|
NameId name_id;
|
|
// The parent scope.
|
|
NameScopeId parent_scope_id;
|
|
|
|
// TODO: The following two fields are only meaningful for a symbolic binding.
|
|
// Consider splitting them off into a separate type so that we don't store
|
|
// them for other kinds of `EntityName`.
|
|
|
|
// The bind_index() value, unwrapped so it can be stored in a bit-field.
|
|
int32_t bind_index_value : 31 = CompileTimeBindIndex::None.index;
|
|
// Whether this binding is a template parameter.
|
|
bool is_template : 1 = false;
|
|
};
|
|
|
|
// Hashing for EntityName. See common/hashing.h.
|
|
inline auto CarbonHashValue(const EntityName& value, uint64_t seed)
|
|
-> HashCode {
|
|
Hasher hasher(seed);
|
|
hasher.HashRaw(value);
|
|
return static_cast<HashCode>(hasher);
|
|
}
|
|
|
|
// Value store for EntityName. In addition to the regular ValueStore
|
|
// functionality, this can provide optional canonical IDs for EntityNames.
|
|
struct EntityNameStore : public ValueStore<EntityNameId> {
|
|
public:
|
|
// Adds an entity name for a symbolic binding.
|
|
auto AddSymbolicBindingName(NameId name_id, NameScopeId parent_scope_id,
|
|
CompileTimeBindIndex bind_index, bool is_template)
|
|
-> EntityNameId {
|
|
return Add({.name_id = name_id,
|
|
.parent_scope_id = parent_scope_id,
|
|
.bind_index_value = bind_index.index,
|
|
.is_template = is_template});
|
|
}
|
|
|
|
// Convert an ID to a canonical ID. All calls to this with equivalent
|
|
// `EntityName`s will return the same `EntityNameId`.
|
|
auto MakeCanonical(EntityNameId id) -> EntityNameId;
|
|
|
|
private:
|
|
class KeyContext;
|
|
|
|
Set<EntityNameId, /*SmallSize=*/0, KeyContext> canonical_ids_;
|
|
};
|
|
|
|
class EntityNameStore::KeyContext : public TranslatingKeyContext<KeyContext> {
|
|
public:
|
|
explicit KeyContext(const EntityNameStore* store) : store_(store) {}
|
|
|
|
// Note that it is safe to return a `const` reference here as the underlying
|
|
// object's lifetime is provided by the `store_`.
|
|
auto TranslateKey(EntityNameId id) const -> const EntityName& {
|
|
return store_->Get(id);
|
|
}
|
|
|
|
private:
|
|
const EntityNameStore* store_;
|
|
};
|
|
|
|
inline auto EntityNameStore::MakeCanonical(EntityNameId id) -> EntityNameId {
|
|
return canonical_ids_.Insert(id, KeyContext(this)).key();
|
|
}
|
|
|
|
} // namespace Carbon::SemIR
|
|
|
|
#endif // CARBON_TOOLCHAIN_SEM_IR_ENTITY_NAME_H_
|