From 41d3058dbf45d3e771192052f1f15ba1b6daf5d3 Mon Sep 17 00:00:00 2001 From: David Reiss Date: Tue, 5 Oct 2010 16:39:29 +0000 Subject: [PATCH] THRIFT-868. Make const values work properly with typdefs Just requires calling get_true_type in the right spot. Because "the right spot" is under src/parse, get_true_type had to be moed from t_generator to t_type. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@1004703 13f79535-47bb-0310-9956-ffa450edef68 --- compiler/cpp/src/generate/t_generator.h | 5 +---- compiler/cpp/src/parse/parse.cc | 9 +++++++++ compiler/cpp/src/parse/t_scope.h | 11 +++++++---- compiler/cpp/src/parse/t_type.h | 1 + 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/compiler/cpp/src/generate/t_generator.h b/compiler/cpp/src/generate/t_generator.h index 8c156f92..458922ff 100644 --- a/compiler/cpp/src/generate/t_generator.h +++ b/compiler/cpp/src/generate/t_generator.h @@ -237,10 +237,7 @@ class t_generator { * Get the true type behind a series of typedefs. */ static t_type* get_true_type(t_type* type) { - while (type->is_typedef()) { - type = ((t_typedef*)type)->get_type(); - } - return type; + return type->get_true_type(); } protected: diff --git a/compiler/cpp/src/parse/parse.cc b/compiler/cpp/src/parse/parse.cc index 2570c37b..a6556525 100644 --- a/compiler/cpp/src/parse/parse.cc +++ b/compiler/cpp/src/parse/parse.cc @@ -1,4 +1,5 @@ #include "t_type.h" +#include "t_typedef.h" #include "md5.h" @@ -9,3 +10,11 @@ void t_type::generate_fingerprint() { md5_append(&ctx, (md5_byte_t*)(material.data()), (int)material.size()); md5_finish(&ctx, (md5_byte_t*)fingerprint_); } + +t_type* t_type::get_true_type() { + t_type* type = this; + while (type->is_typedef()) { + type = ((t_typedef*)type)->get_type(); + } + return type; +} diff --git a/compiler/cpp/src/parse/t_scope.h b/compiler/cpp/src/parse/t_scope.h index 01894aa8..4617bf8d 100644 --- a/compiler/cpp/src/parse/t_scope.h +++ b/compiler/cpp/src/parse/t_scope.h @@ -115,8 +115,11 @@ class t_scope { throw "No enum value or constant found named \"" + const_val->get_identifier() + "\"!"; } - if (constant->get_type()->is_base_type()) { - switch (((t_base_type*)constant->get_type())->get_base()) { + // Resolve typedefs to the underlying type + t_type* const_type = constant->get_type()->get_true_type(); + + if (const_type->is_base_type()) { + switch (((t_base_type*)const_type)->get_base()) { case t_base_type::TYPE_I16: case t_base_type::TYPE_I32: case t_base_type::TYPE_I64: @@ -133,7 +136,7 @@ class t_scope { case t_base_type::TYPE_VOID: throw "Constants cannot be of type VOID"; } - } else if (constant->get_type()->is_map()) { + } else if (const_type->is_map()) { const std::map& map = constant->get_value()->get_map(); std::map::const_iterator v_iter; @@ -141,7 +144,7 @@ class t_scope { for (v_iter = map.begin(); v_iter != map.end(); ++v_iter) { const_val->add_map(v_iter->first, v_iter->second); } - } else if (constant->get_type()->is_list()) { + } else if (const_type->is_list()) { const std::vector& val = constant->get_value()->get_list(); std::vector::const_iterator v_iter; diff --git a/compiler/cpp/src/parse/t_type.h b/compiler/cpp/src/parse/t_type.h index 0941a264..f0242795 100644 --- a/compiler/cpp/src/parse/t_type.h +++ b/compiler/cpp/src/parse/t_type.h @@ -66,6 +66,7 @@ class t_type : public t_doc { return program_; } + t_type* get_true_type(); // Return a string that uniquely identifies this type // from any other thrift type in the world, as far as -- 2.17.1