|
| 1 | +// Copyright (C) 2020-2022 Free Software Foundation, Inc. |
| 2 | + |
| 3 | +// This file is part of GCC. |
| 4 | + |
| 5 | +// GCC is free software; you can redistribute it and/or modify it under |
| 6 | +// the terms of the GNU General Public License as published by the Free |
| 7 | +// Software Foundation; either version 3, or (at your option) any later |
| 8 | +// version. |
| 9 | + |
| 10 | +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | +// WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | +// for more details. |
| 14 | + |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with GCC; see the file COPYING3. If not see |
| 17 | +// <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +#include "rust-ast-resolve-type.h" |
| 20 | +#include "rust-ast-resolve-expr.h" |
| 21 | + |
| 22 | +namespace Rust { |
| 23 | +namespace Resolver { |
| 24 | + |
| 25 | +// rust-ast-resolve-type.h |
| 26 | + |
| 27 | +std::string |
| 28 | +ResolveTypeToCanonicalPath::canonicalize_generic_args (AST::GenericArgs &args) |
| 29 | +{ |
| 30 | + std::string buf; |
| 31 | + |
| 32 | + size_t i = 0; |
| 33 | + size_t total = args.get_type_args ().size (); |
| 34 | + |
| 35 | + for (auto &ty_arg : args.get_type_args ()) |
| 36 | + { |
| 37 | + buf += ty_arg->as_string (); |
| 38 | + if ((i + 1) < total) |
| 39 | + buf += ","; |
| 40 | + |
| 41 | + i++; |
| 42 | + } |
| 43 | + |
| 44 | + return "<" + buf + ">"; |
| 45 | +} |
| 46 | + |
| 47 | +bool |
| 48 | +ResolveTypeToCanonicalPath::type_resolve_generic_args (AST::GenericArgs &args) |
| 49 | +{ |
| 50 | + for (auto > : args.get_type_args ()) |
| 51 | + { |
| 52 | + ResolveType::go (gt.get (), UNKNOWN_NODEID); |
| 53 | + // FIXME error handling here for inference variable since they do not have |
| 54 | + // a node to resolve to |
| 55 | + // if (resolved == UNKNOWN_NODEID) return false; |
| 56 | + } |
| 57 | + return true; |
| 58 | +} |
| 59 | + |
| 60 | +void |
| 61 | +ResolveTypeToCanonicalPath::visit (AST::TypePathSegmentGeneric &seg) |
| 62 | +{ |
| 63 | + if (seg.is_error ()) |
| 64 | + { |
| 65 | + failure_flag = true; |
| 66 | + rust_error_at (seg.get_locus (), "segment has error: %s", |
| 67 | + seg.as_string ().c_str ()); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + if (!seg.has_generic_args ()) |
| 72 | + { |
| 73 | + auto ident_segment |
| 74 | + = CanonicalPath::new_seg (seg.get_node_id (), |
| 75 | + seg.get_ident_segment ().as_string ()); |
| 76 | + result = result.append (ident_segment); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + if (type_resolve_generic_args_flag) |
| 81 | + { |
| 82 | + bool ok = type_resolve_generic_args (seg.get_generic_args ()); |
| 83 | + failure_flag = !ok; |
| 84 | + } |
| 85 | + |
| 86 | + if (include_generic_args_flag) |
| 87 | + { |
| 88 | + std::string generics |
| 89 | + = canonicalize_generic_args (seg.get_generic_args ()); |
| 90 | + auto generic_segment |
| 91 | + = CanonicalPath::new_seg (seg.get_node_id (), |
| 92 | + seg.get_ident_segment ().as_string () |
| 93 | + + "::" + generics); |
| 94 | + result = result.append (generic_segment); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + auto ident_segment |
| 99 | + = CanonicalPath::new_seg (seg.get_node_id (), |
| 100 | + seg.get_ident_segment ().as_string ()); |
| 101 | + result = result.append (ident_segment); |
| 102 | +} |
| 103 | + |
| 104 | +void |
| 105 | +ResolveTypeToCanonicalPath::visit (AST::TypePathSegment &seg) |
| 106 | +{ |
| 107 | + if (seg.is_error ()) |
| 108 | + { |
| 109 | + failure_flag = true; |
| 110 | + rust_error_at (seg.get_locus (), "segment has error: %s", |
| 111 | + seg.as_string ().c_str ()); |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + CanonicalPath ident_seg |
| 116 | + = CanonicalPath::new_seg (seg.get_node_id (), |
| 117 | + seg.get_ident_segment ().as_string ()); |
| 118 | + result = result.append (ident_seg); |
| 119 | +} |
| 120 | + |
| 121 | +void |
| 122 | +ResolveType::visit (AST::ArrayType &type) |
| 123 | +{ |
| 124 | + type.get_elem_type ()->accept_vis (*this); |
| 125 | + // FIXME |
| 126 | + // the capacity expr can contain block-expr with functions but these should be |
| 127 | + // folded via constexpr code |
| 128 | + ResolveExpr::go (type.get_size_expr ().get (), type.get_node_id (), |
| 129 | + CanonicalPath::create_empty (), |
| 130 | + CanonicalPath::create_empty ()); |
| 131 | +} |
| 132 | + |
| 133 | +void |
| 134 | +ResolveType::visit (AST::TraitObjectTypeOneBound &type) |
| 135 | +{ |
| 136 | + NodeId bound_resolved_id |
| 137 | + = ResolveTypeBound::go (&type.get_trait_bound (), type.get_node_id ()); |
| 138 | + ok = bound_resolved_id != UNKNOWN_NODEID; |
| 139 | +} |
| 140 | + |
| 141 | +void |
| 142 | +ResolveType::visit (AST::TraitObjectType &type) |
| 143 | +{ |
| 144 | + ok = true; |
| 145 | + for (auto &bound : type.get_type_param_bounds ()) |
| 146 | + { |
| 147 | + /* NodeId bound_resolved_id = */ |
| 148 | + ResolveTypeBound::go (bound.get (), type.get_node_id ()); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +void |
| 153 | +ResolveTypeToCanonicalPath::visit (AST::ReferenceType &ref) |
| 154 | +{ |
| 155 | + auto inner_type |
| 156 | + = ResolveTypeToCanonicalPath::resolve (*ref.get_type_referenced ().get (), |
| 157 | + include_generic_args_flag, |
| 158 | + type_resolve_generic_args_flag); |
| 159 | + |
| 160 | + std::string segment_string ("&"); |
| 161 | + if (ref.get_has_mut ()) |
| 162 | + segment_string += "mut "; |
| 163 | + |
| 164 | + segment_string += inner_type.get (); |
| 165 | + |
| 166 | + auto ident_seg = CanonicalPath::new_seg (ref.get_node_id (), segment_string); |
| 167 | + result = result.append (ident_seg); |
| 168 | +} |
| 169 | + |
| 170 | +void |
| 171 | +ResolveType::visit (AST::ReferenceType &type) |
| 172 | +{ |
| 173 | + type.get_type_referenced ()->accept_vis (*this); |
| 174 | + |
| 175 | + if (canonical_path != nullptr && canonical_path->size () > 0) |
| 176 | + { |
| 177 | + std::string seg = canonical_path->get (); |
| 178 | + *canonical_path = CanonicalPath::new_seg (type.get_node_id (), "&" + seg); |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +void |
| 183 | +ResolveType::visit (AST::RawPointerType &type) |
| 184 | +{ |
| 185 | + type.get_type_pointed_to ()->accept_vis (*this); |
| 186 | + |
| 187 | + if (canonical_path != nullptr && canonical_path->size () > 0) |
| 188 | + { |
| 189 | + std::string seg = canonical_path->get (); |
| 190 | + *canonical_path = CanonicalPath::new_seg (type.get_node_id (), "*" + seg); |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +void |
| 195 | +ResolveType::visit (AST::InferredType &type) |
| 196 | +{ |
| 197 | + ok = true; |
| 198 | +} |
| 199 | + |
| 200 | +} // namespace Resolver |
| 201 | +} // namespace Rust |
0 commit comments