THRIFT-2421: Tree/Recursive struct support in thrift
Client: cpp
Patch: Dave Watson
Github Pull Request: This closes #84
----
commit b6134cedf292845e5ed01052919894df6b561bf2
Date: 2014-03-20T18:12:04Z
Recursive structs support in parser
A common complaint is that you can't express trees or other recursive structures in thrift easily - unlike protobufs. This diff loosens up the parser to allow using structs before they are defined (and uses typedef as a forward declaration).
This diff is actually enough to make recursive types work for some dyamic languages (I tried php, works out of the box!)
Other languages will need forward declarations, or ways to box types, to make this work (i.e. C++ needs both forward decls and a way to express structs as pointers)
diff --git a/compiler/cpp/src/parse/t_program.h b/compiler/cpp/src/parse/t_program.h
index 96a8a5c..5cf2738 100644
--- a/compiler/cpp/src/parse/t_program.h
+++ b/compiler/cpp/src/parse/t_program.h
@@ -235,7 +235,7 @@
}
// Scope accessor
- t_scope* scope() {
+ t_scope* scope() const {
return scope_;
}
diff --git a/compiler/cpp/src/parse/t_type.h b/compiler/cpp/src/parse/t_type.h
index 74686b0..b85f2da 100644
--- a/compiler/cpp/src/parse/t_type.h
+++ b/compiler/cpp/src/parse/t_type.h
@@ -66,6 +66,10 @@
return program_;
}
+ const t_program* get_program() const {
+ return program_;
+ }
+
t_type* get_true_type();
// Return a string that uniquely identifies this type
diff --git a/compiler/cpp/src/parse/t_typedef.cc b/compiler/cpp/src/parse/t_typedef.cc
new file mode 100644
index 0000000..ddbe749
--- /dev/null
+++ b/compiler/cpp/src/parse/t_typedef.cc
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <cstdio>
+
+#include "t_typedef.h"
+#include "t_program.h"
+
+t_type* t_typedef::get_type() const {
+ if (type_ == NULL) {
+ t_type* type = get_program()->scope()->get_type(symbolic_);
+ if (type == NULL) {
+ printf("Type \"%s\" not defined\n", symbolic_.c_str());
+ exit(1);
+ }
+ return type;
+ }
+ return type_;
+}
diff --git a/compiler/cpp/src/parse/t_typedef.h b/compiler/cpp/src/parse/t_typedef.h
index 4c77d97..1bea4c9 100644
--- a/compiler/cpp/src/parse/t_typedef.h
+++ b/compiler/cpp/src/parse/t_typedef.h
@@ -32,16 +32,26 @@
*/
class t_typedef : public t_type {
public:
- t_typedef(t_program* program, t_type* type, std::string symbolic) :
+ t_typedef(t_program* program, t_type* type, const std::string& symbolic) :
t_type(program, symbolic),
type_(type),
- symbolic_(symbolic) {}
+ symbolic_(symbolic),
+ seen_(false) {}
+
+ /**
+ * This constructor is used to refer to a type that is lazily
+ * resolved at a later time, like for forward declarations or
+ * recursive types.
+ */
+ t_typedef(t_program* program, const std::string& symbolic) :
+ t_type(program, symbolic),
+ type_(NULL),
+ symbolic_(symbolic),
+ seen_(false) {}
~t_typedef() {}
- t_type* get_type() const {
- return type_;
- }
+ t_type* get_type() const;
const std::string& get_symbolic() const {
return symbolic_;
@@ -52,19 +62,26 @@
}
virtual std::string get_fingerprint_material() const {
- return type_->get_fingerprint_material();
+ if (!seen_) {
+ seen_ = true;
+ std::string ret = get_type()->get_fingerprint_material();
+ seen_ = false;
+ return ret;
+ }
+ return "";
}
virtual void generate_fingerprint() {
t_type::generate_fingerprint();
- if (!type_->has_fingerprint()) {
- type_->generate_fingerprint();
+ if (!get_type()->has_fingerprint()) {
+ get_type()->generate_fingerprint();
}
}
private:
t_type* type_;
std::string symbolic_;
+ mutable bool seen_;
};
#endif