From d0767c579673c868d1ae15e5979a9301ac1347d7 Mon Sep 17 00:00:00 2001 From: Mark Slee Date: Fri, 27 Jul 2007 22:14:41 +0000 Subject: [PATCH] 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 --- compiler/cpp/src/parse/t_const_value.h | 8 +++++++ compiler/cpp/src/parse/t_scope.h | 11 +++++++++ compiler/cpp/src/thrifty.yy | 31 ++++++++++++++++++-------- test/ConstantsDemo.thrift | 7 ++++++ 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/compiler/cpp/src/parse/t_const_value.h b/compiler/cpp/src/parse/t_const_value.h index 6e8887c7..fec2acc4 100644 --- a/compiler/cpp/src/parse/t_const_value.h +++ b/compiler/cpp/src/parse/t_const_value.h @@ -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; diff --git a/compiler/cpp/src/parse/t_scope.h b/compiler/cpp/src/parse/t_scope.h index 3047e7d3..c97f2d62 100644 --- a/compiler/cpp/src/parse/t_scope.h +++ b/compiler/cpp/src/parse/t_scope.h @@ -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::iterator iter; for (iter = types_.begin(); iter != types_.end(); ++iter) { @@ -55,6 +63,9 @@ class t_scope { // Map of names to types std::map types_; + // Map of names to constants + std::map constants_; + // Map of names to services std::map services_; diff --git a/compiler/cpp/src/thrifty.yy b/compiler/cpp/src/thrifty.yy index fe64e853..ce11bf86 100644 --- a/compiler/cpp/src/thrifty.yy +++ b/compiler/cpp/src/thrifty.yy @@ -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; } diff --git a/test/ConstantsDemo.thrift b/test/ConstantsDemo.thrift index 1cdc98ab..3af9f932 100644 --- a/test/ConstantsDemo.thrift +++ b/test/ConstantsDemo.thrift @@ -5,6 +5,13 @@ struct thing { 2: i32 goodbye } +enum constants { + ONE = 1, + TWO = 2 +} + +const map GEN_ENUM_NAMES = {ONE : "HOWDY", TWO: PARTNER} + const i32 hex_const = 0x0001F const i32 GEN_ME = -3523553 -- 2.17.1