2016-07-01 15:22:01 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2017-03-31 17:56:23 +00:00
|
|
|
|
2016-07-01 15:22:01 +00:00
|
|
|
#ifndef SKSL_IRNODE
|
|
|
|
#define SKSL_IRNODE
|
|
|
|
|
2020-09-22 19:05:37 +00:00
|
|
|
#include "src/sksl/SkSLASTNode.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/sksl/SkSLLexer.h"
|
2019-05-15 19:29:54 +00:00
|
|
|
#include "src/sksl/SkSLString.h"
|
2016-07-01 15:22:01 +00:00
|
|
|
|
2020-09-25 18:31:59 +00:00
|
|
|
#include <algorithm>
|
2020-09-22 19:05:37 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2016-07-01 15:22:01 +00:00
|
|
|
namespace SkSL {
|
|
|
|
|
2020-09-22 19:05:37 +00:00
|
|
|
struct Expression;
|
2020-09-25 18:31:59 +00:00
|
|
|
struct Statement;
|
|
|
|
class SymbolTable;
|
2020-09-11 16:27:26 +00:00
|
|
|
class Type;
|
|
|
|
|
2016-07-01 15:22:01 +00:00
|
|
|
/**
|
2017-03-31 17:56:23 +00:00
|
|
|
* Represents a node in the intermediate representation (IR) tree. The IR is a fully-resolved
|
2016-07-01 15:22:01 +00:00
|
|
|
* version of the program (all types determined, everything validated), ready for code generation.
|
|
|
|
*/
|
2020-09-11 16:27:26 +00:00
|
|
|
class IRNode {
|
|
|
|
public:
|
2020-09-22 19:05:37 +00:00
|
|
|
virtual ~IRNode();
|
2020-03-31 22:31:46 +00:00
|
|
|
|
2020-09-22 19:05:37 +00:00
|
|
|
IRNode& operator=(const IRNode& other) {
|
|
|
|
// Need to have a copy assignment operator because Type requires it, but can't use the
|
|
|
|
// default version until we finish migrating away from std::unique_ptr children. For now,
|
|
|
|
// just assert that there are no children (we could theoretically clone them, but we never
|
|
|
|
// actually copy nodes containing children).
|
|
|
|
SkASSERT(other.fExpressionChildren.empty());
|
|
|
|
fKind = other.fKind;
|
|
|
|
fOffset = other.fOffset;
|
|
|
|
fData = other.fData;
|
|
|
|
return *this;
|
|
|
|
}
|
2016-07-01 15:22:01 +00:00
|
|
|
|
2017-03-31 17:56:23 +00:00
|
|
|
virtual String description() const = 0;
|
2016-07-01 15:22:01 +00:00
|
|
|
|
2017-09-11 20:50:14 +00:00
|
|
|
// character offset of this element within the program being compiled, for error reporting
|
|
|
|
// purposes
|
2017-09-18 18:10:39 +00:00
|
|
|
int fOffset;
|
2020-09-08 14:22:09 +00:00
|
|
|
|
2020-09-11 16:27:26 +00:00
|
|
|
const Type& type() const {
|
2020-09-22 19:05:37 +00:00
|
|
|
switch (fData.fKind) {
|
2020-09-25 21:20:49 +00:00
|
|
|
case NodeData::Kind::kBoolLiteral:
|
|
|
|
return *this->boolLiteralData().fType;
|
2020-09-22 19:05:37 +00:00
|
|
|
case NodeData::Kind::kType:
|
|
|
|
return *this->typeData();
|
|
|
|
case NodeData::Kind::kTypeToken:
|
|
|
|
return *this->typeTokenData().fType;
|
|
|
|
default:
|
|
|
|
SkUNREACHABLE;
|
|
|
|
}
|
2020-09-11 16:27:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 14:22:09 +00:00
|
|
|
protected:
|
2020-09-25 18:31:59 +00:00
|
|
|
struct BlockData {
|
|
|
|
std::shared_ptr<SymbolTable> fSymbolTable;
|
|
|
|
// if isScope is false, this is just a group of statements rather than an actual
|
|
|
|
// language-level block. This allows us to pass around multiple statements as if they were a
|
|
|
|
// single unit, with no semantic impact.
|
|
|
|
bool fIsScope;
|
|
|
|
};
|
|
|
|
|
2020-09-25 21:20:49 +00:00
|
|
|
struct BoolLiteralData {
|
|
|
|
const Type* fType;
|
|
|
|
bool fValue;
|
|
|
|
};
|
|
|
|
|
2020-09-22 19:05:37 +00:00
|
|
|
struct TypeTokenData {
|
|
|
|
const Type* fType;
|
|
|
|
Token::Kind fToken;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NodeData {
|
2020-09-25 18:31:59 +00:00
|
|
|
char fBytes[std::max({sizeof(BlockData),
|
2020-09-25 21:20:49 +00:00
|
|
|
sizeof(BoolLiteralData),
|
2020-09-25 18:31:59 +00:00
|
|
|
sizeof(Type*),
|
|
|
|
sizeof(TypeTokenData)})];
|
2020-09-22 19:05:37 +00:00
|
|
|
|
|
|
|
enum class Kind {
|
2020-09-25 18:31:59 +00:00
|
|
|
kBlock,
|
2020-09-25 21:20:49 +00:00
|
|
|
kBoolLiteral,
|
2020-09-22 19:05:37 +00:00
|
|
|
kType,
|
|
|
|
kTypeToken,
|
|
|
|
} fKind;
|
|
|
|
|
|
|
|
NodeData() = default;
|
|
|
|
|
2020-09-25 21:20:49 +00:00
|
|
|
NodeData(const BlockData& data)
|
2020-09-25 18:31:59 +00:00
|
|
|
: fKind(Kind::kBlock) {
|
2020-09-25 21:20:49 +00:00
|
|
|
*(new(fBytes) BlockData) = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeData(const BoolLiteralData& data)
|
|
|
|
: fKind(Kind::kBoolLiteral) {
|
|
|
|
*(new(fBytes) BoolLiteralData) = data;
|
2020-09-25 18:31:59 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 19:05:37 +00:00
|
|
|
NodeData(const Type* data)
|
|
|
|
: fKind(Kind::kType) {
|
2020-09-25 21:20:49 +00:00
|
|
|
*(new(fBytes) const Type*) = data;
|
2020-09-22 19:05:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 21:20:49 +00:00
|
|
|
NodeData(const TypeTokenData& data)
|
2020-09-22 19:05:37 +00:00
|
|
|
: fKind(Kind::kTypeToken) {
|
2020-09-25 21:20:49 +00:00
|
|
|
*(new(fBytes) TypeTokenData) = data;
|
2020-09-22 19:05:37 +00:00
|
|
|
}
|
2020-09-25 18:31:59 +00:00
|
|
|
|
|
|
|
~NodeData() {
|
2020-09-25 21:20:49 +00:00
|
|
|
switch (fKind) {
|
|
|
|
case Kind::kBlock:
|
|
|
|
reinterpret_cast<BlockData*>(fBytes)->~BlockData();
|
|
|
|
break;
|
|
|
|
case Kind::kBoolLiteral:
|
|
|
|
reinterpret_cast<BoolLiteralData*>(fBytes)->~BoolLiteralData();
|
|
|
|
break;
|
|
|
|
case Kind::kType:
|
|
|
|
break;
|
|
|
|
case Kind::kTypeToken:
|
|
|
|
reinterpret_cast<TypeTokenData*>(fBytes)->~TypeTokenData();
|
|
|
|
break;
|
2020-09-25 18:31:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-22 19:05:37 +00:00
|
|
|
};
|
|
|
|
|
2020-09-25 21:20:49 +00:00
|
|
|
IRNode(int offset, int kind, const BlockData& data,
|
|
|
|
std::vector<std::unique_ptr<Statement>> stmts);
|
|
|
|
|
|
|
|
IRNode(int offset, int kind, const BoolLiteralData& data);
|
2020-09-25 18:31:59 +00:00
|
|
|
|
2020-09-22 19:05:37 +00:00
|
|
|
IRNode(int offset, int kind, const Type* data = nullptr);
|
|
|
|
|
2020-09-25 21:20:49 +00:00
|
|
|
IRNode(int offset, int kind, const TypeTokenData& data);
|
2020-09-22 19:05:37 +00:00
|
|
|
|
|
|
|
IRNode(const IRNode& other);
|
|
|
|
|
|
|
|
Expression& expressionChild(int index) const {
|
2020-09-25 18:31:59 +00:00
|
|
|
SkASSERT(index >= 0 && index < (int) fExpressionChildren.size());
|
2020-09-22 19:05:37 +00:00
|
|
|
return *fExpressionChildren[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Expression>& expressionPointer(int index) {
|
2020-09-25 18:31:59 +00:00
|
|
|
SkASSERT(index >= 0 && index < (int) fExpressionChildren.size());
|
2020-09-22 19:05:37 +00:00
|
|
|
return fExpressionChildren[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::unique_ptr<Expression>& expressionPointer(int index) const {
|
2020-09-25 18:31:59 +00:00
|
|
|
SkASSERT(index >= 0 && index < (int) fExpressionChildren.size());
|
2020-09-22 19:05:37 +00:00
|
|
|
return fExpressionChildren[index];
|
|
|
|
}
|
|
|
|
|
2020-09-25 18:31:59 +00:00
|
|
|
int expressionChildCount() const {
|
|
|
|
return fExpressionChildren.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Statement& statementChild(int index) const {
|
|
|
|
SkASSERT(index >= 0 && index < (int) fStatementChildren.size());
|
|
|
|
return *fStatementChildren[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Statement>& statementPointer(int index) {
|
|
|
|
SkASSERT(index >= 0 && index < (int) fStatementChildren.size());
|
|
|
|
return fStatementChildren[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::unique_ptr<Statement>& statementPointer(int index) const {
|
|
|
|
SkASSERT(index >= 0 && index < (int) fStatementChildren.size());
|
|
|
|
return fStatementChildren[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
int statementChildCount() const {
|
|
|
|
return fStatementChildren.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockData& blockData() {
|
|
|
|
SkASSERT(fData.fKind == NodeData::Kind::kBlock);
|
|
|
|
return *reinterpret_cast<BlockData*>(fData.fBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
const BlockData& blockData() const {
|
|
|
|
SkASSERT(fData.fKind == NodeData::Kind::kBlock);
|
|
|
|
return *reinterpret_cast<const BlockData*>(fData.fBytes);
|
|
|
|
}
|
|
|
|
|
2020-09-25 21:20:49 +00:00
|
|
|
const BoolLiteralData& boolLiteralData() const {
|
|
|
|
SkASSERT(fData.fKind == NodeData::Kind::kBoolLiteral);
|
|
|
|
return *reinterpret_cast<const BoolLiteralData*>(fData.fBytes);
|
|
|
|
}
|
|
|
|
|
2020-09-25 18:31:59 +00:00
|
|
|
const Type* typeData() const {
|
2020-09-22 19:05:37 +00:00
|
|
|
SkASSERT(fData.fKind == NodeData::Kind::kType);
|
2020-09-25 18:31:59 +00:00
|
|
|
return *reinterpret_cast<const Type* const*>(fData.fBytes);
|
2020-09-22 19:05:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 18:31:59 +00:00
|
|
|
const TypeTokenData& typeTokenData() const {
|
2020-09-22 19:05:37 +00:00
|
|
|
SkASSERT(fData.fKind == NodeData::Kind::kTypeToken);
|
2020-09-25 18:31:59 +00:00
|
|
|
return *reinterpret_cast<const TypeTokenData*>(fData.fBytes);
|
2020-09-22 19:05:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 14:22:09 +00:00
|
|
|
int fKind;
|
2020-09-11 16:27:26 +00:00
|
|
|
|
2020-09-22 19:05:37 +00:00
|
|
|
NodeData fData;
|
2020-09-25 18:31:59 +00:00
|
|
|
|
|
|
|
// Needing two separate vectors is a temporary issue. Ideally, we'd just be able to use a single
|
|
|
|
// vector of nodes, but there are various spots where we take pointers to std::unique_ptr<>,
|
|
|
|
// and it isn't safe to pun std::unique_ptr<IRNode> to std::unique_ptr<Statement / Expression>.
|
|
|
|
// And we can't update the call sites to expect std::unique_ptr<IRNode> while there are still
|
|
|
|
// old-style nodes around.
|
|
|
|
// When the transition is finished, we'll be able to drop the unique_ptrs and just handle
|
|
|
|
// <IRNode> directly.
|
|
|
|
std::vector<std::unique_ptr<Expression>> fExpressionChildren;
|
|
|
|
// it's important to keep fStatements defined after (and thus destroyed before) fData,
|
|
|
|
// because destroying statements can modify reference counts in a SymbolTable contained in fData
|
|
|
|
std::vector<std::unique_ptr<Statement>> fStatementChildren;
|
|
|
|
|
2016-07-01 15:22:01 +00:00
|
|
|
};
|
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace SkSL
|
2016-07-01 15:22:01 +00:00
|
|
|
|
|
|
|
#endif
|