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-30 18:29:56 +00:00
|
|
|
class ExternalValue;
|
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-28 13:18:15 +00:00
|
|
|
case NodeData::Kind::kBoolLiteral:
|
|
|
|
return *this->boolLiteralData().fType;
|
2020-09-30 18:29:56 +00:00
|
|
|
case NodeData::Kind::kExternalValue:
|
|
|
|
return *this->externalValueData().fType;
|
2020-09-28 20:27:18 +00:00
|
|
|
case NodeData::Kind::kIntLiteral:
|
|
|
|
return *this->intLiteralData().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-28 13:18:15 +00:00
|
|
|
struct BoolLiteralData {
|
|
|
|
const Type* fType;
|
|
|
|
bool fValue;
|
|
|
|
};
|
|
|
|
|
2020-09-29 21:05:54 +00:00
|
|
|
struct EnumData {
|
|
|
|
StringFragment fTypeName;
|
|
|
|
std::shared_ptr<SymbolTable> fSymbols;
|
|
|
|
bool fIsBuiltin;
|
|
|
|
};
|
|
|
|
|
2020-09-30 18:29:56 +00:00
|
|
|
struct ExternalValueData {
|
|
|
|
const Type* fType;
|
|
|
|
const ExternalValue* fValue;
|
|
|
|
};
|
|
|
|
|
2020-09-28 20:27:18 +00:00
|
|
|
struct IntLiteralData {
|
|
|
|
const Type* fType;
|
|
|
|
int64_t fValue;
|
|
|
|
};
|
|
|
|
|
2020-09-22 19:05:37 +00:00
|
|
|
struct TypeTokenData {
|
|
|
|
const Type* fType;
|
|
|
|
Token::Kind fToken;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NodeData {
|
|
|
|
enum class Kind {
|
2020-09-25 18:31:59 +00:00
|
|
|
kBlock,
|
2020-09-28 13:18:15 +00:00
|
|
|
kBoolLiteral,
|
2020-09-29 21:05:54 +00:00
|
|
|
kEnum,
|
2020-09-30 18:29:56 +00:00
|
|
|
kExternalValue,
|
2020-09-28 20:27:18 +00:00
|
|
|
kIntLiteral,
|
2020-09-30 14:17:00 +00:00
|
|
|
kString,
|
2020-09-22 19:05:37 +00:00
|
|
|
kType,
|
|
|
|
kTypeToken,
|
2020-09-28 20:27:18 +00:00
|
|
|
} fKind = Kind::kType;
|
|
|
|
// it doesn't really matter what kind we default to, as long as it's a POD type
|
2020-09-22 19:05:37 +00:00
|
|
|
|
2020-09-28 20:27:18 +00:00
|
|
|
union Contents {
|
|
|
|
BlockData fBlock;
|
|
|
|
BoolLiteralData fBoolLiteral;
|
2020-09-29 21:05:54 +00:00
|
|
|
EnumData fEnum;
|
2020-09-30 18:29:56 +00:00
|
|
|
ExternalValueData fExternalValue;
|
2020-09-28 20:27:18 +00:00
|
|
|
IntLiteralData fIntLiteral;
|
2020-09-30 14:17:00 +00:00
|
|
|
String fString;
|
2020-09-28 20:27:18 +00:00
|
|
|
const Type* fType;
|
|
|
|
TypeTokenData fTypeToken;
|
|
|
|
|
|
|
|
Contents() {}
|
|
|
|
|
|
|
|
~Contents() {}
|
|
|
|
} fContents;
|
2020-09-22 19:05:37 +00:00
|
|
|
|
2020-09-28 13:18:15 +00:00
|
|
|
NodeData(const BlockData& data)
|
2020-09-25 18:31:59 +00:00
|
|
|
: fKind(Kind::kBlock) {
|
2020-09-28 20:27:18 +00:00
|
|
|
*(new(&fContents) BlockData) = data;
|
2020-09-28 13:18:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NodeData(const BoolLiteralData& data)
|
|
|
|
: fKind(Kind::kBoolLiteral) {
|
2020-09-28 20:27:18 +00:00
|
|
|
*(new(&fContents) BoolLiteralData) = data;
|
|
|
|
}
|
|
|
|
|
2020-09-29 21:05:54 +00:00
|
|
|
NodeData(const EnumData& data)
|
|
|
|
: fKind(Kind::kEnum) {
|
|
|
|
*(new(&fContents) EnumData) = data;
|
|
|
|
}
|
|
|
|
|
2020-09-30 18:29:56 +00:00
|
|
|
NodeData(const ExternalValueData& data)
|
|
|
|
: fKind(Kind::kExternalValue) {
|
|
|
|
*(new(&fContents) ExternalValueData) = data;
|
|
|
|
}
|
|
|
|
|
2020-09-28 20:27:18 +00:00
|
|
|
NodeData(IntLiteralData data)
|
|
|
|
: fKind(Kind::kIntLiteral) {
|
|
|
|
*(new(&fContents) IntLiteralData) = data;
|
2020-09-25 18:31:59 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 14:17:00 +00:00
|
|
|
NodeData(const String& data)
|
|
|
|
: fKind(Kind::kString) {
|
|
|
|
*(new(&fContents) String) = data;
|
|
|
|
}
|
|
|
|
|
2020-09-22 19:05:37 +00:00
|
|
|
NodeData(const Type* data)
|
|
|
|
: fKind(Kind::kType) {
|
2020-09-28 20:27:18 +00:00
|
|
|
*(new(&fContents) const Type*) = data;
|
2020-09-22 19:05:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 13:18:15 +00:00
|
|
|
NodeData(const TypeTokenData& data)
|
2020-09-22 19:05:37 +00:00
|
|
|
: fKind(Kind::kTypeToken) {
|
2020-09-28 20:27:18 +00:00
|
|
|
*(new(&fContents) TypeTokenData) = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeData(const NodeData& other) {
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeData& operator=(const NodeData& other) {
|
|
|
|
this->cleanup();
|
|
|
|
fKind = other.fKind;
|
|
|
|
switch (fKind) {
|
|
|
|
case Kind::kBlock:
|
|
|
|
*(new(&fContents) BlockData) = other.fContents.fBlock;
|
|
|
|
break;
|
|
|
|
case Kind::kBoolLiteral:
|
|
|
|
*(new(&fContents) BoolLiteralData) = other.fContents.fBoolLiteral;
|
|
|
|
break;
|
2020-09-29 21:05:54 +00:00
|
|
|
case Kind::kEnum:
|
|
|
|
*(new(&fContents) EnumData) = other.fContents.fEnum;
|
|
|
|
break;
|
2020-09-30 18:29:56 +00:00
|
|
|
case Kind::kExternalValue:
|
|
|
|
*(new(&fContents) ExternalValueData) = other.fContents.fExternalValue;
|
|
|
|
break;
|
2020-09-28 20:27:18 +00:00
|
|
|
case Kind::kIntLiteral:
|
|
|
|
*(new(&fContents) IntLiteralData) = other.fContents.fIntLiteral;
|
|
|
|
break;
|
2020-09-30 14:17:00 +00:00
|
|
|
case Kind::kString:
|
|
|
|
*(new(&fContents) String) = other.fContents.fString;
|
|
|
|
break;
|
2020-09-28 20:27:18 +00:00
|
|
|
case Kind::kType:
|
|
|
|
*(new(&fContents) const Type*) = other.fContents.fType;
|
|
|
|
break;
|
|
|
|
case Kind::kTypeToken:
|
|
|
|
*(new(&fContents) TypeTokenData) = other.fContents.fTypeToken;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return *this;
|
2020-09-22 19:05:37 +00:00
|
|
|
}
|
2020-09-25 18:31:59 +00:00
|
|
|
|
|
|
|
~NodeData() {
|
2020-09-28 20:27:18 +00:00
|
|
|
this->cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void cleanup() {
|
2020-09-28 13:18:15 +00:00
|
|
|
switch (fKind) {
|
|
|
|
case Kind::kBlock:
|
2020-09-28 20:27:18 +00:00
|
|
|
fContents.fBlock.~BlockData();
|
2020-09-28 13:18:15 +00:00
|
|
|
break;
|
|
|
|
case Kind::kBoolLiteral:
|
2020-09-28 20:27:18 +00:00
|
|
|
fContents.fBoolLiteral.~BoolLiteralData();
|
|
|
|
break;
|
2020-09-29 21:05:54 +00:00
|
|
|
case Kind::kEnum:
|
|
|
|
fContents.fEnum.~EnumData();
|
|
|
|
break;
|
2020-09-30 18:29:56 +00:00
|
|
|
case Kind::kExternalValue:
|
|
|
|
fContents.fExternalValue.~ExternalValueData();
|
|
|
|
break;
|
2020-09-28 20:27:18 +00:00
|
|
|
case Kind::kIntLiteral:
|
|
|
|
fContents.fIntLiteral.~IntLiteralData();
|
2020-09-28 13:18:15 +00:00
|
|
|
break;
|
2020-09-30 14:17:00 +00:00
|
|
|
case Kind::kString:
|
|
|
|
fContents.fString.~String();
|
|
|
|
break;
|
2020-09-28 13:18:15 +00:00
|
|
|
case Kind::kType:
|
|
|
|
break;
|
|
|
|
case Kind::kTypeToken:
|
2020-09-28 20:27:18 +00:00
|
|
|
fContents.fTypeToken.~TypeTokenData();
|
2020-09-28 13:18:15 +00:00
|
|
|
break;
|
2020-09-25 18:31:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-22 19:05:37 +00:00
|
|
|
};
|
|
|
|
|
2020-09-28 13:18:15 +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-29 21:05:54 +00:00
|
|
|
IRNode(int offset, int kind, const EnumData& data);
|
|
|
|
|
2020-09-30 18:29:56 +00:00
|
|
|
IRNode(int offset, int kind, const ExternalValueData& data);
|
|
|
|
|
2020-09-28 20:27:18 +00:00
|
|
|
IRNode(int offset, int kind, const IntLiteralData& data);
|
|
|
|
|
2020-09-30 14:17:00 +00:00
|
|
|
IRNode(int offset, int kind, const String& data);
|
|
|
|
|
2020-09-22 19:05:37 +00:00
|
|
|
IRNode(int offset, int kind, const Type* data = nullptr);
|
|
|
|
|
2020-09-28 13:18:15 +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);
|
2020-09-28 20:27:18 +00:00
|
|
|
return fData.fContents.fBlock;
|
2020-09-25 18:31:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const BlockData& blockData() const {
|
|
|
|
SkASSERT(fData.fKind == NodeData::Kind::kBlock);
|
2020-09-28 20:27:18 +00:00
|
|
|
return fData.fContents.fBlock;
|
2020-09-25 18:31:59 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 13:18:15 +00:00
|
|
|
const BoolLiteralData& boolLiteralData() const {
|
|
|
|
SkASSERT(fData.fKind == NodeData::Kind::kBoolLiteral);
|
2020-09-28 20:27:18 +00:00
|
|
|
return fData.fContents.fBoolLiteral;
|
|
|
|
}
|
|
|
|
|
2020-09-29 21:05:54 +00:00
|
|
|
const EnumData& enumData() const {
|
|
|
|
SkASSERT(fData.fKind == NodeData::Kind::kEnum);
|
|
|
|
return fData.fContents.fEnum;
|
|
|
|
}
|
|
|
|
|
2020-09-30 18:29:56 +00:00
|
|
|
const ExternalValueData& externalValueData() const {
|
|
|
|
SkASSERT(fData.fKind == NodeData::Kind::kExternalValue);
|
|
|
|
return fData.fContents.fExternalValue;
|
|
|
|
}
|
|
|
|
|
2020-09-28 20:27:18 +00:00
|
|
|
const IntLiteralData& intLiteralData() const {
|
|
|
|
SkASSERT(fData.fKind == NodeData::Kind::kIntLiteral);
|
|
|
|
return fData.fContents.fIntLiteral;
|
2020-09-28 13:18:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 14:17:00 +00:00
|
|
|
const String& stringData() const {
|
|
|
|
SkASSERT(fData.fKind == NodeData::Kind::kString);
|
|
|
|
return fData.fContents.fString;
|
|
|
|
}
|
|
|
|
|
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-28 20:27:18 +00:00
|
|
|
return fData.fContents.fType;
|
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-28 20:27:18 +00:00
|
|
|
return fData.fContents.fTypeToken;
|
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
|