Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCCA enum and union support #705

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 170 additions & 16 deletions include/occa/dtype/dtype.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

namespace occa {
class dtype_t;
class dtypeTuple_t;
class dtypeEnum_t;
class dtypeStruct_t;
class dtypeTuple_t;
class dtypeUnion_t;
class json;

typedef std::map<std::string, const dtype_t*> dtypeGlobalMap_t;
Expand Down Expand Up @@ -40,8 +42,11 @@ namespace occa {
int bytes_;
bool registered;

dtypeTuple_t *tuple_;
dtypeEnum_t *enum_;
dtypeStruct_t *struct_;
dtypeTuple_t *tuple_;
dtypeUnion_t *union_;

mutable dtypeVector_t flatDtype;

public:
Expand Down Expand Up @@ -99,27 +104,47 @@ namespace occa {

bool isRegistered() const;

// Tuple methods
// Enum methods
/**
* @startDoc{isTuple}
* @startDoc{isEnum}
*
* Description:
* Returns `true` if the data type holds a tuple type.
* For example: `occa::dtype::int2` is a tuple of two `int`s
* Returns `true` if the data type represents a enum.
* It's different that a tuple since it can keep distinct data types in its fields.
*
* @endDoc
*/
bool isTuple() const;
bool isEnum() const;

/**
* @startDoc{tupleSize}
* @startDoc{enumEnumeratorCount}
*
* Description:
* Return how big the tuple is, for example `int2` would return `2`
* Returns how many enumerator are defined in the enum
*
* @endDoc
*/
int tupleSize() const;
int enumEnumeratorCount() const;

/**
* @startDoc{enumEnumeratorNames}
*
* Description:
* Return the list of enumerator names for the enum
*
* @endDoc
*/
const strVector& enumEnumeratorNames() const;

/**
* @startDoc{addEnumerator}
*
* Description:
* Add a enumerator to the enum type
*
* @endDoc
*/
dtype_t& addEnumerator(const std::string &enumerator);

// Struct methods
/**
Expand Down Expand Up @@ -153,6 +178,61 @@ namespace occa {
*/
const strVector& structFieldNames() const;

// Tuple methods
/**
* @startDoc{isTuple}
*
* Description:
* Returns `true` if the data type holds a tuple type.
* For example: `occa::dtype::int2` is a tuple of two `int`s
*
* @endDoc
*/
bool isTuple() const;

/**
* @startDoc{tupleSize}
*
* Description:
* Return how big the tuple is, for example `int2` would return `2`
*
* @endDoc
*/
int tupleSize() const;

// Union methods
/**
* @startDoc{isUnion}
*
* Description:
* Returns `true` if the data type represents a union.
* It's different that a tuple since it can keep distinct data types in its fields.
*
* @endDoc
*/
bool isUnion() const;

/**
* @startDoc{unionFieldCount}
*
* Description:
* Returns how many fields are defined in the union
*
* @endDoc
*/
int unionFieldCount() const;

/**
* @startDoc{unionFieldNames}
*
* Description:
* Return the list of field names for the union
*
* @endDoc
*/
const strVector& unionFieldNames() const;


/**
* @startDoc{operator_bracket[0]}
*
Expand Down Expand Up @@ -186,7 +266,21 @@ namespace occa {
const int tupleSize_ = 1);

// Dtype methods
/**
* @startDoc{setFlattenedDtype}
*
* Description:
* Add flatten dtypes of each field.
* @endDoc
*/
void setFlattenedDtype() const;
/**
* @startDoc{addFlatDtypes}
*
* Description:
* Add dtypes of each field.
* @endDoc
*/
void addFlatDtypes(dtypeVector_t &vec) const;

/**
Expand Down Expand Up @@ -260,6 +354,66 @@ namespace occa {
const dtype_t &dtype);


//---[ Enum ]-----------------------
class dtypeEnum_t {
friend class dtype_t;

private:
strVector enumeratorNames;

dtypeEnum_t();

dtypeEnum_t* clone() const;

bool matches(const dtypeEnum_t &other) const;

int enumeratorCount() const;

const dtype_t& operator [] (const int enumerator) const;
const dtype_t& operator [] (const std::string &enumerator) const;

void addEnumerator(const std::string &enumerator);

void toJson(json &j, const std::string &name = "") const;
static dtypeEnum_t fromJson(const json &j);

std::string toString(const std::string &varName = "") const;
};
//====================================


//---[ Struct ]-----------------------
class dtypeStruct_t {
friend class dtype_t;

private:
strVector fieldNames;
dtypeNameMap_t fieldTypes;

dtypeStruct_t();

dtypeStruct_t* clone() const;

bool matches(const dtypeStruct_t &other) const;

int fieldCount() const;

const dtype_t& operator [] (const int field) const;
const dtype_t& operator [] (const std::string &field) const;

void addField(const std::string &field,
const dtype_t &dtype);

void addFlatDtypes(dtypeVector_t &vec) const;

void toJson(json &j, const std::string &name = "") const;
static dtypeStruct_t fromJson(const json &j);

std::string toString(const std::string &varName = "") const;
};
//====================================


//---[ Tuple ]------------------------
class dtypeTuple_t {
friend class dtype_t;
Expand All @@ -285,19 +439,19 @@ namespace occa {
//====================================


//---[ Struct ]-----------------------
class dtypeStruct_t {
//---[ Union ]-----------------------
class dtypeUnion_t {
friend class dtype_t;

private:
strVector fieldNames;
dtypeNameMap_t fieldTypes;

dtypeStruct_t();
dtypeUnion_t();

dtypeStruct_t* clone() const;
dtypeUnion_t* clone() const;

bool matches(const dtypeStruct_t &other) const;
bool matches(const dtypeUnion_t &other) const;

int fieldCount() const;

Expand All @@ -310,7 +464,7 @@ namespace occa {
void addFlatDtypes(dtypeVector_t &vec) const;

void toJson(json &j, const std::string &name = "") const;
static dtypeStruct_t fromJson(const json &j);
static dtypeUnion_t fromJson(const json &j);

std::string toString(const std::string &varName = "") const;
};
Expand Down
Loading