Avoid non-oneliner definition in class and add missing iterators.

This commit is contained in:
Lei Zhang 2016-08-12 09:13:04 -04:00
parent 7a94e66433
commit 4b3247feba
4 changed files with 33 additions and 9 deletions

View File

@ -36,6 +36,7 @@
#include <vector> #include <vector>
#include "instruction.h" #include "instruction.h"
#include "iterator.h"
namespace spvtools { namespace spvtools {
namespace ir { namespace ir {
@ -45,16 +46,22 @@ class Function;
// A SPIR-V basic block. // A SPIR-V basic block.
class BasicBlock { class BasicBlock {
public: public:
// Creates a basic block with the given enclosing |function| and starting using iterator = UptrVectorIterator<Instruction>;
// |label|. using const_iterator = UptrVectorIterator<Instruction, true>;
BasicBlock(std::unique_ptr<Instruction> label)
: function_(nullptr), label_(std::move(label)) {} // Creates a basic block with the given starting |label|.
inline explicit BasicBlock(std::unique_ptr<Instruction> label);
// Sets the enclosing function for this basic block. // Sets the enclosing function for this basic block.
void SetParent(Function* function) { function_ = function; } void SetParent(Function* function) { function_ = function; }
// Appends an instruction to this basic block. // Appends an instruction to this basic block.
inline void AddInstruction(std::unique_ptr<Instruction> i); inline void AddInstruction(std::unique_ptr<Instruction> i);
iterator begin() { return iterator(&insts_, insts_.begin()); }
iterator end() { return iterator(&insts_, insts_.end()); }
const_iterator cbegin() { return const_iterator(&insts_, insts_.cbegin()); }
const_iterator cend() { return const_iterator(&insts_, insts_.cend()); }
// Runs the given function |f| on each instruction in this basic block. // Runs the given function |f| on each instruction in this basic block.
inline void ForEachInst(const std::function<void(Instruction*)>& f); inline void ForEachInst(const std::function<void(Instruction*)>& f);
@ -71,6 +78,9 @@ class BasicBlock {
std::vector<std::unique_ptr<Instruction>> insts_; std::vector<std::unique_ptr<Instruction>> insts_;
}; };
inline BasicBlock::BasicBlock(std::unique_ptr<Instruction> label)
: function_(nullptr), label_(std::move(label)) {}
inline void BasicBlock::AddInstruction(std::unique_ptr<Instruction> i) { inline void BasicBlock::AddInstruction(std::unique_ptr<Instruction> i) {
insts_.emplace_back(std::move(i)); insts_.emplace_back(std::move(i));
} }

View File

@ -47,11 +47,9 @@ class Function {
using iterator = UptrVectorIterator<BasicBlock>; using iterator = UptrVectorIterator<BasicBlock>;
using const_iterator = UptrVectorIterator<BasicBlock, true>; using const_iterator = UptrVectorIterator<BasicBlock, true>;
// Creates a function instance declared by the given instruction |def_inst|. // Creates a function instance declared by the given OpFunction instruction
Function(std::unique_ptr<Instruction> def_inst) // |def_inst|.
: module_(nullptr), inline explicit Function(std::unique_ptr<Instruction> def_inst);
def_inst_(std::move(def_inst)),
end_inst_(SpvOpFunctionEnd) {}
// Sets the enclosing module for this function. // Sets the enclosing module for this function.
void SetParent(Module* module) { module_ = module; } void SetParent(Module* module) { module_ = module; }
@ -59,6 +57,7 @@ class Function {
inline void AddParameter(std::unique_ptr<Instruction> p); inline void AddParameter(std::unique_ptr<Instruction> p);
// Appends a basic block to this function. // Appends a basic block to this function.
inline void AddBasicBlock(std::unique_ptr<BasicBlock> b); inline void AddBasicBlock(std::unique_ptr<BasicBlock> b);
iterator begin() { return iterator(&blocks_, blocks_.begin()); } iterator begin() { return iterator(&blocks_, blocks_.begin()); }
iterator end() { return iterator(&blocks_, blocks_.end()); } iterator end() { return iterator(&blocks_, blocks_.end()); }
const_iterator cbegin() { return const_iterator(&blocks_, blocks_.cbegin()); } const_iterator cbegin() { return const_iterator(&blocks_, blocks_.cbegin()); }
@ -84,6 +83,11 @@ class Function {
Instruction end_inst_; Instruction end_inst_;
}; };
inline Function::Function(std::unique_ptr<Instruction> def_inst)
: module_(nullptr),
def_inst_(std::move(def_inst)),
end_inst_(SpvOpFunctionEnd) {}
inline void Function::AddParameter(std::unique_ptr<Instruction> p) { inline void Function::AddParameter(std::unique_ptr<Instruction> p) {
params_.emplace_back(std::move(p)); params_.emplace_back(std::move(p));
} }

View File

@ -57,6 +57,15 @@ std::vector<Instruction*> Module::GetConstants() {
return insts; return insts;
}; };
std::vector<const Instruction*> Module::GetConstants() const {
std::vector<const Instruction*> insts;
for (uint32_t i = 0; i < types_values_.size(); ++i) {
if (IsConstantInst(types_values_[i]->opcode()))
insts.push_back(types_values_[i].get());
}
return insts;
};
void Module::ForEachInst(const std::function<void(Instruction*)>& f) { void Module::ForEachInst(const std::function<void(Instruction*)>& f) {
for (auto& i : capabilities_) f(i.get()); for (auto& i : capabilities_) f(i.get());
for (auto& i : extensions_) f(i.get()); for (auto& i : extensions_) f(i.get());

View File

@ -94,6 +94,7 @@ class Module {
// Returns a vector of pointers to constant-creation instructions in this // Returns a vector of pointers to constant-creation instructions in this
// module. // module.
std::vector<Instruction*> GetConstants(); std::vector<Instruction*> GetConstants();
std::vector<const Instruction*> GetConstants() const;
// Iterators for debug instructions (excluding OpLine & OpNoLine) contained in // Iterators for debug instructions (excluding OpLine & OpNoLine) contained in
// this module. // this module.