Start drafting out semantic type checking. (#2406)

This is a first pass at what semantic type checking might look like. Types propagate along nodes, we use an InvalidType object when there's an error, and once there's an InvalidType we stop doing so much type checking.

This adds some RealLiteral handling in order to get type mismatches. I'm cautious about creating some real value for SemanticsIR (since the tokenized buffer version is a bit constrained), so I'm not doing that yet. But I will probably need to in order to maintain SemanticsIR having hermetic copies of its data, without a parse tree dependency.
This commit is contained in:
Jon Ross-Perkins
2022-11-17 13:36:40 -08:00
committed by GitHub
parent c9a1e351ba
commit 4c8fdf5124
23 changed files with 298 additions and 55 deletions
@@ -9,6 +9,7 @@
#include "toolchain/lexer/token_kind.h"
#include "toolchain/lexer/tokenized_buffer.h"
#include "toolchain/parser/parse_node_kind.h"
#include "toolchain/semantics/semantics_builtin_kind.h"
#include "toolchain/semantics/semantics_node.h"
namespace Carbon {
@@ -219,12 +220,29 @@ auto SemanticsParseTreeHandler::HandleInfixOperator(ParseTree::Node parse_node)
auto rhs_id = PopWithResult();
auto lhs_id = PopWithResult();
auto block = node_block_stack_.back();
auto lhs_type = semantics_->GetType(block, lhs_id);
auto rhs_type = semantics_->GetType(block, rhs_id);
SemanticsNodeId result_type = lhs_type;
// TODO: This should attempt a type conversion, but there's not enough
// implemented to do that right now.
if (lhs_type != rhs_type) {
auto invalid_type = SemanticsNodeId::MakeBuiltinReference(
SemanticsBuiltinKind::InvalidType());
if (lhs_type != invalid_type && rhs_type != invalid_type) {
// TODO: This is a poor diagnostic, and should be expanded.
CARBON_DIAGNOSTIC(TypeMismatch, Error, "Type mismatch");
emitter_->Emit(parse_tree_->node_token(parse_node), TypeMismatch);
}
result_type = invalid_type;
}
// Figure out the operator for the token.
auto token = parse_tree_->node_token(parse_node);
switch (auto token_kind = tokens_->GetKind(token)) {
case TokenKind::Plus():
Push(parse_node,
SemanticsNode::MakeBinaryOperatorAdd(parse_node, lhs_id, rhs_id));
Push(parse_node, SemanticsNode::MakeBinaryOperatorAdd(
parse_node, result_type, lhs_id, rhs_id));
break;
default:
CARBON_FATAL() << "Unrecognized token kind: " << token_kind.Name();
@@ -241,6 +259,11 @@ auto SemanticsParseTreeHandler::HandleLiteral(ParseTree::Node parse_node)
Push(parse_node, SemanticsNode::MakeIntegerLiteral(parse_node, id));
break;
}
case TokenKind::RealLiteral(): {
// TODO: Add storage of the Real literal.
Push(parse_node, SemanticsNode::MakeRealLiteral(parse_node));
break;
}
default:
CARBON_FATAL() << "Unhandled kind: " << token_kind.Name();
}
@@ -264,8 +287,10 @@ auto SemanticsParseTreeHandler::HandleReturnStatement(
Push(parse_node, SemanticsNode::MakeReturn(parse_node));
} else {
auto arg = PopWithResult();
auto arg_type = semantics_->GetType(node_block_stack_.back(), arg);
Pop(ParseNodeKind::ReturnStatementStart());
Push(parse_node, SemanticsNode::MakeReturnExpression(parse_node, arg));
Push(parse_node,
SemanticsNode::MakeReturnExpression(parse_node, arg_type, arg));
}
}