mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Sorry about the big change, this is hard to split. ParenContents is used by both, templated, and expects the same pointer type. While I could duplicate ParenContents with some ExpressionParenContents or PatternParenContents, that seems a little kludgy versus a single large change handling both. The worst of it is that Expression is already pretty sweeping, Pattern is really just incrementally adding. That said, I believe this includes a couple fixes I found with incorrect use of dyn_cast in typecheck.cpp (checked nullptr at the wrong step in 2 code locations). There's also a missing `*` in member.cpp this caught. I adjust passing of expressions for Return due to nullness (I felt adding another constructor was the best solution). I add a `.Release()` to BisonWrap due to things like `$3.first` needing some way to work through BIsonWrap. I felt this was better than `operator->`, but feel free to comment if you prefer the other path (`.Release()` conveniently lets me do pair unwrapping, so it felt a better solution). I do add a TODO to think about better Ptr-to-Ptr cast<> support too, though, as that doesn't work cleanly with LLVM's infra. But so far it seems to only come up in one spot, so I'm not prioritizing it.
136 lines
3.5 KiB
C++
136 lines
3.5 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 EXECUTABLE_SEMANTICS_INTERPRETER_ACTION_H_
|
|
#define EXECUTABLE_SEMANTICS_INTERPRETER_ACTION_H_
|
|
|
|
#include <vector>
|
|
|
|
#include "common/ostream.h"
|
|
#include "executable_semantics/ast/expression.h"
|
|
#include "executable_semantics/ast/pattern.h"
|
|
#include "executable_semantics/ast/statement.h"
|
|
#include "executable_semantics/interpreter/stack.h"
|
|
#include "executable_semantics/interpreter/value.h"
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
namespace Carbon {
|
|
|
|
class Action {
|
|
public:
|
|
enum class Kind {
|
|
LValAction,
|
|
ExpressionAction,
|
|
PatternAction,
|
|
StatementAction,
|
|
};
|
|
|
|
Action(const Value&) = delete;
|
|
Action& operator=(const Value&) = delete;
|
|
|
|
// The position or state of the action. Starts at 0 and goes up to the number
|
|
// of subexpressions.
|
|
//
|
|
// pos indicates how many of the entries in the following `results` vector
|
|
// will be filled in the next time this action is active.
|
|
// For each i < pos, results[i] contains a pointer to a Value.
|
|
auto Pos() const -> int { return pos; }
|
|
|
|
// Results from a subexpression.
|
|
auto Results() const -> const std::vector<const Value*>& { return results; }
|
|
|
|
void IncrementPos() { ++pos; }
|
|
|
|
void AddResult(const Value* result) { results.push_back(result); }
|
|
|
|
void Clear() {
|
|
pos = 0;
|
|
results.clear();
|
|
}
|
|
|
|
// Returns the enumerator corresponding to the most-derived type of this
|
|
// object.
|
|
auto Tag() const -> Kind { return tag; }
|
|
|
|
static void PrintList(const Stack<Ptr<Action>>& ls, llvm::raw_ostream& out);
|
|
|
|
void Print(llvm::raw_ostream& out) const;
|
|
LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
|
|
|
|
protected:
|
|
// Constructs an Action. `tag` must be the enumerator corresponding to the
|
|
// most-derived type being constructed.
|
|
explicit Action(Kind tag) : tag(tag) {}
|
|
|
|
private:
|
|
int pos = 0;
|
|
std::vector<const Value*> results;
|
|
|
|
const Kind tag;
|
|
};
|
|
|
|
class LValAction : public Action {
|
|
public:
|
|
explicit LValAction(Ptr<const Expression> exp)
|
|
: Action(Kind::LValAction), exp(exp) {}
|
|
|
|
static auto classof(const Action* action) -> bool {
|
|
return action->Tag() == Kind::LValAction;
|
|
}
|
|
|
|
auto Exp() const -> Ptr<const Expression> { return exp; }
|
|
|
|
private:
|
|
Ptr<const Expression> exp;
|
|
};
|
|
|
|
class ExpressionAction : public Action {
|
|
public:
|
|
explicit ExpressionAction(Ptr<const Expression> exp)
|
|
: Action(Kind::ExpressionAction), exp(exp) {}
|
|
|
|
static auto classof(const Action* action) -> bool {
|
|
return action->Tag() == Kind::ExpressionAction;
|
|
}
|
|
|
|
auto Exp() const -> Ptr<const Expression> { return exp; }
|
|
|
|
private:
|
|
Ptr<const Expression> exp;
|
|
};
|
|
|
|
class PatternAction : public Action {
|
|
public:
|
|
explicit PatternAction(Ptr<const Pattern> pat)
|
|
: Action(Kind::PatternAction), pat(pat) {}
|
|
|
|
static auto classof(const Action* action) -> bool {
|
|
return action->Tag() == Kind::PatternAction;
|
|
}
|
|
|
|
auto Pat() const -> Ptr<const Pattern> { return pat; }
|
|
|
|
private:
|
|
Ptr<const Pattern> pat;
|
|
};
|
|
|
|
class StatementAction : public Action {
|
|
public:
|
|
explicit StatementAction(const Statement* stmt)
|
|
: Action(Kind::StatementAction), stmt(stmt) {}
|
|
|
|
static auto classof(const Action* action) -> bool {
|
|
return action->Tag() == Kind::StatementAction;
|
|
}
|
|
|
|
auto Stmt() const -> const Statement* { return stmt; }
|
|
|
|
private:
|
|
const Statement* stmt;
|
|
};
|
|
|
|
} // namespace Carbon
|
|
|
|
#endif // EXECUTABLE_SEMANTICS_INTERPRETER_ACTION_H_
|