Thrift compiler improvements, two modes for PHP

Summary: Complete PHP generator and CPP generator to new formats, and offer PHP generator that generates inline code free of any TProtocol abstraction


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664771 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/parse/t_base_type.h b/compiler/cpp/src/parse/t_base_type.h
index f6cc197..5aebba7 100644
--- a/compiler/cpp/src/parse/t_base_type.h
+++ b/compiler/cpp/src/parse/t_base_type.h
@@ -15,10 +15,9 @@
     TYPE_VOID,
     TYPE_STRING,
     TYPE_BYTE,
+    TYPE_I16,
     TYPE_I32,
-    TYPE_U32,
-    TYPE_I64,
-    TYPE_U64
+    TYPE_I64
   };
 
   t_base_type(std::string name, t_base base) :
diff --git a/compiler/cpp/src/parse/t_field.h b/compiler/cpp/src/parse/t_field.h
index fc38456..7f4d0e9 100644
--- a/compiler/cpp/src/parse/t_field.h
+++ b/compiler/cpp/src/parse/t_field.h
@@ -14,19 +14,19 @@
   t_field(t_type* type, std::string name) :
     type_(type), name_(name), key_(0) {}
 
-  t_field(t_type* type, std::string name, uint32_t key) :
+  t_field(t_type* type, std::string name, int32_t key) :
     type_(type), name_(name), key_(key) {}
 
   ~t_field() {}
 
   t_type* get_type() const { return type_; }
   const std::string& get_name() const { return name_; }
-  uint32_t get_key() const { return key_; }
+  int32_t get_key() const { return key_; }
 
  private:
   t_type* type_;
   std::string name_;
-  uint32_t key_;
+  int32_t key_;
 };
 
 #endif
diff --git a/compiler/cpp/src/parse/t_function.h b/compiler/cpp/src/parse/t_function.h
index 9e6c56a..58667d8 100644
--- a/compiler/cpp/src/parse/t_function.h
+++ b/compiler/cpp/src/parse/t_function.h
@@ -20,6 +20,20 @@
     returntype_(returntype),
     name_(name),
     arglist_(arglist),
+    async_(async) {
+    xceptions_ = new t_struct;
+  }
+
+
+  t_function(t_type* returntype,
+             std::string name,
+             t_struct* arglist,
+             t_struct* xceptions,
+             bool async=false) :
+    returntype_(returntype),
+    name_(name),
+    arglist_(arglist),
+    xceptions_(xceptions),
     async_(async) {}
 
   ~t_function() {}
@@ -27,12 +41,14 @@
   t_type*      get_returntype() const { return returntype_; }
   const std::string& get_name() const { return name_; }
   t_struct*    get_arglist()    const { return arglist_; }
+  t_struct*    get_xceptions()  const { return xceptions_; }
   bool         is_async()       const { return async_; }
 
  private:
   t_type* returntype_;
   std::string name_;
   t_struct* arglist_;
+  t_struct* xceptions_;
   bool async_;
 };
 
diff --git a/compiler/cpp/src/parse/t_program.h b/compiler/cpp/src/parse/t_program.h
index cda24c6..98968ff 100644
--- a/compiler/cpp/src/parse/t_program.h
+++ b/compiler/cpp/src/parse/t_program.h
@@ -21,6 +21,7 @@
  *   Typedefs
  *   Enumerations
  *   Structs
+ *   Exceptions
  *   Services
  *
  * @author Mark Slee <mcslee@facebook.com>
