Fix Thrift compiler to support reuse of enums and constants in later constants
authorMark Slee <mcslee@apache.org>
Fri, 27 Jul 2007 22:14:41 +0000 (22:14 +0000)
committerMark Slee <mcslee@apache.org>
Fri, 27 Jul 2007 22:14:41 +0000 (22:14 +0000)
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

compiler/cpp/src/parse/t_const_value.h
compiler/cpp/src/parse/t_scope.h
compiler/cpp/src/thrifty.yy
test/ConstantsDemo.thrift

index 6e8887c..fec2acc 100644 (file)
@@ -30,6 +30,14 @@ class t_const_value {
 
   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;
index 3047e7d..c97f2d6 100644 (file)
@@ -41,6 +41,14 @@ class t_scope {
     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 @@ class t_scope {
   // 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_; 
  
index fe64e85..ce11bf8 100644 (file)
@@ -411,6 +411,12 @@ EnumDef:
       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 @@ Const:
       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 @@ ConstValue:
 | 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 @@ Xception:
       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;
     }
index 1cdc98a..3af9f93 100644 (file)
@@ -5,6 +5,13 @@ struct thing {
   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