Properly handle constants of typedef'ed types.
authorDavid Reiss <dreiss@apache.org>
Thu, 1 May 2008 05:52:50 +0000 (05:52 +0000)
committerDavid Reiss <dreiss@apache.org>
Thu, 1 May 2008 05:52:50 +0000 (05:52 +0000)
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

compiler/cpp/src/generate/t_cocoa_generator.cc
compiler/cpp/src/generate/t_cpp_generator.cc
compiler/cpp/src/generate/t_csharp_generator.cc
compiler/cpp/src/generate/t_erl_generator.cc
compiler/cpp/src/generate/t_hs_generator.cc
compiler/cpp/src/generate/t_java_generator.cc
compiler/cpp/src/generate/t_ocaml_generator.cc
compiler/cpp/src/generate/t_py_generator.cc
compiler/cpp/src/generate/t_rb_generator.cc
compiler/cpp/src/generate/t_st_generator.cc
test/ConstantsDemo.thrift

index 759b25c..9814aa0 100644 (file)
@@ -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()) {
index ef16243..0a1d4de 100644 (file)
@@ -481,6 +481,7 @@ void t_cpp_generator::generate_consts(std::vector<t_const*> 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();
   }
 }
 
index f4fd543..ec5d90b 100644 (file)
@@ -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()) {
index 02d7361..069d4b4 100644 (file)
@@ -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();
 }
index fec7d3f..d1e3f9e 100644 (file)
@@ -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();
 }
index f0f6b49..c0c3e99 100644 (file)
@@ -350,6 +350,7 @@ void t_java_generator::generate_consts(std::vector<t_const*> 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()) {
index 7178fc4..e38f7b1 100644 (file)
@@ -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();
 }
index 9eac2f2..d2f2e0d 100644 (file)
@@ -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();
index 395bb64..c3d1d41 100644 (file)
@@ -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();
 }
index cf93bdc..1ce20f2 100644 (file)
@@ -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();
 }
index 0b9d839..dc12bb0 100644 (file)
@@ -14,6 +14,9 @@ struct thing2 {
   1: constants val = TWO
 }
 
+typedef i32 myIntType
+const myIntType myInt = 3
+
 const map<constants,string> GEN_ENUM_NAMES = {ONE : "HOWDY", TWO: PARTNER}
 
 const i32 hex_const = 0x0001F