Files
carbon-lang/toolchain/check/literal.h
T
Richard Smith f9ab963bd6 Add a type_literal instruction to represent syntactic type literals. (#6781)
This allows us to capture the location at which a type literal was used,
even in the cases where we don't otherwise need to create a new
instruction to represent the type such as for `char` or `str`.

The logic used to build the underlying type is now marked as desugaring.
For cases such as `iN`, this causes the call to `Core.Int` to no longer
be added as a dedicated IR instruction, and instead its constant value
is used directly as the value of the `type_literal`. This results in
this being on balance a reduction in the size of the IR.

This also fixes a crash in C++ interop when using a `char` literal as a
template argument. The crash was caused by the template argument not
having an associated location when mapping to a C++ location. See
changes to check/testdata/interop/cpp/template/type_param.carbon for an
example that used to crash before this change.

Update alias handling to allow an alias to point at any type literal,
reinstating support for aliases for type literals such as `bool` and
`i32` that had previously worked but stopped working when we
transitioned those types to being defined in the prelude. See changes to
toolchain/check/testdata/alias/builtins.carbon.

All the test changes other than the two mentioned above are mechanical
autoupdate changes switching to the new instruction.
2026-02-26 20:10:50 +00:00

64 lines
2.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_CHECK_LITERAL_H_
#define CARBON_TOOLCHAIN_CHECK_LITERAL_H_
#include "toolchain/base/value_ids.h"
#include "toolchain/check/context.h"
#include "toolchain/check/convert.h"
#include "toolchain/lex/token_info.h"
#include "toolchain/sem_ir/ids.h"
namespace Carbon::Check {
// Forms a TypeType for a `type` literal.
auto MakeTypeTypeLiteral(Context& context, Parse::NodeId node_id)
-> SemIR::TypeInstId;
// Forms a boolean type for a `bool` literal.
auto MakeBoolTypeLiteral(Context& context, Parse::NodeId node_id)
-> SemIR::TypeInstId;
// Forms a BoolLiteral instruction with the given value and returns it.
auto MakeBoolLiteral(Context& context, SemIR::LocId loc_id,
SemIR::BoolValue value) -> SemIR::InstId;
// Forms an IntValue instruction with type `IntLiteral` for a given literal
// integer value, which is assumed to be unsigned.
auto MakeIntLiteral(Context& context, Parse::NodeId node_id, IntId int_id)
-> SemIR::InstId;
// Forms a char type expression for `char` literal.
auto MakeCharTypeLiteral(Context& context, Parse::NodeId node_id)
-> SemIR::TypeInstId;
// Forms an integer type expression for either an `iN` or `uN` literal.
auto MakeIntTypeLiteral(Context& context, Parse::NodeId node_id,
SemIR::IntKind int_kind, IntId size_id)
-> SemIR::TypeInstId;
// Forms an integer type of the specified kind and bit-width.
auto MakeIntType(Context& context, Parse::NodeId node_id,
SemIR::IntKind int_kind, IntId size_id) -> SemIR::TypeId;
// Forms a floating point type expression for `fN` literal.
auto MakeFloatTypeLiteral(Context& context, Parse::NodeId node_id,
IntId size_id) -> SemIR::TypeInstId;
// Forms a string literal value instruction for a given string literal.
auto MakeStringLiteral(Context& context, Parse::StringLiteralId node_id,
StringLiteralValueId value_id) -> SemIR::InstId;
// Forms a string literal type expression for a `str` literal.
auto MakeStringTypeLiteral(Context& context, Parse::StringTypeLiteralId node_id)
-> SemIR::TypeInstId;
// Forms a string type.
auto MakeStringType(Context& context, SemIR::LocId loc_id) -> TypeExpr;
} // namespace Carbon::Check
#endif // CARBON_TOOLCHAIN_CHECK_LITERAL_H_