Initial support for builtin functions. (#3803)

For now, a builtin function is defined by specifying a string literal
initializer in a function declaration:

```carbon
fn MyBuiltin(a: i32) -> i32 = "builtin.name";
```

End-to-end support is included for a sample `"int.add"` builtin
performing integer addition, covering constant evaluation and code
generation.

The implementation here needs substantial refactoring before we'll be
ready to start adding more builtins. That refactoring work will be
coming next. This change is aiming to checkpoint some incremental
progress.
This commit is contained in:
Richard Smith authored and GitHub committed 2024-03-21 20:46:34 +00:00
1 parent 8907948242
commit f0e940ddfd
43 files changed
+1588 -86

No files matched your search

+24
View File
@@ -52,6 +52,30 @@ auto HandleFunctionSignatureFinish(Context& context) -> void {
context.PushState(State::StatementScopeLoop);
break;
}
case Lex::TokenKind::Equal: {
context.AddNode(NodeKind::BuiltinFunctionDefinitionStart,
context.Consume(), state.subtree_start, state.has_error);
if (!context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::StringLiteral,
NodeKind::BuiltinName)) {
CARBON_DIAGNOSTIC(ExpectedBuiltinName, Error,
"Expected builtin function name after `=`.");
context.emitter().Emit(*context.position(), ExpectedBuiltinName);
state.has_error = true;
}
auto semi = context.ConsumeIf(Lex::TokenKind::Semi);
if (!semi && !state.has_error) {
context.EmitExpectedDeclSemi(context.tokens().GetKind(state.token));
state.has_error = true;
}
if (state.has_error) {
context.RecoverFromDeclError(state, NodeKind::BuiltinFunctionDefinition,
/*skip_past_likely_end=*/true);
} else {
context.AddNode(NodeKind::BuiltinFunctionDefinition, *semi,
state.subtree_start, state.has_error);
}
break;
}
default: {
if (!state.has_error) {
context.EmitExpectedDeclSemiOrDefinition(Lex::TokenKind::Fn);