skia2/src/sksl/ir/SkSLIRNode.h

231 lines
7.1 KiB
C
Raw Normal View History

/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SKSL_IRNODE
#define SKSL_IRNODE
#include "src/sksl/SkSLASTNode.h"
#include "src/sksl/SkSLLexer.h"
#include "src/sksl/SkSLString.h"
#include <algorithm>
#include <vector>
namespace SkSL {
struct Expression;
struct Statement;
class SymbolTable;
class Type;
/**
* Represents a node in the intermediate representation (IR) tree. The IR is a fully-resolved
* version of the program (all types determined, everything validated), ready for code generation.
*/
class IRNode {
public:
virtual ~IRNode();
Revert "Enable deprecated-copy-dtor warning." This reverts commit e990fcc4b0c7d2d7b7e5350970da538a8c7a6c20. Reason for revert: Build-Win-Clang-x86_64-Release-Shared Original change's description: > Enable deprecated-copy-dtor warning. > > In C++11 a user declared destructor still requires the compiler to > implicitly default the copy constructor and copy assignment operator, > but this is deprecated. Note that a user declared destructor suppresses > the move constructor and move assignment operator; a user declared > destructor exists if any '~Foo' method declaration appears inside > 'class Foo' (even if defaulted); if the copy and move operations are the > same then copy operations that take 'const Foo&' will do fine double > duty as move operations. > > Clang seems to have an issue with this warning, in that it does not > appear to distinguish between compiler defaulted and user defaulted > destructors. As a result, it does not always warn when it should. > There may yet be places in the code where a move operation is desired > but may be suppressed because the implicitly defaulted moves are not > declared because a destructor has been declared. > > This wraps dawn and shaderc configs in 'third_party' so that their > headers will be included through '-isystem' in order to avoid the > warnings generated by including their headers. > > Change-Id: I681524cd890d86305aa99b6b765a52113b4dfa4b > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/280406 > Reviewed-by: Mike Klein <mtklein@google.com> > Reviewed-by: Brian Salomon <bsalomon@google.com> > Commit-Queue: Ben Wagner <bungeman@google.com> TBR=mtklein@google.com,bsalomon@google.com,bungeman@google.com Change-Id: Icd6a2487637d21fcf7c4c7ab7cba7a8adfda5afd No-Presubmit: true No-Tree-Checks: true No-Try: true Reviewed-on: https://skia-review.googlesource.com/c/skia/+/280836 Reviewed-by: Ben Wagner <bungeman@google.com> Commit-Queue: Ben Wagner <bungeman@google.com>
2020-03-31 22:31:46 +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;
}
virtual String description() const = 0;
// character offset of this element within the program being compiled, for error reporting
// purposes
int fOffset;
const Type& type() const {
switch (fData.fKind) {
case NodeData::Kind::kBoolLiteral:
return *this->boolLiteralData().fType;
case NodeData::Kind::kType:
return *this->typeData();
case NodeData::Kind::kTypeToken:
return *this->typeTokenData().fType;
default:
SkUNREACHABLE;
}
}
protected:
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;
};
struct BoolLiteralData {
const Type* fType;
bool fValue;
};
struct TypeTokenData {
const Type* fType;
Token::Kind fToken;
};
struct NodeData {
char fBytes[std::max({sizeof(BlockData),
sizeof(BoolLiteralData),
sizeof(Type*),
sizeof(TypeTokenData)})];
enum class Kind {
kBlock,
kBoolLiteral,
kType,
kTypeToken,
} fKind;
NodeData() = default;
NodeData(const BlockData& data)
: fKind(Kind::kBlock) {
*(new(fBytes) BlockData) = data;
}
NodeData(const BoolLiteralData& data)
: fKind(Kind::kBoolLiteral) {
*(new(fBytes) BoolLiteralData) = data;
}
NodeData(const Type* data)
: fKind(Kind::kType) {
*(new(fBytes) const Type*) = data;
}
NodeData(const TypeTokenData& data)
: fKind(Kind::kTypeToken) {
*(new(fBytes) TypeTokenData) = data;
}
~NodeData() {
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;
}
}
};
IRNode(int offset, int kind, const BlockData& data,
std::vector<std::unique_ptr<Statement>> stmts);
IRNode(int offset, int kind, const BoolLiteralData& data);
IRNode(int offset, int kind, const Type* data = nullptr);
IRNode(int offset, int kind, const TypeTokenData& data);
IRNode(const IRNode& other);
Expression& expressionChild(int index) const {
SkASSERT(index >= 0 && index < (int) fExpressionChildren.size());
return *fExpressionChildren[index];
}
std::unique_ptr<Expression>& expressionPointer(int index) {
SkASSERT(index >= 0 && index < (int) fExpressionChildren.size());
return fExpressionChildren[index];
}
const std::unique_ptr<Expression>& expressionPointer(int index) const {
SkASSERT(index >= 0 && index < (int) fExpressionChildren.size());
return fExpressionChildren[index];
}
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);
}
const BoolLiteralData& boolLiteralData() const {
SkASSERT(fData.fKind == NodeData::Kind::kBoolLiteral);
return *reinterpret_cast<const BoolLiteralData*>(fData.fBytes);
}
const Type* typeData() const {
SkASSERT(fData.fKind == NodeData::Kind::kType);
return *reinterpret_cast<const Type* const*>(fData.fBytes);
}
const TypeTokenData& typeTokenData() const {
SkASSERT(fData.fKind == NodeData::Kind::kTypeToken);
return *reinterpret_cast<const TypeTokenData*>(fData.fBytes);
}
int fKind;
NodeData fData;
// 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;
};
} // namespace SkSL
#endif