@@ -28,52 +29,54 @@
 class t_program {
  public:
   t_program(std::string name) :
-    name_(name) {
+    name_(name), namespace_() {
     type_void   = new t_base_type("void",   t_base_type::TYPE_VOID);
     type_string = new t_base_type("string", t_base_type::TYPE_STRING);
     type_byte   = new t_base_type("byte",   t_base_type::TYPE_BYTE);
+    type_i16    = new t_base_type("i16",    t_base_type::TYPE_I16);
     type_i32    = new t_base_type("i32",    t_base_type::TYPE_I32);
-    type_u32    = new t_base_type("u32",    t_base_type::TYPE_U32);
     type_i64    = new t_base_type("i64",    t_base_type::TYPE_I64);
-    type_u64    = new t_base_type("u64",    t_base_type::TYPE_U64);
   }
 
   ~t_program() {
     delete type_string;
     delete type_byte;
+    delete type_i16;
     delete type_i32;
-    delete type_u32;
     delete type_i64;
-    delete type_u64;
   }
 
   // Name accessor
   const std::string& get_name() const { return name_; }
 
+  // Namespace
+  const std::string& get_namespace() const { return namespace_; }
+
   // Accessors for program elements
-  const std::vector<t_typedef*>& get_typedefs() const { return typedefs_; }
-  const std::vector<t_enum*>&    get_enums()    const { return enums_;    }
-  const std::vector<t_struct*>&  get_structs()  const { return structs_;  }
-  const std::vector<t_service*>& get_services() const { return services_; }
+  const std::vector<t_typedef*>& get_typedefs()  const { return typedefs_;  }
+  const std::vector<t_enum*>&    get_enums()     const { return enums_;     }
+  const std::vector<t_struct*>&  get_structs()   const { return structs_;   }
+  const std::vector<t_struct*>&  get_xceptions() const { return xceptions_; }
+  const std::vector<t_service*>& get_services()  const { return services_;  }
 
   // Accessors for global types
   t_type* get_void_type()   const { return type_void;   }
   t_type* get_string_type() const { return type_string; }
   t_type* get_byte_type()   const { return type_byte;   }
+  t_type* get_i16_type()    const { return type_i16;    }
   t_type* get_i32_type()    const { return type_i32;    }
-  t_type* get_u32_type()    const { return type_u32;    }
   t_type* get_i64_type()    const { return type_i64;    }
-  t_type* get_u64_type()    const { return type_u64;    }
 
   // Custom data type lookup
-  void add_custom_type(std::string name, t_type* type) {
-    custom_types_[name] = type;
-  }
   t_type* get_custom_type(std::string name) {
     return custom_types_[name];
   }
 
   // New program element addition
+  void set_namespace(std::string name) {
+    namespace_ = name;
+  }
+
   void add_typedef(t_typedef* td) {
     typedefs_.push_back(td);
     add_custom_type(td->get_symbolic(), td);
@@ -86,19 +89,32 @@
     structs_.push_back(ts);
     add_custom_type(ts->get_name(), ts);
   }
+  void add_xception(t_struct* tx) {
+    xceptions_.push_back(tx);
+    add_custom_type(tx->get_name(), tx);
+  }
   void add_service(t_service* ts) {
     services_.push_back(ts);
   }
 
  private:
+  // Add custom type for lookup
+  void add_custom_type(std::string name, t_type* type) {
+    custom_types_[name] = type;
+  }
+
   // Name
   std::string name_;
 
+  // Namespace
+  std::string namespace_;
+
   // Components
-  std::vector<t_typedef*> typedefs_;
-  std::vector<t_enum*>    enums_;
-  std::vector<t_struct*>  structs_;
-  std::vector<t_service*> services_;
+  std::vector<t_typedef*>  typedefs_;
+  std::vector<t_enum*>     enums_;
+  std::vector<t_struct*>   structs_;
+  std::vector<t_struct*>   xceptions_;
+  std::vector<t_service*>  services_;
 
   // Type map
   std::map<std::string, t_type*> custom_types_;
@@ -107,10 +123,9 @@
   t_type* type_void;
   t_type* type_string;
   t_type* type_byte;
+  t_type* type_i16;
   t_type* type_i32;
-  t_type* type_u32;
   t_type* type_i64;
-  t_type* type_u64;  
 };
 
 #endif
diff --git a/compiler/cpp/src/parse/t_struct.h b/compiler/cpp/src/parse/t_struct.h
index 6e87c8c..a365cd4 100644
--- a/compiler/cpp/src/parse/t_struct.h
+++ b/compiler/cpp/src/parse/t_struct.h
@@ -9,22 +9,41 @@
 
 class t_struct : public t_type {
  public:
-  t_struct() {}
-  t_struct(const std::string& name) : t_type(name) {}
+  t_struct() : is_xception_(false) {}
+  t_struct(const std::string& name) : t_type(name), is_xception_(false) {}
 
   ~t_struct() {}
 
   /** Set the struct name */
-  void set_name(const std::string& name) { name_ = name; }
+  void set_name(const std::string& name) {
+    name_ = name;
+  }
+
+  /** Mark as an exception */
+  void set_xception(bool is_xception) {
+    is_xception_ = is_xception;
+  }
 
   /** Add a new field to the list */
-  void append(t_field* elem) { members_.push_back(elem); }
+  void append(t_field* elem) {
+    members_.push_back(elem);
+  }
 
-  const std::vector<t_field*>& get_members() { return members_; }
-  bool is_struct() const { return true; }
+  const std::vector<t_field*>& get_members() {
+    return members_;
+  }
+
+  bool is_struct() const {
+    return !is_xception_;
+  }
+
+  bool is_xception() const {
+    return is_xception_;
+  }
 
  private:
   std::vector<t_field*> members_;
+  bool is_xception_;
 };
 
 #endif
diff --git a/compiler/cpp/src/parse/t_type.h b/compiler/cpp/src/parse/t_type.h
index 6703bfa..6328961 100644
--- a/compiler/cpp/src/parse/t_type.h
+++ b/compiler/cpp/src/parse/t_type.h
@@ -19,6 +19,7 @@
   virtual bool is_typedef()   const { return false; }
   virtual bool is_enum()      const { return false; }
   virtual bool is_struct()    const { return false; }
+  virtual bool is_xception()  const { return false; }
   virtual bool is_list()      const { return false; }
   virtual bool is_set()       const { return false; }
   virtual bool is_map()       const { return false; }