Fix Thrift compiler to support reuse of enums and constants in later constants
Summary: So for example, you can make a constant map of enum values to string identifiers.
Reviewed By: dreiss
Test Plan: ConstantsDemo.thrift in the test folder
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665181 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/parse/t_const_value.h b/compiler/cpp/src/parse/t_const_value.h
index 6e8887c..fec2acc 100644
--- a/compiler/cpp/src/parse/t_const_value.h
+++ b/compiler/cpp/src/parse/t_const_value.h
@@ -30,6 +30,14 @@
t_const_value() {}
+ t_const_value(int val) {
+ set_integer(val);
+ }
+
+ t_const_value(std::string val) {
+ set_string(val);
+ }
+
void set_string(std::string val) {
valType_ = CV_STRING;
stringVal_ = val;
diff --git a/compiler/cpp/src/parse/t_scope.h b/compiler/cpp/src/parse/t_scope.h
index 3047e7d..c97f2d6 100644
--- a/compiler/cpp/src/parse/t_scope.h
+++ b/compiler/cpp/src/parse/t_scope.h
@@ -41,6 +41,14 @@
return services_[name];
}
+ void add_constant(std::string name, t_const* constant) {
+ constants_[name] = constant;
+ }
+
+ t_const* get_constant(std::string name) {
+ return constants_[name];
+ }
+
void print() {
std::map<std::string, t_type*>::iterator iter;
for (iter = types_.begin(); iter != types_.end(); ++iter) {
@@ -55,6 +63,9 @@
// Map of names to types
std::map<std::string, t_type*> types_;
+ // Map of names to constants
+ std::map<std::string, t_const*> constants_;
+
// Map of names to services
std::map<std::string, t_service*> services_;
diff --git a/compiler/cpp/src/thrifty.yy b/compiler/cpp/src/thrifty.yy
index fe64e85..ce11bf8 100644
--- a/compiler/cpp/src/thrifty.yy
+++ b/compiler/cpp/src/thrifty.yy
@@ -411,6 +411,12 @@
if ($1 != NULL) {
$$->set_doc($1);
}
+ if (g_parse_mode == PROGRAM) {
+ g_scope->add_constant($2, new t_const(g_type_i32, $2, new t_const_value($4)));
+ if (g_parent_scope != NULL) {
+ g_parent_scope->add_constant(g_parent_prefix + $2, new t_const(g_type_i32, $2, new t_const_value($4)));
+ }
+ }
}
|
DocTextOptional tok_identifier CommaOrSemicolonOptional
@@ -460,6 +466,12 @@
if (g_parse_mode == PROGRAM) {
$$ = new t_const($2, $3, $5);
validate_const_type($$);
+
+ g_scope->add_constant($3, $$);
+ if (g_parent_scope != NULL) {
+ g_parent_scope->add_constant(g_parent_prefix + $3, $$);
+ }
+
} else {
$$ = NULL;
}
@@ -481,14 +493,20 @@
| tok_literal
{
pdebug("ConstValue => tok_literal");
- $$ = new t_const_value();
- $$->set_string($1);
+ $$ = new t_const_value($1);
}
| tok_identifier
{
pdebug("ConstValue => tok_identifier");
- $$ = new t_const_value();
- $$->set_string($1);
+ t_const* constant = g_scope->get_constant($1);
+ if (constant != NULL) {
+ $$ = constant->get_value();
+ } else {
+ if (g_parse_mode == PROGRAM) {
+ pwarning(1, "Constant strings should be quoted: %s\n", $1);
+ }
+ $$ = new t_const_value($1);
+ }
}
| ConstList
{
@@ -602,11 +620,6 @@
pdebug("Xception -> tok_xception tok_identifier { FieldList }");
$4->set_name($2);
$4->set_xception(true);
-/*
- if ($4 != NULL) {
- $5->set_doc($4);
- }
-*/
$$ = $4;
y_field_val = -1;
}
diff --git a/test/ConstantsDemo.thrift b/test/ConstantsDemo.thrift
index 1cdc98a..3af9f93 100644
--- a/test/ConstantsDemo.thrift
+++ b/test/ConstantsDemo.thrift
@@ -5,6 +5,13 @@
2: i32 goodbye
}
+enum constants {
+ ONE = 1,
+ TWO = 2
+}
+
+const map<i32,string> GEN_ENUM_NAMES = {ONE : "HOWDY", TWO: PARTNER}
+
const i32 hex_const = 0x0001F
const i32 GEN_ME = -3523553