mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 17:31:07 +01:00
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:
+25
-16
@@ -128,39 +128,48 @@ struct Extractable<NodeIdInCategory<Category>> {
|
||||
}
|
||||
};
|
||||
|
||||
static auto NodeIdForKindAccept(NodeKind kind1, NodeKind kind2,
|
||||
const Tree* tree,
|
||||
const Tree::SiblingIterator& it,
|
||||
Tree::SiblingIterator end, ErrorBuilder* trace)
|
||||
static auto NodeIdOneOfAccept(std::initializer_list<NodeKind> kinds,
|
||||
const Tree* tree, const Tree::SiblingIterator& it,
|
||||
Tree::SiblingIterator end, ErrorBuilder* trace)
|
||||
-> bool {
|
||||
auto trace_kinds = [&] {
|
||||
llvm::ListSeparator sep(" or ");
|
||||
for (auto kind : kinds) {
|
||||
*trace << sep << kind;
|
||||
}
|
||||
};
|
||||
auto kind = tree->node_kind(*it);
|
||||
if (it == end || (kind != kind1 && kind != kind2)) {
|
||||
if (it == end || std::find(kinds.begin(), kinds.end(), kind) == kinds.end()) {
|
||||
if (trace) {
|
||||
if (it == end) {
|
||||
*trace << "NodeIdOneOf error: no more children, expected " << kind1
|
||||
<< " or " << kind2 << "\n";
|
||||
*trace << "NodeIdOneOf error: no more children, expected ";
|
||||
trace_kinds();
|
||||
*trace << "\n";
|
||||
} else {
|
||||
*trace << "NodeIdOneOf error: wrong kind " << tree->node_kind(*it)
|
||||
<< ", expected " << kind1 << " or " << kind2 << "\n";
|
||||
<< ", expected ";
|
||||
trace_kinds();
|
||||
*trace << "\n";
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (trace) {
|
||||
*trace << "NodeIdOneOf " << kind1 << " or " << kind2 << ": "
|
||||
<< tree->node_kind(*it) << " consumed\n";
|
||||
*trace << "NodeIdOneOf ";
|
||||
trace_kinds();
|
||||
*trace << ": " << tree->node_kind(*it) << " consumed\n";
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Extract a `NodeIdOneOf<T, U>` as a single required child.
|
||||
template <typename T, typename U>
|
||||
struct Extractable<NodeIdOneOf<T, U>> {
|
||||
// Extract a `NodeIdOneOf<T...>` as a single required child.
|
||||
template <typename... T>
|
||||
struct Extractable<NodeIdOneOf<T...>> {
|
||||
static auto Extract(const Tree* tree, Tree::SiblingIterator& it,
|
||||
Tree::SiblingIterator end, ErrorBuilder* trace)
|
||||
-> std::optional<NodeIdOneOf<T, U>> {
|
||||
if (NodeIdForKindAccept(T::Kind, U::Kind, tree, it, end, trace)) {
|
||||
return NodeIdOneOf<T, U>(*it++);
|
||||
-> std::optional<NodeIdOneOf<T...>> {
|
||||
if (NodeIdOneOfAccept({T::Kind...}, tree, it, end, trace)) {
|
||||
return NodeIdOneOf<T...>(*it++);
|
||||
} else {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user