From: David Reiss Date: Thu, 1 May 2008 05:52:50 +0000 (+0000) Subject: Properly handle constants of typedef'ed types. X-Git-Tag: 0.2.0~797 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=9a4edfa07fe0c09865015a15d26695823cd4a5fd;p=common%2Fthrift.git Properly handle constants of typedef'ed types. Also throw an error in the compiler if we cannot generate a constant for a declared const because of its type. Added a test of this functionality in ConstantsDemo.thrift. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665675 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/compiler/cpp/src/generate/t_cocoa_generator.cc b/compiler/cpp/src/generate/t_cocoa_generator.cc index 759b25cb..9814aa06 100644 --- a/compiler/cpp/src/generate/t_cocoa_generator.cc +++ b/compiler/cpp/src/generate/t_cocoa_generator.cc @@ -1788,6 +1788,7 @@ string t_cocoa_generator::render_const_value(string name, t_type* type, t_const_value* value, bool containerize_it) { + type = get_true_type(type); std::ostringstream render; if (type->is_base_type()) { diff --git a/compiler/cpp/src/generate/t_cpp_generator.cc b/compiler/cpp/src/generate/t_cpp_generator.cc index ef162432..0a1d4def 100644 --- a/compiler/cpp/src/generate/t_cpp_generator.cc +++ b/compiler/cpp/src/generate/t_cpp_generator.cc @@ -481,6 +481,7 @@ void t_cpp_generator::generate_consts(std::vector consts) { * validate_types method in main.cc */ void t_cpp_generator::print_const_value(ofstream& out, string name, t_type* type, t_const_value* value) { + type = get_true_type(type); if (type->is_base_type()) { string v2 = render_const_value(out, name, type, value); indent(out) << name << " = " << v2 << ";" << endl << @@ -537,6 +538,8 @@ void t_cpp_generator::print_const_value(ofstream& out, string name, t_type* type indent(out) << name << ".insert(" << val << ");" << endl; } out << endl; + } else { + throw "INVALID TYPE IN print_const_value: " + type->get_name(); } } diff --git a/compiler/cpp/src/generate/t_csharp_generator.cc b/compiler/cpp/src/generate/t_csharp_generator.cc index f4fd543d..ec5d90b7 100644 --- a/compiler/cpp/src/generate/t_csharp_generator.cc +++ b/compiler/cpp/src/generate/t_csharp_generator.cc @@ -290,6 +290,7 @@ void t_csharp_generator::print_const_constructor(std::ofstream& out, std::vector //it seems like all that methods that call this are using in_static to be the opposite of what it would imply bool t_csharp_generator::print_const_value(std::ofstream& out, string name, t_type* type, t_const_value* value, bool in_static, bool defval, bool needtype) { + type = get_true_type(type); indent(out); bool need_static_construction = !in_static; if (!defval || needtype) { @@ -310,6 +311,8 @@ bool t_csharp_generator::print_const_value(std::ofstream& out, string name, t_ty out << name << " = new " << type_name(type, true, true) << "();" << endl; } else if (type->is_list() || type->is_set()) { out << name << " = new " << type_name(type) << "();" << endl; + } else { + throw "compiler error: no const of type " + type->get_name(); } if (defval && !type->is_base_type() && !type->is_enum()) { diff --git a/compiler/cpp/src/generate/t_erl_generator.cc b/compiler/cpp/src/generate/t_erl_generator.cc index 02d73614..069d4b49 100644 --- a/compiler/cpp/src/generate/t_erl_generator.cc +++ b/compiler/cpp/src/generate/t_erl_generator.cc @@ -175,6 +175,7 @@ void t_erl_generator::generate_const(t_const* tconst) { * validate_types method in main.cc */ string t_erl_generator::render_const_value(t_type* type, t_const_value* value) { + type = get_true_type(type); std::ostringstream out; if (type->is_base_type()) { @@ -291,6 +292,8 @@ string t_erl_generator::render_const_value(t_type* type, t_const_value* value) { out << render_const_value(etype, *v_iter); } out << "]"; + } else { + throw "CANNOT GENERATE CONSTANT FOR TYPE: " + type->get_name(); } return out.str(); } diff --git a/compiler/cpp/src/generate/t_hs_generator.cc b/compiler/cpp/src/generate/t_hs_generator.cc index fec7d3f9..d1e3f9e2 100644 --- a/compiler/cpp/src/generate/t_hs_generator.cc +++ b/compiler/cpp/src/generate/t_hs_generator.cc @@ -351,6 +351,7 @@ void t_hs_generator::generate_const(t_const* tconst) { * validate_types method in main.cc */ string t_hs_generator::render_const_value(t_type* type, t_const_value* value) { + type = get_true_type(type); std::ostringstream out; if (type->is_base_type()) { t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); @@ -462,6 +463,8 @@ string t_hs_generator::render_const_value(t_type* type, t_const_value* value) { out << val; } out << "])"; + } else { + throw "CANNOT GENERATE CONSTANT FOR TYPE: " + type->get_name(); } return out.str(); } diff --git a/compiler/cpp/src/generate/t_java_generator.cc b/compiler/cpp/src/generate/t_java_generator.cc index f0f6b497..c0c3e997 100644 --- a/compiler/cpp/src/generate/t_java_generator.cc +++ b/compiler/cpp/src/generate/t_java_generator.cc @@ -350,6 +350,7 @@ void t_java_generator::generate_consts(std::vector consts) { * validate_types method in main.cc */ void t_java_generator::print_const_value(std::ofstream& out, string name, t_type* type, t_const_value* value, bool in_static, bool defval) { + type = get_true_type(type); indent(out); if (!defval) { @@ -434,10 +435,13 @@ void t_java_generator::print_const_value(std::ofstream& out, string name, t_type indent(out) << "}" << endl; } out << endl; + } else { + throw "compiler error: no const of type " + type->get_name(); } } string t_java_generator::render_const_value(ofstream& out, string name, t_type* type, t_const_value* value) { + type = get_true_type(type); std::ostringstream render; if (type->is_base_type()) { diff --git a/compiler/cpp/src/generate/t_ocaml_generator.cc b/compiler/cpp/src/generate/t_ocaml_generator.cc index 7178fc4a..e38f7b18 100644 --- a/compiler/cpp/src/generate/t_ocaml_generator.cc +++ b/compiler/cpp/src/generate/t_ocaml_generator.cc @@ -356,6 +356,7 @@ void t_ocaml_generator::generate_const(t_const* tconst) { * validate_types method in main.cc */ string t_ocaml_generator::render_const_value(t_type* type, t_const_value* value) { + type = get_true_type(type); std::ostringstream out; if (type->is_base_type()) { t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); @@ -476,6 +477,8 @@ string t_ocaml_generator::render_const_value(t_type* type, t_const_value* value) indent(out) << hm << ")" << endl; indent_down(); out << endl; + } else { + throw "CANNOT GENERATE CONSTANT FOR TYPE: " + type->get_name(); } return out.str(); } diff --git a/compiler/cpp/src/generate/t_py_generator.cc b/compiler/cpp/src/generate/t_py_generator.cc index 9eac2f28..d2f2e0d9 100644 --- a/compiler/cpp/src/generate/t_py_generator.cc +++ b/compiler/cpp/src/generate/t_py_generator.cc @@ -356,6 +356,7 @@ void t_py_generator::generate_const(t_const* tconst) { * validate_types method in main.cc */ string t_py_generator::render_const_value(t_type* type, t_const_value* value) { + type = get_true_type(type); std::ostringstream out; if (type->is_base_type()) { @@ -450,6 +451,8 @@ string t_py_generator::render_const_value(t_type* type, t_const_value* value) { if (type->is_set()) { out << ")"; } + } else { + throw "CANNOT GENERATE CONSTANT FOR TYPE: " + type->get_name(); } return out.str(); diff --git a/compiler/cpp/src/generate/t_rb_generator.cc b/compiler/cpp/src/generate/t_rb_generator.cc index 395bb642..c3d1d419 100644 --- a/compiler/cpp/src/generate/t_rb_generator.cc +++ b/compiler/cpp/src/generate/t_rb_generator.cc @@ -319,6 +319,7 @@ void t_rb_generator::generate_const(t_const* tconst) { * validate_types method in main.cc */ string t_rb_generator::render_const_value(t_type* type, t_const_value* value) { + type = get_true_type(type); std::ostringstream out; if (type->is_base_type()) { t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); @@ -417,6 +418,8 @@ string t_rb_generator::render_const_value(t_type* type, t_const_value* value) { } else { indent(out) << "]"; } + } else { + throw "CANNOT GENERATE CONSTANT FOR TYPE: " + type->get_name(); } return out.str(); } diff --git a/compiler/cpp/src/generate/t_st_generator.cc b/compiler/cpp/src/generate/t_st_generator.cc index cf93bdc0..1ce20f22 100644 --- a/compiler/cpp/src/generate/t_st_generator.cc +++ b/compiler/cpp/src/generate/t_st_generator.cc @@ -343,6 +343,7 @@ void t_st_generator::generate_const(t_const* tconst) { * validate_types method in main.cc */ string t_st_generator::render_const_value(t_type* type, t_const_value* value) { + type = get_true_type(type); std::ostringstream out; if (type->is_base_type()) { t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); @@ -439,6 +440,8 @@ string t_st_generator::render_const_value(t_type* type, t_const_value* value) { out << indent() << indent() << "yourself)"; indent_down(); indent_down(); + } else { + throw "CANNOT GENERATE CONSTANT FOR TYPE: " + type->get_name(); } return out.str(); } diff --git a/test/ConstantsDemo.thrift b/test/ConstantsDemo.thrift index 0b9d8396..dc12bb06 100644 --- a/test/ConstantsDemo.thrift +++ b/test/ConstantsDemo.thrift @@ -14,6 +14,9 @@ struct thing2 { 1: constants val = TWO } +typedef i32 myIntType +const myIntType myInt = 3 + const map GEN_ENUM_NAMES = {ONE : "HOWDY", TWO: PARTNER} const i32 hex_const = 0x0001F