mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-23 21:12:39 +00:00
This rewrites semantics towards a more pure instruction model, in pursuit of the simple instruction-style output. I think I can get this approach to type-check as it goes along, but obviously this change doesn't prove that yet. I'm separating it out because it's a large rewrite of the semantics structure, tossing out a lot of what was there before. But I think it does help towards several requests, like setting up a clear path for consolidating duplicate identifiers and making the node style more standardized. I expect to need to pass multiple args to function calls, that'd probably be storing vectors of args similar to how I'm showing identifiers and integer literals stored. This removes the semantics namespace because (a) it was getting annoying writing the `::` everywhere, and (b) I think the leaning with Carbon is to avoid namespaces (@chandlerc asked not to put SemanticsIR/SemanticsFactory in a namespace, which is the crux of the issue). But, it's still necessary to avoid name conflicts so I just prefix everything with "Semantics" (still a lot of typing, but no `::`).
60 lines
1.6 KiB
C++
60 lines
1.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
|
|
|
|
#ifndef CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_H_
|
|
#define CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_H_
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
#include "llvm/ADT/SmallVector.h"
|
|
#include "toolchain/parser/parse_tree.h"
|
|
#include "toolchain/semantics/semantics_node.h"
|
|
|
|
namespace Carbon::Testing {
|
|
class SemanticsIRForTest;
|
|
} // namespace Carbon::Testing
|
|
|
|
namespace Carbon {
|
|
|
|
// Provides semantic analysis on a ParseTree.
|
|
class SemanticsIR {
|
|
public:
|
|
// Prints the full IR.
|
|
auto Print(llvm::raw_ostream& out) const -> void;
|
|
|
|
private:
|
|
friend class SemanticsIRFactory;
|
|
|
|
explicit SemanticsIR(const ParseTree& parse_tree)
|
|
: parse_tree_(&parse_tree) {}
|
|
|
|
auto AddIdentifier(llvm::StringRef identifier) -> SemanticsIdentifierId {
|
|
SemanticsIdentifierId id(identifiers_.size());
|
|
identifiers_.push_back(identifier);
|
|
return id;
|
|
}
|
|
|
|
auto AddIntegerLiteral(llvm::APInt integer_literal)
|
|
-> SemanticsIntegerLiteralId {
|
|
SemanticsIntegerLiteralId id(integer_literals_.size());
|
|
integer_literals_.push_back(integer_literal);
|
|
return id;
|
|
}
|
|
|
|
auto AddNode(SemanticsNode node) -> SemanticsNodeId {
|
|
SemanticsNodeId id(nodes_.size());
|
|
nodes_.push_back(node);
|
|
return id;
|
|
}
|
|
|
|
llvm::SmallVector<llvm::StringRef> identifiers_;
|
|
llvm::SmallVector<llvm::APInt> integer_literals_;
|
|
llvm::SmallVector<SemanticsNode> nodes_;
|
|
|
|
const ParseTree* parse_tree_;
|
|
};
|
|
|
|
} // namespace Carbon
|
|
|
|
#endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_H_
|