Branch data Line data Source code
1 : : // Copyright (C) 2020-2025 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-privacy-reporter.h"
20 : : #include "rust-session-manager.h"
21 : : #include "rust-hir-expr.h"
22 : : #include "rust-hir-stmt.h"
23 : : #include "rust-hir-item.h"
24 : : #include "rust-attribute-values.h"
25 : : #include "rust-immutable-name-resolution-context.h"
26 : :
27 : : namespace Rust {
28 : : namespace Privacy {
29 : :
30 : 3819 : PrivacyReporter::PrivacyReporter (
31 : : Analysis::Mappings &mappings, Resolver::Resolver &resolver,
32 : 3819 : const Rust::Resolver::TypeCheckContext &ty_ctx)
33 : 3819 : : mappings (mappings), resolver (resolver), ty_ctx (ty_ctx),
34 : 3819 : current_module (tl::nullopt)
35 : 3819 : {}
36 : :
37 : : // Find a proc_macro, proc_macro_derive or proc_macro_attribute
38 : : // attribute in a vector of attribute
39 : : static tl::optional<std::string>
40 : 12 : find_proc_macro_attribute (const AST::AttrVec &outer_attrs)
41 : : {
42 : 12 : for (auto &a : outer_attrs)
43 : : {
44 : 6 : auto &segments = a.get_path ().get_segments ();
45 : 6 : if (segments.size () != 1)
46 : 0 : continue;
47 : 6 : auto name = segments.at (0).get_segment_name ();
48 : 6 : if (name == Values::Attributes::PROC_MACRO
49 : 4 : || name == Values::Attributes::PROC_MACRO_ATTRIBUTE
50 : 8 : || name == Values::Attributes::PROC_MACRO_DERIVE)
51 : 6 : return name;
52 : 6 : }
53 : :
54 : 6 : return tl::nullopt;
55 : : }
56 : :
57 : : // Common check on crate items when dealing with 'proc-macro' crate type.
58 : : static void
59 : 12 : proc_macro_privacy_check (std::unique_ptr<HIR::Item> &item)
60 : : {
61 : 12 : if (item->get_hir_kind () == HIR::Node::BaseKind::VIS_ITEM)
62 : : {
63 : 12 : auto attribute = find_proc_macro_attribute (item->get_outer_attrs ());
64 : :
65 : 12 : bool pub_item = static_cast<HIR::VisItem *> (item.get ())
66 : 12 : ->get_visibility ()
67 : 12 : .is_public ();
68 : :
69 : 12 : if (pub_item && !attribute.has_value ()) // Public non proc-macro
70 : 4 : rust_error_at (
71 : 4 : item->get_locus (),
72 : : "%<proc-macro%> crate types currently cannot export any "
73 : : "items other than functions tagged with %<#[proc_macro]%>, "
74 : : "%<#[proc_macro_derive]%> or %<#[proc_macro_attribute]%>");
75 : 8 : else if (!pub_item && attribute.has_value ()) // Private proc-macro
76 : 6 : rust_error_at (item->get_locus (),
77 : : "functions tagged with %<#[%s]%> must be %<pub%>",
78 : 6 : attribute.value ().c_str ());
79 : 12 : }
80 : 12 : }
81 : :
82 : : void
83 : 3819 : PrivacyReporter::go (HIR::Crate &crate)
84 : : {
85 : 18168 : for (auto &item : crate.get_items ())
86 : : {
87 : 14349 : if (Session::get_instance ().options.is_proc_macro ())
88 : 12 : proc_macro_privacy_check (item);
89 : 14349 : item->accept_vis (*this);
90 : : }
91 : 3819 : }
92 : :
93 : : static bool
94 : 21 : is_child_module (Analysis::Mappings &mappings, NodeId parent,
95 : : NodeId possible_child)
96 : : {
97 : 21 : if (flag_name_resolution_2_0)
98 : : {
99 : 7 : auto &nr_ctx
100 : 7 : = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
101 : :
102 : 7 : return nr_ctx.values.is_module_descendant (parent, possible_child);
103 : : }
104 : :
105 : 14 : auto children = mappings.lookup_module_children (parent);
106 : :
107 : 14 : if (!children)
108 : : return false;
109 : :
110 : : // Visit all toplevel children
111 : 4 : for (auto &child : *children)
112 : 4 : if (child == possible_child)
113 : 2 : return true;
114 : :
115 : : // Now descend recursively in the child module tree
116 : 0 : for (auto &child : *children)
117 : 0 : if (is_child_module (mappings, child, possible_child))
118 : 2 : return true;
119 : :
120 : : return false;
121 : : }
122 : :
123 : : // FIXME: This function needs a lot of refactoring
124 : : void
125 : 28107 : PrivacyReporter::check_for_privacy_violation (const NodeId &use_id,
126 : : const location_t locus)
127 : : {
128 : 28107 : NodeId ref_node_id = UNKNOWN_NODEID;
129 : :
130 : 28107 : if (flag_name_resolution_2_0)
131 : : {
132 : 829 : auto &nr_ctx
133 : 829 : = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
134 : :
135 : 829 : if (auto id = nr_ctx.lookup (use_id))
136 : 826 : ref_node_id = *id;
137 : : }
138 : : // FIXME: Don't assert here - we might be dealing with a type
139 : 27278 : else if (!resolver.lookup_resolved_name (use_id, &ref_node_id))
140 : 2935 : resolver.lookup_resolved_type (use_id, &ref_node_id);
141 : :
142 : : // FIXME: Assert here. For now, we return since this causes issues when
143 : : // checking inferred types (#1260)
144 : : // rust_assert (ref_node_id != UNKNOWN_NODEID);
145 : 28107 : if (ref_node_id == UNKNOWN_NODEID)
146 : 27253 : return;
147 : :
148 : 28063 : auto vis = mappings.lookup_visibility (ref_node_id);
149 : :
150 : : // FIXME: Can we really return here if the item has no visibility?
151 : 28063 : if (!vis)
152 : : return;
153 : :
154 : 5030 : auto valid = true;
155 : :
156 : 5030 : switch (vis->get_kind ())
157 : : {
158 : : case ModuleVisibility::Public:
159 : : break;
160 : 4210 : case ModuleVisibility::Restricted: {
161 : : // If we are in the crate, everything is restricted correctly, but we
162 : : // can't get a module for it
163 : 4210 : if (!current_module.has_value ())
164 : : return;
165 : :
166 : 34 : auto module = mappings.lookup_defid (vis->get_module_id ()).value ();
167 : :
168 : 34 : auto mod_node_id = module->get_mappings ().get_nodeid ();
169 : :
170 : : // We are in the module referenced by the pub(restricted) visibility.
171 : : // This is valid
172 : 34 : if (mod_node_id == current_module.value ())
173 : : break;
174 : :
175 : : // FIXME: This needs a LOT of TLC: hinting about the definition, a
176 : : // string to say if it's a module, function, type, etc...
177 : 21 : if (!is_child_module (mappings, mod_node_id, current_module.value ()))
178 : 18 : valid = false;
179 : : }
180 : : break;
181 : 0 : case ModuleVisibility::Unknown:
182 : 0 : rust_unreachable ();
183 : 18 : break;
184 : : }
185 : :
186 : 18 : if (!valid)
187 : : {
188 : 18 : rich_location richloc (line_table, locus);
189 : 18 : richloc.add_fixit_replace ("item is private");
190 : 18 : rust_error_at (richloc, ErrorCode::E0603,
191 : : "definition is private in this context");
192 : 18 : }
193 : : }
194 : :
195 : : void
196 : 6820 : PrivacyReporter::check_base_type_privacy (Analysis::NodeMapping &node_mappings,
197 : : const TyTy::BaseType *ty,
198 : : const location_t locus)
199 : : {
200 : : // Avoids repeating commong argument such as `use_id` or `locus` since we're
201 : : // doing a lot of recursive calls here
202 : 6820 : auto recursive_check
203 : 1803 : = [this, &node_mappings, &locus] (const TyTy::BaseType *ty) {
204 : 1803 : return check_base_type_privacy (node_mappings, ty, locus);
205 : 6820 : };
206 : :
207 : 6820 : switch (ty->get_kind ())
208 : : {
209 : : // These "simple" types are our stop condition
210 : 4063 : case TyTy::BOOL:
211 : 4063 : case TyTy::CHAR:
212 : 4063 : case TyTy::INT:
213 : 4063 : case TyTy::UINT:
214 : 4063 : case TyTy::FLOAT:
215 : 4063 : case TyTy::USIZE:
216 : 4063 : case TyTy::ISIZE:
217 : 4063 : case TyTy::ADT:
218 : 4063 : case TyTy::STR: {
219 : 4063 : auto ref_id = ty->get_ref ();
220 : 4063 : if (auto lookup_id = mappings.lookup_hir_to_node (ref_id))
221 : 4063 : return check_for_privacy_violation (*lookup_id, locus);
222 : 0 : rust_unreachable ();
223 : : }
224 : 755 : case TyTy::REF:
225 : 755 : return recursive_check (
226 : 1510 : static_cast<const TyTy::ReferenceType *> (ty)->get_base ());
227 : 342 : case TyTy::POINTER:
228 : 342 : return recursive_check (
229 : 684 : static_cast<const TyTy::PointerType *> (ty)->get_base ());
230 : 334 : case TyTy::ARRAY:
231 : 334 : return recursive_check (
232 : 668 : static_cast<const TyTy::ArrayType *> (ty)->get_element_type ());
233 : 194 : case TyTy::SLICE:
234 : 194 : return recursive_check (
235 : 388 : static_cast<const TyTy::SliceType *> (ty)->get_element_type ());
236 : 32 : case TyTy::FNPTR:
237 : 59 : for (auto ¶m : static_cast<const TyTy::FnPtr *> (ty)->get_params ())
238 : 27 : recursive_check (param.get_tyty ());
239 : 32 : return recursive_check (
240 : 64 : static_cast<const TyTy::FnPtr *> (ty)->get_return_type ());
241 : 69 : case TyTy::TUPLE:
242 : 153 : for (auto ¶m :
243 : 153 : static_cast<const TyTy::TupleType *> (ty)->get_fields ())
244 : 84 : recursive_check (param.get_tyty ());
245 : : return;
246 : 0 : case TyTy::PLACEHOLDER:
247 : 0 : return recursive_check (
248 : : // FIXME: Can we use `resolve` here? Is that what we should do?
249 : 0 : static_cast<const TyTy::PlaceholderType *> (ty)->resolve ());
250 : 35 : case TyTy::PROJECTION:
251 : 35 : return recursive_check (
252 : 35 : static_cast<const TyTy::ProjectionType *> (ty)->get ());
253 : 0 : case TyTy::CLOSURE:
254 : 0 : rust_sorry_at (locus, "privacy pass for closures is not handled yet");
255 : 0 : break;
256 : :
257 : : // If we're dealing with a generic param, there's nothing we should be
258 : : // doing here
259 : : case TyTy::PARAM:
260 : : // We are dealing with a function definition that has been assigned
261 : : // somewhere else. Nothing to resolve privacy-wise other than the actual
262 : : // function, which is resolved as an expression
263 : : case TyTy::FNDEF:
264 : : // FIXME: Can we really not resolve Dynamic types here? Shouldn't we have
265 : : // a look at the path and perform proper privacy analysis?
266 : : case TyTy::DYNAMIC:
267 : : // The never type is builtin and always available
268 : : case TyTy::NEVER:
269 : : // We shouldn't have inference types here, ever
270 : : case TyTy::INFER:
271 : : return;
272 : : case TyTy::ERROR:
273 : : return;
274 : : }
275 : : }
276 : :
277 : : void
278 : 5017 : PrivacyReporter::check_type_privacy (const HIR::Type &type)
279 : : {
280 : 5017 : TyTy::BaseType *lookup = nullptr;
281 : 5017 : rust_assert (ty_ctx.lookup_type (type.get_mappings ().get_hirid (), &lookup));
282 : :
283 : 5017 : auto node_mappings = type.get_mappings ();
284 : 5017 : return check_base_type_privacy (node_mappings, lookup, type.get_locus ());
285 : : }
286 : :
287 : : void
288 : 23965 : PrivacyReporter::visit (HIR::PathInExpression &path)
289 : : {
290 : 23965 : check_for_privacy_violation (path.get_mappings ().get_nodeid (),
291 : : path.get_locus ());
292 : 23965 : }
293 : :
294 : : void
295 : 0 : PrivacyReporter::visit (HIR::TypePathSegmentFunction &)
296 : : {
297 : : // FIXME: Do we need to do anything for this?
298 : 0 : }
299 : :
300 : : void
301 : 36 : PrivacyReporter::visit (HIR::InlineAsm &)
302 : 36 : {}
303 : :
304 : : void
305 : 0 : PrivacyReporter::visit (HIR::TypePath &path)
306 : : {
307 : 0 : check_for_privacy_violation (path.get_mappings ().get_nodeid (),
308 : 0 : path.get_locus ());
309 : 0 : }
310 : :
311 : : void
312 : 79 : PrivacyReporter::visit (HIR::QualifiedPathInExpression &path)
313 : : {
314 : 79 : check_for_privacy_violation (path.get_mappings ().get_nodeid (),
315 : : path.get_locus ());
316 : 79 : }
317 : :
318 : : void
319 : 0 : PrivacyReporter::visit (HIR::QualifiedPathInType &path)
320 : : {
321 : 0 : check_for_privacy_violation (path.get_mappings ().get_nodeid (),
322 : 0 : path.get_locus ());
323 : 0 : }
324 : :
325 : : void
326 : 12833 : PrivacyReporter::visit (HIR::LiteralExpr &)
327 : : {
328 : : // Literals cannot contain any sort of privacy violation
329 : 12833 : }
330 : :
331 : : void
332 : 1034 : PrivacyReporter::visit (HIR::BorrowExpr &expr)
333 : : {
334 : 1034 : expr.get_expr ().accept_vis (*this);
335 : 1034 : }
336 : :
337 : : void
338 : 1283 : PrivacyReporter::visit (HIR::DereferenceExpr &expr)
339 : : {
340 : 1283 : expr.get_expr ().accept_vis (*this);
341 : 1283 : }
342 : :
343 : : void
344 : 0 : PrivacyReporter::visit (HIR::ErrorPropagationExpr &expr)
345 : : {
346 : 0 : expr.get_expr ().accept_vis (*this);
347 : 0 : }
348 : :
349 : : void
350 : 296 : PrivacyReporter::visit (HIR::NegationExpr &expr)
351 : : {
352 : 296 : expr.get_expr ().accept_vis (*this);
353 : 296 : }
354 : :
355 : : void
356 : 2342 : PrivacyReporter::visit (HIR::ArithmeticOrLogicalExpr &expr)
357 : : {
358 : 2342 : expr.get_lhs ().accept_vis (*this);
359 : 2342 : expr.get_rhs ().accept_vis (*this);
360 : 2342 : }
361 : :
362 : : void
363 : 814 : PrivacyReporter::visit (HIR::ComparisonExpr &expr)
364 : : {
365 : 814 : expr.get_lhs ().accept_vis (*this);
366 : 814 : expr.get_rhs ().accept_vis (*this);
367 : 814 : }
368 : :
369 : : void
370 : 280 : PrivacyReporter::visit (HIR::LazyBooleanExpr &expr)
371 : : {
372 : 280 : expr.get_lhs ().accept_vis (*this);
373 : 280 : expr.get_rhs ().accept_vis (*this);
374 : 280 : }
375 : :
376 : : void
377 : 2932 : PrivacyReporter::visit (HIR::TypeCastExpr &expr)
378 : : {
379 : 2932 : expr.get_expr ().accept_vis (*this);
380 : 2932 : }
381 : :
382 : : void
383 : 1584 : PrivacyReporter::visit (HIR::AssignmentExpr &expr)
384 : : {
385 : 1584 : expr.get_lhs ().accept_vis (*this);
386 : 1584 : expr.get_rhs ().accept_vis (*this);
387 : 1584 : }
388 : :
389 : : void
390 : 181 : PrivacyReporter::visit (HIR::CompoundAssignmentExpr &expr)
391 : : {
392 : 181 : expr.get_lhs ().accept_vis (*this);
393 : 181 : expr.get_rhs ().accept_vis (*this);
394 : 181 : }
395 : :
396 : : void
397 : 198 : PrivacyReporter::visit (HIR::GroupedExpr &expr)
398 : : {
399 : 198 : expr.get_expr_in_parens ().accept_vis (*this);
400 : 198 : }
401 : :
402 : : void
403 : 315 : PrivacyReporter::visit (HIR::ArrayExpr &expr)
404 : : {
405 : 315 : HIR::ArrayElems &elements = expr.get_internal_elements ();
406 : 315 : switch (elements.get_array_expr_type ())
407 : : {
408 : 208 : case HIR::ArrayElems::ArrayExprType::VALUES: {
409 : 208 : auto &elems = static_cast<HIR::ArrayElemsValues &> (elements);
410 : 1395 : for (auto &value : elems.get_values ())
411 : 1187 : value->accept_vis (*this);
412 : : }
413 : : return;
414 : :
415 : 107 : case HIR::ArrayElems::ArrayExprType::COPIED:
416 : 107 : auto &elems = static_cast<HIR::ArrayElemsCopied &> (elements);
417 : 107 : elems.get_elem_to_copy ().accept_vis (*this);
418 : : }
419 : : }
420 : :
421 : : void
422 : 213 : PrivacyReporter::visit (HIR::ArrayIndexExpr &expr)
423 : : {
424 : 213 : expr.get_array_expr ().accept_vis (*this);
425 : 213 : expr.get_index_expr ().accept_vis (*this);
426 : 213 : }
427 : :
428 : : void
429 : 326 : PrivacyReporter::visit (HIR::TupleExpr &expr)
430 : : {
431 : 944 : for (auto &value : expr.get_tuple_elems ())
432 : 618 : value->accept_vis (*this);
433 : 326 : }
434 : :
435 : : void
436 : 763 : PrivacyReporter::visit (HIR::TupleIndexExpr &expr)
437 : : {
438 : 763 : expr.get_tuple_expr ().accept_vis (*this);
439 : 763 : }
440 : :
441 : : void
442 : 52 : PrivacyReporter::visit (HIR::StructExprStruct &)
443 : : {
444 : : // FIXME: We need to check the visibility of the type it refers to here
445 : 52 : }
446 : :
447 : : void
448 : 215 : PrivacyReporter::visit (HIR::StructExprFieldIdentifier &)
449 : 215 : {}
450 : :
451 : : void
452 : 2017 : PrivacyReporter::visit (HIR::StructExprFieldIdentifierValue &field)
453 : : {
454 : 2017 : field.get_value ().accept_vis (*this);
455 : 2017 : }
456 : :
457 : : void
458 : 42 : PrivacyReporter::visit (HIR::StructExprFieldIndexValue &field)
459 : : {
460 : 42 : field.get_value ().accept_vis (*this);
461 : 42 : }
462 : :
463 : : void
464 : 901 : PrivacyReporter::visit (HIR::StructExprStructFields &expr)
465 : : {
466 : 3175 : for (auto &field : expr.get_fields ())
467 : 2274 : field->accept_vis (*this);
468 : 901 : }
469 : :
470 : : void
471 : 6631 : PrivacyReporter::visit (HIR::CallExpr &expr)
472 : : {
473 : 6631 : expr.get_fnexpr ().accept_vis (*this);
474 : :
475 : 14302 : for (auto ¶m : expr.get_arguments ())
476 : 7671 : param->accept_vis (*this);
477 : 6631 : }
478 : :
479 : : void
480 : 1100 : PrivacyReporter::visit (HIR::MethodCallExpr &expr)
481 : : {
482 : 1100 : expr.get_receiver ().accept_vis (*this);
483 : :
484 : 1554 : for (auto ¶m : expr.get_arguments ())
485 : 454 : param->accept_vis (*this);
486 : 1100 : }
487 : :
488 : : void
489 : 2108 : PrivacyReporter::visit (HIR::FieldAccessExpr &expr)
490 : : {
491 : 2108 : expr.get_receiver_expr ().accept_vis (*this);
492 : :
493 : : // FIXME: We should also check if the field is public?
494 : 2108 : }
495 : :
496 : : void
497 : 52 : PrivacyReporter::visit (HIR::ClosureExpr &)
498 : : {
499 : : // Not handled yet
500 : 52 : }
501 : :
502 : : void
503 : 12849 : PrivacyReporter::visit (HIR::BlockExpr &expr)
504 : : {
505 : 29003 : for (auto &stmt : expr.get_statements ())
506 : 16154 : stmt->accept_vis (*this);
507 : :
508 : 12849 : if (expr.has_final_expr ())
509 : 8478 : expr.get_final_expr ().accept_vis (*this);
510 : 12849 : }
511 : :
512 : : void
513 : 8 : PrivacyReporter::visit (HIR::ContinueExpr &)
514 : 8 : {}
515 : :
516 : : void
517 : 44 : PrivacyReporter::visit (HIR::BreakExpr &expr)
518 : : {
519 : 44 : if (expr.has_break_expr ())
520 : 16 : expr.get_expr ().accept_vis (*this);
521 : 44 : }
522 : :
523 : : void
524 : 50 : PrivacyReporter::visit (HIR::RangeFromToExpr &expr)
525 : : {
526 : 50 : expr.get_from_expr ().accept_vis (*this);
527 : 50 : expr.get_to_expr ().accept_vis (*this);
528 : 50 : }
529 : :
530 : : void
531 : 7 : PrivacyReporter::visit (HIR::RangeFromExpr &expr)
532 : : {
533 : 7 : expr.get_from_expr ().accept_vis (*this);
534 : 7 : }
535 : :
536 : : void
537 : 7 : PrivacyReporter::visit (HIR::RangeToExpr &expr)
538 : : {
539 : 7 : expr.get_to_expr ().accept_vis (*this);
540 : 7 : }
541 : :
542 : : void
543 : 0 : PrivacyReporter::visit (HIR::RangeFullExpr &)
544 : 0 : {}
545 : :
546 : : void
547 : 7 : PrivacyReporter::visit (HIR::RangeFromToInclExpr &expr)
548 : : {
549 : 7 : expr.get_from_expr ().accept_vis (*this);
550 : 7 : expr.get_to_expr ().accept_vis (*this);
551 : 7 : }
552 : :
553 : : void
554 : 0 : PrivacyReporter::visit (HIR::RangeToInclExpr &)
555 : : {
556 : : // Not handled yet
557 : 0 : }
558 : :
559 : : void
560 : 330 : PrivacyReporter::visit (HIR::ReturnExpr &expr)
561 : : {
562 : 330 : if (expr.has_expr ())
563 : 305 : expr.get_expr ().accept_vis (*this);
564 : 330 : }
565 : :
566 : : void
567 : 2316 : PrivacyReporter::visit (HIR::UnsafeBlockExpr &expr)
568 : : {
569 : 2316 : expr.get_block_expr ().accept_vis (*this);
570 : 2316 : }
571 : :
572 : : void
573 : 95 : PrivacyReporter::visit (HIR::LoopExpr &expr)
574 : : {
575 : 95 : expr.get_loop_block ().accept_vis (*this);
576 : 95 : }
577 : :
578 : : void
579 : 43 : PrivacyReporter::visit (HIR::WhileLoopExpr &expr)
580 : : {
581 : 43 : expr.get_predicate_expr ().accept_vis (*this);
582 : 43 : expr.get_loop_block ().accept_vis (*this);
583 : 43 : }
584 : :
585 : : void
586 : 0 : PrivacyReporter::visit (HIR::WhileLetLoopExpr &expr)
587 : : {
588 : 0 : expr.get_cond ().accept_vis (*this);
589 : 0 : expr.get_loop_block ().accept_vis (*this);
590 : 0 : }
591 : :
592 : : void
593 : 320 : PrivacyReporter::visit (HIR::IfExpr &expr)
594 : : {
595 : 320 : expr.get_if_condition ().accept_vis (*this);
596 : 320 : expr.get_if_block ().accept_vis (*this);
597 : 320 : }
598 : :
599 : : void
600 : 355 : PrivacyReporter::visit (HIR::IfExprConseqElse &expr)
601 : : {
602 : 355 : expr.get_if_condition ().accept_vis (*this);
603 : 355 : expr.get_if_block ().accept_vis (*this);
604 : 355 : expr.get_else_block ().accept_vis (*this);
605 : 355 : }
606 : :
607 : : void
608 : 329 : PrivacyReporter::visit (HIR::MatchExpr &expr)
609 : : {
610 : 329 : expr.get_scrutinee_expr ().accept_vis (*this);
611 : 329 : }
612 : :
613 : : void
614 : 0 : PrivacyReporter::visit (HIR::AwaitExpr &)
615 : : {
616 : : // Not handled yet
617 : 0 : }
618 : :
619 : : void
620 : 0 : PrivacyReporter::visit (HIR::AsyncBlockExpr &)
621 : : {
622 : : // Not handled yet
623 : 0 : }
624 : :
625 : : void
626 : 492 : PrivacyReporter::visit (HIR::Module &module)
627 : : {
628 : : // FIXME: We also need to think about module privacy
629 : :
630 : 492 : auto old_module = current_module;
631 : 492 : current_module = module.get_mappings ().get_nodeid ();
632 : :
633 : 1130 : for (auto &item : module.get_items ())
634 : 638 : item->accept_vis (*this);
635 : :
636 : 492 : current_module = old_module;
637 : 492 : }
638 : :
639 : : void
640 : 0 : PrivacyReporter::visit (HIR::ExternCrate &)
641 : 0 : {}
642 : :
643 : : void
644 : 0 : PrivacyReporter::visit (HIR::UseDeclaration &)
645 : : {
646 : : // FIXME: Is there anything we need to do here?
647 : 0 : }
648 : :
649 : : void
650 : 9038 : PrivacyReporter::visit (HIR::Function &function)
651 : : {
652 : 12210 : for (auto ¶m : function.get_function_params ())
653 : 3172 : check_type_privacy (param.get_type ());
654 : :
655 : 9038 : function.get_definition ().accept_vis (*this);
656 : 9038 : }
657 : :
658 : : void
659 : 833 : PrivacyReporter::visit (HIR::TypeAlias &)
660 : : {
661 : : // TODO: Check the type here
662 : 833 : }
663 : :
664 : : void
665 : 1034 : PrivacyReporter::visit (HIR::StructStruct &)
666 : : {
667 : : // TODO: Check the type of all fields
668 : 1034 : }
669 : :
670 : : void
671 : 783 : PrivacyReporter::visit (HIR::TupleStruct &)
672 : : {
673 : : // TODO: Check the type of all fields
674 : 783 : }
675 : :
676 : : void
677 : 0 : PrivacyReporter::visit (HIR::EnumItem &)
678 : : {
679 : : // TODO: Check the type of all variants
680 : 0 : }
681 : :
682 : : void
683 : 0 : PrivacyReporter::visit (HIR::EnumItemTuple &)
684 : : {
685 : : // TODO: Check the type
686 : 0 : }
687 : :
688 : : void
689 : 0 : PrivacyReporter::visit (HIR::EnumItemStruct &)
690 : : {
691 : : // TODO: Check the type
692 : 0 : }
693 : :
694 : : void
695 : 0 : PrivacyReporter::visit (HIR::EnumItemDiscriminant &)
696 : 0 : {}
697 : :
698 : : void
699 : 206 : PrivacyReporter::visit (HIR::Enum &)
700 : 206 : {}
701 : :
702 : : void
703 : 94 : PrivacyReporter::visit (HIR::Union &)
704 : : {
705 : : // TODO: Check the type
706 : 94 : }
707 : :
708 : : void
709 : 479 : PrivacyReporter::visit (HIR::ConstantItem &const_item)
710 : : {
711 : : // TODO: We need to visit the type
712 : 479 : const_item.get_expr ().accept_vis (*this);
713 : 479 : }
714 : :
715 : : void
716 : 50 : PrivacyReporter::visit (HIR::StaticItem &static_item)
717 : : {
718 : : // TODO: We need to visit the type
719 : 50 : static_item.get_expr ().accept_vis (*this);
720 : 50 : }
721 : :
722 : : void
723 : 2295 : PrivacyReporter::visit (HIR::Trait &)
724 : : {
725 : : // FIXME: We need to be an ItemVisitor as well
726 : : // for (auto &item : trait.get_trait_items ())
727 : : // item->accept_vis (*this);
728 : 2295 : }
729 : :
730 : : void
731 : 3296 : PrivacyReporter::visit (HIR::ImplBlock &impl)
732 : : {
733 : 7707 : for (auto &item : impl.get_impl_items ())
734 : 4411 : item->accept_vis (*this);
735 : 3296 : }
736 : :
737 : : void
738 : 1089 : PrivacyReporter::visit (HIR::ExternBlock &)
739 : : {
740 : : // FIXME: We need to be an ItemVisitor as well
741 : : // for (auto &block: block.get_extern_items ())
742 : : // item->accept_vis (*this);
743 : 1089 : }
744 : :
745 : : void
746 : 46 : PrivacyReporter::visit (HIR::EmptyStmt &)
747 : 46 : {}
748 : :
749 : : void
750 : 9794 : PrivacyReporter::visit (HIR::LetStmt &stmt)
751 : : {
752 : 9794 : if (stmt.has_type ())
753 : 1845 : check_type_privacy (stmt.get_type ());
754 : :
755 : 9794 : if (stmt.has_init_expr ())
756 : 8761 : stmt.get_init_expr ().accept_vis (*this);
757 : 9794 : }
758 : :
759 : : void
760 : 6023 : PrivacyReporter::visit (HIR::ExprStmt &stmt)
761 : : {
762 : 6023 : stmt.get_expr ().accept_vis (*this);
763 : 6023 : }
764 : :
765 : : } // namespace Privacy
766 : : } // namespace Rust
|