David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | e9ce01c | 2007-05-16 02:29:53 +0000 | [diff] [blame] | 19 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 20 | #ifndef T_STRUCT_H |
| 21 | #define T_STRUCT_H |
| 22 | |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 23 | #include <algorithm> |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 24 | #include <vector> |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 25 | #include <utility> |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 26 | #include <string> |
| 27 | |
| 28 | #include "t_type.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 29 | #include "t_field.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 30 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 31 | // Forward declare that puppy |
| 32 | class t_program; |
| 33 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 34 | /** |
| 35 | * A struct is a container for a set of member fields that has a name. Structs |
| 36 | * are also used to implement exception types. |
| 37 | * |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 38 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 39 | class t_struct : public t_type { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 40 | public: |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 41 | typedef std::vector<t_field*> members_type; |
| 42 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 43 | t_struct(t_program* program) |
| 44 | : t_type(program), |
| 45 | is_xception_(false), |
| 46 | is_union_(false), |
| 47 | members_validated(false), |
| 48 | members_with_value(0), |
| 49 | xsd_all_(false) {} |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 50 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 51 | t_struct(t_program* program, const std::string& name) |
| 52 | : t_type(program, name), |
| 53 | is_xception_(false), |
| 54 | is_union_(false), |
| 55 | members_validated(false), |
| 56 | members_with_value(0), |
| 57 | xsd_all_(false) {} |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 58 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 59 | void set_name(const std::string& name) { |
| 60 | name_ = name; |
Jens Geyer | 04f7097 | 2012-12-17 22:41:21 +0100 | [diff] [blame] | 61 | validate_union_members(); |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 64 | void set_xception(bool is_xception) { is_xception_ = is_xception; } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 65 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 66 | void validate_union_member(t_field* field) { |
| 67 | if (is_union_ && (!name_.empty())) { |
Jens Geyer | 04f7097 | 2012-12-17 22:41:21 +0100 | [diff] [blame] | 68 | |
| 69 | // unions can't have required fields |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 70 | if (field->get_req() == t_field::T_REQUIRED) { |
| 71 | pwarning(1, |
| 72 | "Required field %s of union %s set to optional.\n", |
| 73 | field->get_name().c_str(), |
| 74 | name_.c_str()); |
| 75 | field->set_req(t_field::T_OPTIONAL); |
Jens Geyer | 04f7097 | 2012-12-17 22:41:21 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // unions may have up to one member defaulted, but not more |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 79 | if (field->get_value() != NULL) { |
| 80 | if (1 < ++members_with_value) { |
| 81 | throw "Error: Field " + field->get_name() + " provides another default value for union " |
| 82 | + name_; |
Jens Geyer | 04f7097 | 2012-12-17 22:41:21 +0100 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |
Jens Geyer | 04f7097 | 2012-12-17 22:41:21 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void validate_union_members() { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 89 | if (is_union_ && (!name_.empty()) && (!members_validated)) { |
Jens Geyer | 04f7097 | 2012-12-17 22:41:21 +0100 | [diff] [blame] | 90 | members_type::const_iterator m_iter; |
| 91 | for (m_iter = members_in_id_order_.begin(); m_iter != members_in_id_order_.end(); ++m_iter) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 92 | validate_union_member(*m_iter); |
Jens Geyer | 04f7097 | 2012-12-17 22:41:21 +0100 | [diff] [blame] | 93 | } |
Roger Meier | 4f4b15b | 2014-11-05 16:51:04 +0100 | [diff] [blame] | 94 | members_validated = true; |
Jens Geyer | 04f7097 | 2012-12-17 22:41:21 +0100 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 98 | void set_union(bool is_union) { |
| 99 | is_union_ = is_union; |
Jens Geyer | 04f7097 | 2012-12-17 22:41:21 +0100 | [diff] [blame] | 100 | validate_union_members(); |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 103 | void set_xsd_all(bool xsd_all) { xsd_all_ = xsd_all; } |
Mark Slee | 782abbb | 2007-01-19 00:17:02 +0000 | [diff] [blame] | 104 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 105 | bool get_xsd_all() const { return xsd_all_; } |
Mark Slee | 782abbb | 2007-01-19 00:17:02 +0000 | [diff] [blame] | 106 | |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 107 | bool append(t_field* elem) { |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 108 | typedef members_type::iterator iter_type; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 109 | std::pair<iter_type, iter_type> bounds = std::equal_range(members_in_id_order_.begin(), |
| 110 | members_in_id_order_.end(), |
| 111 | elem, |
| 112 | t_field::key_compare()); |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 113 | if (bounds.first != bounds.second) { |
| 114 | return false; |
| 115 | } |
Jens Geyer | 3a67c2f | 2013-02-03 22:30:41 +0100 | [diff] [blame] | 116 | // returns false when there is a conflict of field names |
| 117 | if (get_field_by_name(elem->get_name()) != NULL) { |
Roger Meier | 4f4b15b | 2014-11-05 16:51:04 +0100 | [diff] [blame] | 118 | return false; |
Jens Geyer | 3a67c2f | 2013-02-03 22:30:41 +0100 | [diff] [blame] | 119 | } |
| 120 | members_.push_back(elem); |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 121 | members_in_id_order_.insert(bounds.second, elem); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 122 | validate_union_member(elem); |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 123 | return true; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 124 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 125 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 126 | const members_type& get_members() { return members_; } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 127 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 128 | const members_type& get_sorted_members() { return members_in_id_order_; } |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 129 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 130 | bool is_struct() const { return !is_xception_; } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 131 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 132 | bool is_xception() const { return is_xception_; } |
Roger Meier | 4f4b15b | 2014-11-05 16:51:04 +0100 | [diff] [blame] | 133 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 134 | bool is_union() const { return is_union_; } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 135 | |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 136 | virtual std::string get_fingerprint_material() const { |
| 137 | std::string rv = "{"; |
Jens Geyer | 83767a7 | 2013-09-23 22:09:12 +0200 | [diff] [blame] | 138 | bool do_reserve = (members_in_id_order_.size() > 1); |
| 139 | size_t estimation = 0; |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 140 | members_type::const_iterator m_iter; |
| 141 | for (m_iter = members_in_id_order_.begin(); m_iter != members_in_id_order_.end(); ++m_iter) { |
David Reiss | 6ce97e3 | 2007-08-30 23:18:31 +0000 | [diff] [blame] | 142 | rv += (*m_iter)->get_fingerprint_material(); |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 143 | rv += ";"; |
Roger Meier | 4f4b15b | 2014-11-05 16:51:04 +0100 | [diff] [blame] | 144 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 145 | if (do_reserve) { |
Jens Geyer | 83767a7 | 2013-09-23 22:09:12 +0200 | [diff] [blame] | 146 | estimation = members_in_id_order_.size() * rv.size() + 16; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 147 | rv.reserve(estimation); |
Jens Geyer | 83767a7 | 2013-09-23 22:09:12 +0200 | [diff] [blame] | 148 | do_reserve = false; |
| 149 | } |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 150 | } |
| 151 | rv += "}"; |
| 152 | return rv; |
| 153 | } |
| 154 | |
David Reiss | 9885c68 | 2007-08-30 23:12:37 +0000 | [diff] [blame] | 155 | virtual void generate_fingerprint() { |
| 156 | t_type::generate_fingerprint(); |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 157 | members_type::const_iterator m_iter; |
| 158 | for (m_iter = members_in_id_order_.begin(); m_iter != members_in_id_order_.end(); ++m_iter) { |
David Reiss | 6ce97e3 | 2007-08-30 23:18:31 +0000 | [diff] [blame] | 159 | (*m_iter)->get_type()->generate_fingerprint(); |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Bryan Duxbury | 2d80470 | 2009-12-18 19:41:11 +0000 | [diff] [blame] | 163 | t_field* get_field_by_name(std::string field_name) { |
| 164 | members_type::const_iterator m_iter; |
| 165 | for (m_iter = members_in_id_order_.begin(); m_iter != members_in_id_order_.end(); ++m_iter) { |
| 166 | if ((*m_iter)->get_name() == field_name) { |
| 167 | return *m_iter; |
| 168 | } |
| 169 | } |
| 170 | return NULL; |
| 171 | } |
| 172 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 173 | private: |
Bryan Duxbury | ff219ac | 2009-04-10 21:51:00 +0000 | [diff] [blame] | 174 | members_type members_; |
| 175 | members_type members_in_id_order_; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 176 | bool is_xception_; |
Bryan Duxbury | ab3666e | 2009-09-01 23:03:47 +0000 | [diff] [blame] | 177 | bool is_union_; |
Jens Geyer | 04f7097 | 2012-12-17 22:41:21 +0100 | [diff] [blame] | 178 | bool members_validated; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 179 | int members_with_value; |
Mark Slee | 782abbb | 2007-01-19 00:17:02 +0000 | [diff] [blame] | 180 | |
| 181 | bool xsd_all_; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | #endif |