Files
carbon-lang/toolchain/sem_ir/name.h
T
Chandler Carruth 4148161e24 Refactor value store code to use separate files. (#4477)
This is in anticipation of making the integer value store be customized
heavily. I'd like to extract it from the common code when doing that, so
first disentangling them here without any intended change in
functionality or behavior to enable that.

I've tried to update `#include`s to be as minimal as I can and added a
few missing includes spotted in the process.

I've split the test for value store to include what was easy focused on
just the value store templates rather than the unified shared value
stores.

This might surface some opportunities for adding more tests, but for
this PR, just doing the minimal restructuring.
2024-11-04 04:00:17 +00:00

60 lines
2.2 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_NAME_H_
#define CARBON_TOOLCHAIN_SEM_IR_NAME_H_
#include "toolchain/base/value_ids.h"
#include "toolchain/base/value_store.h"
#include "toolchain/sem_ir/ids.h"
namespace Carbon::SemIR {
// Provides a ValueStore-like interface for names.
//
// A name is either an identifier name or a special name such as `self` that
// does not correspond to an identifier token. Identifier names are represented
// as `NameId`s with the same non-negative index as the `IdentifierId` of the
// identifier. Special names are represented as `NameId`s with a negative index.
//
// `SemIR::NameId` values should be obtained by using `NameId::ForIdentifier`
// or the named constants such as `NameId::SelfValue`.
//
// As we do not require any additional explicit storage for names, this is
// currently a wrapper around an identifier store that has no state of its own.
class NameStoreWrapper {
public:
explicit NameStoreWrapper(
const CanonicalValueStore<IdentifierId>* identifiers)
: identifiers_(identifiers) {}
// Returns the requested name as a string, if it is an identifier name. This
// returns std::nullopt for special names.
auto GetAsStringIfIdentifier(NameId name_id) const
-> std::optional<llvm::StringRef> {
if (auto identifier_id = name_id.AsIdentifierId();
identifier_id.is_valid()) {
return identifiers_->Get(identifier_id);
}
return std::nullopt;
}
// Returns the requested name as a string for formatted output. This returns
// `"r#name"` if `name` is a keyword.
auto GetFormatted(NameId name_id) const -> llvm::StringRef;
// Returns a best-effort name to use as the basis for SemIR and LLVM IR names.
// This is always identifier-shaped, but may be ambiguous, for example if
// there is both a `self` and an `r#self` in the same scope. Returns "" for an
// invalid name.
auto GetIRBaseName(NameId name_id) const -> llvm::StringRef;
private:
const CanonicalValueStore<IdentifierId>* identifiers_;
};
} // namespace Carbon::SemIR
#endif // CARBON_TOOLCHAIN_SEM_IR_NAME_H_