Files
carbon-lang/toolchain/check/handle_literal.cpp
T
Jon Ross-PerkinsandRichard Smith f197219c10 Split parse nodes out from instructions because they're rarely used. (#3590)
The parse nodes are still tracked as part of the same value store
interface in order to ensure parity, but they're split out from Inst
itself in order to reduce the size of Inst -- the expectation is that
they don't need to be passed around quite as much.

This change doesn't actually reduce the passing very much, although
there are hints of it: AddInstAndPush doesn't typically need a separate
parse node from the one on the Inst itself, for example. In a couple
spots I changed code to rely a little more on the InstId until the
ParseNode is needed, but it's very low hanging fruit where done. I think
convert could do more to not eagerly fetch the parse node before its
use, but more cleanup felt it would be easier to handle separately. I'm
currently viewing this as making such cleanup _possible_ rather than
executing on it up-front.

But also, I want to make sure there's a consensus to head in this
direction before pulling the trigger. We speculated that this would
result in the parse node being passed around less, and I do think that's
the case, although it's a bit fuzzy in the change.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2024-01-12 19:01:51 +00:00

104 lines
3.6 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
#include "toolchain/check/context.h"
namespace Carbon::Check {
auto HandleBoolLiteralFalse(Context& context,
Parse::BoolLiteralFalseId parse_node) -> bool {
context.AddInstAndPush(
{parse_node,
SemIR::BoolLiteral{context.GetBuiltinType(SemIR::BuiltinKind::BoolType),
SemIR::BoolValue::False}});
return true;
}
auto HandleBoolLiteralTrue(Context& context,
Parse::BoolLiteralTrueId parse_node) -> bool {
context.AddInstAndPush(
{parse_node,
SemIR::BoolLiteral{context.GetBuiltinType(SemIR::BuiltinKind::BoolType),
SemIR::BoolValue::True}});
return true;
}
auto HandleIntLiteral(Context& context, Parse::IntLiteralId parse_node)
-> bool {
context.AddInstAndPush(
{parse_node,
SemIR::IntLiteral{context.GetBuiltinType(SemIR::BuiltinKind::IntType),
context.tokens().GetIntLiteral(
context.parse_tree().node_token(parse_node))}});
return true;
}
auto HandleRealLiteral(Context& context, Parse::RealLiteralId parse_node)
-> bool {
context.AddInstAndPush(
{parse_node,
SemIR::RealLiteral{context.GetBuiltinType(SemIR::BuiltinKind::FloatType),
context.tokens().GetRealLiteral(
context.parse_tree().node_token(parse_node))}});
return true;
}
auto HandleStringLiteral(Context& context, Parse::StringLiteralId parse_node)
-> bool {
context.AddInstAndPush(
{parse_node, SemIR::StringLiteral{
context.GetBuiltinType(SemIR::BuiltinKind::StringType),
context.tokens().GetStringLiteralValue(
context.parse_tree().node_token(parse_node))}});
return true;
}
auto HandleBoolTypeLiteral(Context& context,
Parse::BoolTypeLiteralId parse_node) -> bool {
context.node_stack().Push(parse_node, SemIR::InstId::BuiltinBoolType);
return true;
}
auto HandleIntTypeLiteral(Context& context, Parse::IntTypeLiteralId parse_node)
-> bool {
auto text = context.tokens().GetTokenText(
context.parse_tree().node_token(parse_node));
if (text != "i32") {
return context.TODO(parse_node, "Currently only i32 is allowed");
}
context.node_stack().Push(parse_node, SemIR::InstId::BuiltinIntType);
return true;
}
auto HandleUnsignedIntTypeLiteral(Context& context,
Parse::UnsignedIntTypeLiteralId parse_node)
-> bool {
return context.TODO(parse_node, "Need to support unsigned type literals");
}
auto HandleFloatTypeLiteral(Context& context,
Parse::FloatTypeLiteralId parse_node) -> bool {
auto text = context.tokens().GetTokenText(
context.parse_tree().node_token(parse_node));
if (text != "f64") {
return context.TODO(parse_node, "Currently only f64 is allowed");
}
context.node_stack().Push(parse_node, SemIR::InstId::BuiltinFloatType);
return true;
}
auto HandleStringTypeLiteral(Context& context,
Parse::StringTypeLiteralId parse_node) -> bool {
context.node_stack().Push(parse_node, SemIR::InstId::BuiltinStringType);
return true;
}
auto HandleTypeTypeLiteral(Context& context,
Parse::TypeTypeLiteralId parse_node) -> bool {
context.node_stack().Push(parse_node, SemIR::InstId::BuiltinTypeType);
return true;
}
} // namespace Carbon::Check