Skip to content

Commit

Permalink
move size logic to Type::end() and implement Type::size() on top
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Aug 19, 2020
1 parent 0edaa68 commit eb2a485
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 34 deletions.
34 changes: 20 additions & 14 deletions src/wasm-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ class Type {
Type(std::initializer_list<Type> types);
explicit Type(const std::vector<Type>& types);

// Accessors
size_t size() const;

// Predicates
// Compound Concrete
// Type Basic │ Single│
Expand Down Expand Up @@ -177,14 +174,6 @@ class Type {

std::string toString() const;

const Type& operator[](size_t i) const {
if (isMulti()) {
return (*(std::vector<Type>*)getID())[i];
}
assert(id != Type::none && i == 0 && "Index out of bounds");
return *this;
}

struct Iterator
: std::iterator<std::random_access_iterator_tag, Type, long, Type*, Type&> {
const Type* parent;
Expand All @@ -209,14 +198,31 @@ class Type {
const value_type& operator*() const {
if (parent->isMulti()) {
return (*(std::vector<Type>*)parent->getID())[index];
} else {
// see TODO in Type::end()
assert(index == 0 && parent->id != Type::none && "Index out of bounds");
return *parent;
}
assert(index == 0 && *parent != Type::none && "Index out of bounds");
return *parent;
}
};

Iterator begin() const { return Iterator(this, 0); }
Iterator end() const { return Iterator(this, size()); }
Iterator end() const {
if (isMulti()) {
return Iterator(this, (*(std::vector<Type>*)getID()).size());
} else {
// TODO: unreachable expands to {unreachable} currently. change to {}?
return Iterator(this, size_t(id != Type::none));
}
}
size_t size() const { return end() - begin(); }
const Type& operator[](size_t i) const {
if (isMulti()) {
return (*(std::vector<Type>*)getID())[i];
} else {
return *begin();
}
}
};

// Wrapper type for formatting types as "(param i32 i64 f32)"
Expand Down
20 changes: 0 additions & 20 deletions src/wasm/wasm-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,6 @@ namespace {

std::mutex mutex;

std::array<std::vector<Type>, Type::_last_value_type + 1> basicTypes = {
{{},
{Type::unreachable},
{Type::i32},
{Type::i64},
{Type::f32},
{Type::f64},
{Type::v128},
{Type::funcref},
{Type::externref},
{Type::nullref},
{Type::exnref}}};

// Track unique_ptrs for constructed types to avoid leaks
std::vector<std::unique_ptr<std::vector<Type>>> constructedTypes;

Expand Down Expand Up @@ -123,13 +110,6 @@ Type::Type(std::initializer_list<Type> types) { init(types); }

Type::Type(const std::vector<Type>& types) { init(types); }

size_t Type::size() const {
if (isMulti()) {
return (*(std::vector<Type>*)getID()).size();
}
return size_t(getID() != Type::none);
}

bool Type::operator<(const Type& other) const {
return std::lexicographical_compare((*this).begin(),
(*this).end(),
Expand Down

0 comments on commit eb2a485

Please sign in to comment.