Thrift compiler frontend support for constants
Summary: The parser now accepts constants and adds them into the parse tree
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664880 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/parse/t_const.h b/compiler/cpp/src/parse/t_const.h
new file mode 100644
index 0000000..77d7e18
--- /dev/null
+++ b/compiler/cpp/src/parse/t_const.h
@@ -0,0 +1,41 @@
+#ifndef T_CONST_H
+#define T_CONST_H
+
+#include "t_type.h"
+#include "t_const_value.h"
+
+/**
+ * A const is a constant value defined across languages that has a type and
+ * a value. The trick here is that the declared type might not match the type
+ * of the value object, since that is not determined until after parsing the
+ * whole thing out.
+ *
+ * @author Mark Slee <mcslee@facebook.com>
+ */
+class t_const {
+ public:
+ t_const(t_type* type, std::string name, t_const_value* value) :
+ type_(type),
+ name_(name),
+ value_(value) {}
+
+ t_type* get_type() const {
+ return type_;
+ }
+
+ std::string get_name() const {
+ return name_;
+ }
+
+ t_const_value* get_value() const {
+ return value_;
+ }
+
+ private:
+ t_type* type_;
+ std::string name_;
+ t_const_value* value_;
+};
+
+#endif
+
diff --git a/compiler/cpp/src/parse/t_enum.h b/compiler/cpp/src/parse/t_enum.h
index 002ca82..5fe6f89 100644
--- a/compiler/cpp/src/parse/t_enum.h
+++ b/compiler/cpp/src/parse/t_enum.h
@@ -1,11 +1,11 @@
#ifndef T_ENUM_H
#define T_ENUM_H
-#include "t_constant.h"
+#include "t_enum_value.h"
#include <vector>
/**
- * An enumerated type. A list of t_constant objects with a name for the type.
+ * An enumerated type. A list of constant objects with a name for the type.
*
* @author Mark Slee <mcslee@facebook.com>
*/
@@ -18,11 +18,11 @@
name_ = name;
}
- void append(t_constant* constant) {
+ void append(t_enum_value* constant) {
constants_.push_back(constant);
}
- const std::vector<t_constant*>& get_constants() {
+ const std::vector<t_enum_value*>& get_constants() {
return constants_;
}
@@ -31,7 +31,7 @@
}
private:
- std::vector<t_constant*> constants_;
+ std::vector<t_enum_value*> constants_;
};
#endif
diff --git a/compiler/cpp/src/parse/t_constant.h b/compiler/cpp/src/parse/t_enum_value.h
similarity index 77%
rename from compiler/cpp/src/parse/t_constant.h
rename to compiler/cpp/src/parse/t_enum_value.h
index f502f75..65e61cb 100644
--- a/compiler/cpp/src/parse/t_constant.h
+++ b/compiler/cpp/src/parse/t_enum_value.h
@@ -1,5 +1,5 @@
-#ifndef T_CONSTANT_H
-#define T_CONSTANT_H
+#ifndef T_ENUM_VALUE_H
+#define T_ENUM_VALUE_H
#include <string>
@@ -10,19 +10,19 @@
*
* @author Mark Slee <mcslee@facebook.com>
*/
-class t_constant {
+class t_enum_value {
public:
- t_constant(std::string name) :
+ t_enum_value(std::string name) :
name_(name),
has_value_(false),
value_(0) {}
- t_constant(std::string name, int value) :
+ t_enum_value(std::string name, int value) :
name_(name),
has_value_(true),
value_(value) {}
- ~t_constant() {}
+ ~t_enum_value() {}
const std::string& get_name() {
return name_;
diff --git a/compiler/cpp/src/parse/t_program.h b/compiler/cpp/src/parse/t_program.h
index a3270ad..b0eb60e 100644
--- a/compiler/cpp/src/parse/t_program.h
+++ b/compiler/cpp/src/parse/t_program.h
@@ -12,6 +12,7 @@
#include "t_base_type.h"
#include "t_typedef.h"
#include "t_enum.h"
+#include "t_const.h"
#include "t_struct.h"
#include "t_service.h"
#include "t_list.h"
@@ -24,6 +25,7 @@
*
* Typedefs
* Enumerations
+ * Constants
* Structs
* Exceptions
* Services
@@ -58,6 +60,7 @@
// 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_const*>& get_consts() const { return consts_; }
const std::vector<t_struct*>& get_structs() const { return structs_; }
const std::vector<t_struct*>& get_xceptions() const { return xceptions_; }
const std::vector<t_service*>& get_services() const { return services_; }
@@ -65,6 +68,7 @@
// Program elements
void add_typedef (t_typedef* td) { typedefs_.push_back(td); }
void add_enum (t_enum* te) { enums_.push_back(te); }
+ void add_const (t_const* tc) { consts_.push_back(tc); }
void add_struct (t_struct* ts) { structs_.push_back(ts); }
void add_xception (t_struct* tx) { xceptions_.push_back(tx); }
void add_service (t_service* ts) { services_.push_back(ts); }
@@ -139,6 +143,7 @@
// Components to generate code for
std::vector<t_typedef*> typedefs_;
std::vector<t_enum*> enums_;
+ std::vector<t_const*> consts_;
std::vector<t_struct*> structs_;
std::vector<t_struct*> xceptions_;
std::vector<t_service*> services_;