Baseline commit for thrift, which is pillar v2
Reviewed By: aditya
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664711 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/src/parse/t_base_type.h b/compiler/src/parse/t_base_type.h
new file mode 100644
index 0000000..bc96122
--- /dev/null
+++ b/compiler/src/parse/t_base_type.h
@@ -0,0 +1,34 @@
+#ifndef T_BASE_TYPE_H
+#define T_BASE_TYPE_H
+
+#include "t_type.h"
+
+/**
+ * A thrift base type, which must be one of the defined enumerated types.
+ *
+ * @author Mark Slee <mcslee@facebook.com>
+ */
+class t_base_type : public t_type {
+ public:
+ /** Enumeration of thrift base types */
+ enum t_base {
+ TYPE_VOID,
+ TYPE_STRING,
+ TYPE_BYTE,
+ TYPE_I32,
+ TYPE_U32,
+ TYPE_I64,
+ TYPE_U64
+ };
+
+ t_base_type(std::string name, t_base base) :
+ t_type(name), base_(base) {}
+
+ t_base get_base() const { return base_; }
+ bool is_base_type() const { return true; }
+
+ private:
+ t_base base_;
+};
+
+#endif
diff --git a/compiler/src/parse/t_constant.h b/compiler/src/parse/t_constant.h
new file mode 100644
index 0000000..1f3c868
--- /dev/null
+++ b/compiler/src/parse/t_constant.h
@@ -0,0 +1,26 @@
+#ifndef T_CONSTANT_H
+#define T_CONSTANT_H
+
+#include <string>
+
+class t_constant {
+ public:
+ t_constant(std::string name) :
+ name_(name), has_value_(false), value_(0) {}
+
+ t_constant(std::string name, int value) :
+ name_(name), has_value_(true), value_(value) {}
+
+ ~t_constant() {}
+
+ const std::string& get_name() { return name_; }
+ bool has_value() { return has_value_; }
+ int get_value() { return value_; }
+
+ private:
+ std::string name_;
+ bool has_value_;
+ int value_;
+};
+
+#endif
diff --git a/compiler/src/parse/t_enum.h b/compiler/src/parse/t_enum.h
new file mode 100644
index 0000000..f089150
--- /dev/null
+++ b/compiler/src/parse/t_enum.h
@@ -0,0 +1,22 @@
+#ifndef T_ENUM_H
+#define T_ENUM_H
+
+#include "t_constant.h"
+#include <vector>
+
+class t_enum : public t_type {
+ public:
+ t_enum() {}
+ ~t_enum() {}
+
+ void set_name(std::string name) { name_ = name; }
+ void append(t_constant* constant) { constants_.push_back(constant); }
+
+ const std::vector<t_constant*>& get_constants() { return constants_; }
+ bool is_enum() const { return true; }
+
+ private:
+ std::vector<t_constant*> constants_;
+};
+
+#endif
diff --git a/compiler/src/parse/t_field.h b/compiler/src/parse/t_field.h
new file mode 100644
index 0000000..304bb16
--- /dev/null
+++ b/compiler/src/parse/t_field.h
@@ -0,0 +1,29 @@
+#ifndef T_FIELD_H
+#define T_FIELD_H
+
+#include <string>
+
+/**
+ * Class to represent a field in a thrift structure or in a set of arguments
+ * to a thrift function.
+ *
+ * @author Mark Slee <mcslee@facebook.com>
+ */
+class t_field {
+ public:
+ t_field(t_type* type, std::string name, uint32_t key) :
+ type_(type), name_(name), key_(key) {}
+
+ ~t_field() {}
+
+ t_type* get_type() const { return type_; }
+ const std::string& get_name() const { return name_; }
+ uint32_t get_key() const { return key_; }
+
+ private:
+ t_type* type_;
+ std::string name_;
+ uint32_t key_;
+};
+
+#endif
diff --git a/compiler/src/parse/t_function.h b/compiler/src/parse/t_function.h
new file mode 100644
index 0000000..1caf54a
--- /dev/null
+++ b/compiler/src/parse/t_function.h
@@ -0,0 +1,51 @@
+#ifndef T_FUNCTION_H
+#define T_FUNCTION_H
+
+#include <string>
+#include "t_type.h"
+#include "t_struct.h"
+
+/**
+ * Representation of a function. Key parst are return type, function name,
+ * optional modifiers, and an argument list. Each function also has a
+ * hash signature that is used in the network protocol.
+ *
+ * @author Mark Slee <mcslee@facebook.com>
+ */
+class t_function {
+ public:
+ t_function(t_type* returntype, std::string name, t_struct* arglist) :
+ returntype_(returntype), name_(name), arglist_(arglist) {}
+
+ ~t_function() {}
+
+ /**
+ * Implementation of the Fowler / Noll / Vo (FNV) Hash
+ * http://www.isthe.com/chongo/tech/comp/fnv/
+ */
+ static uint32_t fnv32(const char *s) {
+ uint32_t hash = (uint32_t)216613626;
+ while (*s) {
+ hash +=
+ (hash << 1) +
+ (hash << 4) +
+ (hash << 7) +
+ (hash << 8) +
+ (hash << 24);
+ hash ^= *s++;
+ }
+ return hash;
+ }
+
+ t_type* get_returntype() const { return returntype_; }
+ const std::string& get_name() const { return name_; }
+ t_struct* get_arglist() const { return arglist_; }
+ uint32_t get_hash() const { return fnv32(name_.c_str()); }
+
+ private:
+ t_type* returntype_;
+ std::string name_;
+ t_struct* arglist_;
+};
+
+#endif
diff --git a/compiler/src/parse/t_list.h b/compiler/src/parse/t_list.h
new file mode 100644
index 0000000..736ac48
--- /dev/null
+++ b/compiler/src/parse/t_list.h
@@ -0,0 +1,27 @@
+#ifndef T_LIST_H
+#define T_LIST_H
+
+#include "t_field.h"
+#include <vector>
+
+/**
+ * List of elements.
+ *
+ * @author Mark Slee <mcslee@facebook.com>
+ */
+class t_list {
+ public:
+ t_list() {}
+ ~t_list() {}
+
+ /** Add a new field to the list */
+ void append(t_field* elem) { list_.push_back(elem); }
+
+ /** Retrieve the list contents */
+ const std::vector<t_field*>& elems() const { return list_; }
+
+ private:
+ std::vector<t_field*> list_;
+};
+
+#endif
diff --git a/compiler/src/parse/t_program.h b/compiler/src/parse/t_program.h
new file mode 100644
index 0000000..fd35799
--- /dev/null
+++ b/compiler/src/parse/t_program.h
@@ -0,0 +1,90 @@
+#ifndef T_PROGRAM_H
+#define T_PROGRAM_H
+
+#include <string>
+#include <vector>
+
+#include "t_base_type.h"
+#include "t_typedef.h"
+#include "t_enum.h"
+#include "t_struct.h"
+#include "t_service.h"
+
+/**
+ * Top level class representing an entire thrift program. A program consists
+ * fundamentally of the following:
+ *
+ * Typedefs
+ * Enumerations
+ * Structs
+ * Services
+ *
+ * @author Mark Slee <mcslee@facebook.com>
+ */
+class t_program {
+ public:
+ t_program(std::string name) :
+ name_(name) {
+ type_void = new t_base_type("void", t_base_type::TYPE_VOID);
+ type_string = new t_base_type("string", t_base_type::TYPE_STRING);
+ type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
+ type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
+ type_u32 = new t_base_type("u32", t_base_type::TYPE_U32);
+ type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
+ type_u64 = new t_base_type("u64", t_base_type::TYPE_U64);
+ }
+
+ ~t_program() {
+ delete type_string;
+ delete type_byte;
+ delete type_i32;
+ delete type_u32;
+ delete type_i64;
+ delete type_u64;
+ }
+
+ // Name accessor
+ const std::string& get_name() const { return name_; }
+
+ // Accessors for program elements
+ const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; }
+ const std::vector<t_enum*>& get_enums() const { return enums_; }
+ const std::vector<t_struct*>& get_structs() const { return structs_; }
+ const std::vector<t_service*>& get_services() const { return services_; }
+
+ // New program element addition
+ void add_typedef(t_typedef *td) { typedefs_.push_back(td); }
+ void add_enum (t_enum *te) { enums_.push_back(te); }
+ void add_struct (t_struct *ts) { structs_.push_back(ts); }
+ void add_service(t_service *ts) { services_.push_back(ts); }
+
+ // Accessors for global types
+ t_type* get_void_type() const { return type_void; }
+ t_type* get_string_type() const { return type_string; }
+ t_type* get_byte_type() const { return type_byte; }
+ t_type* get_i32_type() const { return type_i32; }
+ t_type* get_u32_type() const { return type_u32; }
+ t_type* get_i64_type() const { return type_i64; }
+ t_type* get_u64_type() const { return type_u64; }
+
+ private:
+ // Name
+ std::string name_;
+
+ // Components
+ std::vector<t_typedef*> typedefs_;
+ std::vector<t_enum*> enums_;
+ std::vector<t_struct*> structs_;
+ std::vector<t_service*> services_;
+
+ // Global base types
+ t_type* type_void;
+ t_type* type_string;
+ t_type* type_byte;
+ t_type* type_i32;
+ t_type* type_u32;
+ t_type* type_i64;
+ t_type* type_u64;
+};
+
+#endif
diff --git a/compiler/src/parse/t_service.h b/compiler/src/parse/t_service.h
new file mode 100644
index 0000000..92e59b2
--- /dev/null
+++ b/compiler/src/parse/t_service.h
@@ -0,0 +1,23 @@
+#ifndef T_SERVICE_H
+#define T_SERVICE_H
+
+#include "t_function.h"
+#include <vector>
+
+class t_service {
+ public:
+ t_service() {}
+ ~t_service() {}
+
+ void set_name(std::string name) { name_ = name; }
+ void add_function(t_function* func) { functions_.push_back(func); }
+
+ const std::string& get_name() const { return name_; }
+ const std::vector<t_function*>& get_functions() const { return functions_; }
+
+ private:
+ std::string name_;
+ std::vector<t_function*> functions_;
+};
+
+#endif
diff --git a/compiler/src/parse/t_struct.h b/compiler/src/parse/t_struct.h
new file mode 100644
index 0000000..fcc00e7
--- /dev/null
+++ b/compiler/src/parse/t_struct.h
@@ -0,0 +1,23 @@
+#ifndef T_STRUCT_H
+#define T_STRUCT_H
+
+#include <vector>
+#include <string>
+
+#include "t_type.h"
+#include "t_list.h"
+
+class t_struct : public t_type {
+ public:
+ t_struct(std::string name, t_list* members) :
+ t_type(name), members_(members) {}
+ ~t_struct() {}
+
+ t_list* get_members() { return members_; }
+ bool is_struct() { return true; }
+
+ private:
+ t_list* members_;
+};
+
+#endif
diff --git a/compiler/src/parse/t_type.h b/compiler/src/parse/t_type.h
new file mode 100644
index 0000000..436c59e
--- /dev/null
+++ b/compiler/src/parse/t_type.h
@@ -0,0 +1,31 @@
+#ifndef T_TYPE_H
+#define T_TYPE_H
+
+#include <string>
+
+/**
+ * Generic representation of a thrift type.
+ *
+ * @author Mark Slee <mcslee@facebook.com>
+ */
+class t_type {
+ public:
+ virtual ~t_type() {}
+
+ virtual const std::string& get_name() const { return name_; }
+
+ virtual bool is_base_type() const { return false; }
+ virtual bool is_typedef() const { return false; }
+ virtual bool is_enum() const { return false; }
+ virtual bool is_struct() const { return false; }
+
+ protected:
+ t_type() {}
+
+ t_type(std::string name) :
+ name_(name) {}
+
+ std::string name_;
+};
+
+#endif
diff --git a/compiler/src/parse/t_typedef.h b/compiler/src/parse/t_typedef.h
new file mode 100644
index 0000000..97284be
--- /dev/null
+++ b/compiler/src/parse/t_typedef.h
@@ -0,0 +1,31 @@
+#ifndef T_TYPEDEF_H
+#define T_TYPEDEF_H
+
+#include <string>
+#include "t_type.h"
+
+/**
+ * A typedef is a mapping from a symbolic name to another type.
+ *
+ * @author Mark Slee <mcslee@facebook.com>
+ */
+class t_typedef : public t_type {
+ public:
+ t_typedef(t_type* type, std::string symbolic) :
+ t_type(symbolic), type_(type), symbolic_(symbolic) {}
+
+ ~t_typedef() {}
+
+ t_type* get_type() const { return type_; }
+ const std::string& get_symbolic() const { return symbolic_; }
+ bool is_typedef() const { return true; }
+
+ private:
+ /** Type that this definition inherits from */
+ t_type* type_;
+
+ /** Symbolic name for this type */
+ std::string symbolic_;
+};
+
+#endif