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-cfg-strip.h"
20 : : #include "rust-ast-full.h"
21 : : #include "rust-ast-visitor.h"
22 : : #include "rust-path.h"
23 : : #include "rust-session-manager.h"
24 : : #include "rust-attribute-values.h"
25 : : #include "rust-macro-expand.h"
26 : :
27 : : namespace Rust {
28 : :
29 : : /**
30 : : * Determines whether any cfg predicate is false and hence item with attributes
31 : : * should be stripped. Note that attributes must be expanded before calling.
32 : : */
33 : : bool
34 : 0 : CfgStrip::fails_cfg (const AST::AttrVec &attrs) const
35 : : {
36 : 0 : auto &session = Session::get_instance ();
37 : :
38 : 0 : for (const auto &attr : attrs)
39 : : {
40 : 0 : if (attr.get_path () == Values::Attributes::CFG
41 : 0 : && !attr.check_cfg_predicate (session))
42 : : return true;
43 : 0 : else if (!expansion_cfg.should_test
44 : 0 : && attr.get_path () == Values::Attributes::TEST)
45 : : return true;
46 : : }
47 : : return false;
48 : : }
49 : :
50 : : /**
51 : : * Determines whether any cfg predicate is false and hence item with attributes
52 : : * should be stripped. Will expand attributes as well.
53 : : */
54 : : bool
55 : 64636847 : CfgStrip::fails_cfg_with_expand (AST::AttrVec &attrs) const
56 : : {
57 : 64636847 : auto &session = Session::get_instance ();
58 : :
59 : : // TODO: maybe have something that strips cfg attributes that evaluate true?
60 : 68467239 : for (auto &attr : attrs)
61 : : {
62 : 3830732 : if (attr.get_path () == Values::Attributes::CFG)
63 : : {
64 : 7416 : if (!attr.is_parsed_to_meta_item ())
65 : 805 : attr.parse_attr_to_meta_item ();
66 : :
67 : : // DEBUG
68 : 7416 : if (!attr.is_parsed_to_meta_item ())
69 : 0 : rust_debug ("failed to parse attr to meta item, right before "
70 : : "cfg predicate check");
71 : : else
72 : 7416 : rust_debug ("attr has been successfully parsed to meta item, "
73 : : "right before cfg predicate check");
74 : :
75 : 7416 : if (!attr.check_cfg_predicate (session))
76 : : {
77 : : // DEBUG
78 : 339 : rust_debug (
79 : : "cfg predicate failed for attribute: \033[0;31m'%s'\033[0m",
80 : : attr.as_string ().c_str ());
81 : :
82 : 339 : return true;
83 : : }
84 : : else
85 : : {
86 : : // DEBUG
87 : 7077 : rust_debug ("cfg predicate succeeded for attribute: "
88 : : "\033[0;31m'%s'\033[0m",
89 : : attr.as_string ().c_str ());
90 : : }
91 : : }
92 : 3823316 : else if (!expansion_cfg.should_test
93 : 7646632 : && attr.get_path () == Values::Attributes::TEST)
94 : : return true;
95 : : }
96 : : return false;
97 : : }
98 : :
99 : : /**
100 : : * Expands cfg_attr attributes.
101 : : */
102 : : void
103 : 64636847 : expand_cfg_attrs (AST::AttrVec &attrs)
104 : : {
105 : 64636847 : auto &session = Session::get_instance ();
106 : :
107 : 66952242 : for (std::size_t i = 0; i < attrs.size (); i++)
108 : : {
109 : 2315395 : auto &attr = attrs[i];
110 : 2315395 : if (attr.get_path () == Values::Attributes::CFG_ATTR)
111 : : {
112 : 24689 : if (!attr.is_parsed_to_meta_item ())
113 : 714 : attr.parse_attr_to_meta_item ();
114 : :
115 : 24689 : if (attr.check_cfg_predicate (session))
116 : : {
117 : : // split off cfg_attr
118 : 20 : AST::AttrVec new_attrs = attr.separate_cfg_attrs ();
119 : :
120 : : // remove attr from vector
121 : 20 : attrs.erase (attrs.begin () + i);
122 : :
123 : : // add new attrs to vector
124 : 20 : attrs.insert (attrs.begin () + i,
125 : : std::make_move_iterator (new_attrs.begin ()),
126 : : std::make_move_iterator (new_attrs.end ()));
127 : 20 : }
128 : :
129 : : /* do something - if feature (first token in tree) is in fact enabled,
130 : : * make tokens listed afterwards into attributes. i.e.: for
131 : : * [cfg_attr(feature = "wow", wow1, wow2)], if "wow" is true, then add
132 : : * attributes [wow1] and [wow2] to attribute list. This can also be
133 : : * recursive, so check for expanded attributes being recursive and
134 : : * possibly recursively call the expand_attrs? */
135 : : }
136 : : else
137 : : {
138 : 2290706 : i++;
139 : : }
140 : : }
141 : 64636847 : attrs.shrink_to_fit ();
142 : 64636847 : }
143 : :
144 : : void
145 : 10340 : CfgStrip::go (AST::Crate &crate)
146 : : {
147 : 10340 : visit (crate);
148 : 10340 : }
149 : :
150 : : void
151 : 10340 : CfgStrip::visit (AST::Crate &crate)
152 : : {
153 : : // expand crate cfg_attr attributes
154 : 10340 : expand_cfg_attrs (crate.inner_attrs);
155 : :
156 : 10340 : if (fails_cfg_with_expand (crate.inner_attrs))
157 : : {
158 : : // basically, delete whole crate
159 : 1 : crate.strip_crate ();
160 : : // TODO: maybe create warning here? probably not desired behaviour
161 : : }
162 : :
163 : 10340 : auto &items = crate.items;
164 : :
165 : 10340 : AST::DefaultASTVisitor::visit (crate);
166 : 57348 : for (auto it = items.begin (); it != items.end ();)
167 : : {
168 : 47008 : auto &item = *it;
169 : 47008 : if (item->is_marked_for_strip ())
170 : 18 : it = items.erase (it);
171 : : else
172 : 46990 : it++;
173 : : }
174 : : // expand module attributes?
175 : 10340 : }
176 : :
177 : : // Visitor used to expand attributes.
178 : : void
179 : 112735 : CfgStrip::maybe_strip_struct_fields (std::vector<AST::StructField> &fields)
180 : : {
181 : 137988 : for (auto it = fields.begin (); it != fields.end ();)
182 : : {
183 : 25253 : auto &field = *it;
184 : :
185 : 25253 : auto &field_attrs = field.get_outer_attrs ();
186 : 25253 : expand_cfg_attrs (field_attrs);
187 : 25253 : if (fails_cfg_with_expand (field_attrs))
188 : : {
189 : 2 : it = fields.erase (it);
190 : 2 : continue;
191 : : }
192 : :
193 : : // expand sub-types of type, but can't strip type itself
194 : 25251 : auto &type = field.get_field_type ();
195 : 25251 : type.accept_vis (*this);
196 : :
197 : 25251 : if (type.is_marked_for_strip ())
198 : 0 : rust_error_at (type.get_locus (), "cannot strip type in this position");
199 : :
200 : : // if nothing else happens, increment
201 : 25251 : ++it;
202 : : }
203 : 112735 : }
204 : :
205 : : void
206 : 53665 : CfgStrip::maybe_strip_struct_expr_fields (
207 : : std::vector<std::unique_ptr<AST::StructExprField>> &fields)
208 : : {
209 : 153960 : for (auto it = fields.begin (); it != fields.end ();)
210 : : {
211 : 100295 : auto &field = *it;
212 : :
213 : 100295 : auto &field_attrs = field->get_outer_attrs ();
214 : 100295 : expand_cfg_attrs (field_attrs);
215 : 100295 : if (fails_cfg_with_expand (field_attrs))
216 : : {
217 : 1 : it = fields.erase (it);
218 : 1 : continue;
219 : : }
220 : :
221 : 100294 : ++it;
222 : : }
223 : 53665 : }
224 : :
225 : : void
226 : 10155 : CfgStrip::maybe_strip_tuple_fields (std::vector<AST::TupleField> &fields)
227 : : {
228 : 35653 : for (auto it = fields.begin (); it != fields.end ();)
229 : : {
230 : 25498 : auto &field = *it;
231 : :
232 : 25498 : auto &field_attrs = field.get_outer_attrs ();
233 : 25498 : expand_cfg_attrs (field_attrs);
234 : 25498 : if (fails_cfg_with_expand (field_attrs))
235 : : {
236 : 0 : it = fields.erase (it);
237 : 0 : continue;
238 : : }
239 : :
240 : : // expand sub-types of type, but can't strip type itself
241 : 25498 : auto &type = field.get_field_type ();
242 : 25498 : type.accept_vis (*this);
243 : 25498 : if (type.is_marked_for_strip ())
244 : 0 : rust_error_at (type.get_locus (), "cannot strip type in this position");
245 : :
246 : : // if nothing else happens, increment
247 : 25498 : ++it;
248 : : }
249 : 10155 : }
250 : :
251 : : void
252 : 800181 : CfgStrip::maybe_strip_function_params (
253 : : std::vector<std::unique_ptr<AST::Param>> ¶ms)
254 : : {
255 : 2204139 : for (auto it = params.begin (); it != params.end ();)
256 : : {
257 : 1403958 : if (!(*it)->is_self () && !(*it)->is_variadic ())
258 : : {
259 : 817091 : auto param = static_cast<AST::FunctionParam *> (it->get ());
260 : :
261 : 817091 : auto ¶m_attrs = param->get_outer_attrs ();
262 : 817091 : expand_cfg_attrs (param_attrs);
263 : 817091 : if (fails_cfg_with_expand (param_attrs))
264 : : {
265 : 0 : it = params.erase (it);
266 : 0 : continue;
267 : : }
268 : :
269 : : // TODO: should an unwanted strip lead to break out of loop?
270 : 817091 : auto &pattern = param->get_pattern ();
271 : 817091 : pattern.accept_vis (*this);
272 : 817091 : if (pattern.is_marked_for_strip ())
273 : 0 : rust_error_at (pattern.get_locus (),
274 : : "cannot strip pattern in this position");
275 : :
276 : 817091 : auto &type = param->get_type ();
277 : 817091 : type.accept_vis (*this);
278 : :
279 : 817091 : if (type.is_marked_for_strip ())
280 : 0 : rust_error_at (type.get_locus (),
281 : : "cannot strip type in this position");
282 : : }
283 : : // increment
284 : 1403958 : ++it;
285 : : }
286 : 800181 : }
287 : :
288 : : void
289 : 1308686 : CfgStrip::maybe_strip_generic_args (AST::GenericArgs &args)
290 : : {
291 : : // lifetime args can't be expanded
292 : : // FIXME: Can we have macro invocations for lifetimes?
293 : :
294 : : // expand type args - strip sub-types only
295 : 2558412 : for (auto &arg : args.get_generic_args ())
296 : : {
297 : 1249726 : switch (arg.get_kind ())
298 : : {
299 : 352818 : case AST::GenericArg::Kind::Type:
300 : 352818 : {
301 : 352818 : auto &type = arg.get_type ();
302 : 352818 : type.accept_vis (*this);
303 : :
304 : 352818 : if (type.is_marked_for_strip ())
305 : 0 : rust_error_at (type.get_locus (),
306 : : "cannot strip type in this position");
307 : : break;
308 : : }
309 : 392 : case AST::GenericArg::Kind::Const:
310 : 392 : {
311 : 392 : auto &expr = arg.get_expression ();
312 : 392 : expr.accept_vis (*this);
313 : :
314 : 392 : if (expr.is_marked_for_strip ())
315 : 0 : rust_error_at (expr.get_locus (),
316 : : "cannot strip expression in this position");
317 : : break;
318 : : }
319 : : default:
320 : : break;
321 : : // FIXME: Figure out what to do here if there is ambiguity. Since the
322 : : // resolver comes after the expansion, we need to figure out a way to
323 : : // strip ambiguous values here
324 : : // TODO: Arthur: Probably add a `mark_as_strip` method to `GenericArg`
325 : : // or something. This would clean up this whole thing
326 : : }
327 : : }
328 : :
329 : : // FIXME: Can we have macro invocations in generic type bindings?
330 : : // expand binding args - strip sub-types only
331 : 1325027 : for (auto &binding : args.get_binding_args ())
332 : : {
333 : 16341 : auto &type = binding.get_type ();
334 : 16341 : type.accept_vis (*this);
335 : :
336 : 16341 : if (type.is_marked_for_strip ())
337 : 0 : rust_error_at (type.get_locus (), "cannot strip type in this position");
338 : : }
339 : 1308686 : }
340 : :
341 : : void
342 : 217926 : CfgStrip::maybe_strip_qualified_path_type (AST::QualifiedPathType &path_type)
343 : : {
344 : 217926 : auto &type = path_type.get_type ();
345 : 217926 : type.accept_vis (*this);
346 : :
347 : 217926 : if (type.is_marked_for_strip ())
348 : 0 : rust_error_at (type.get_locus (), "cannot strip type in this position");
349 : :
350 : 217926 : if (path_type.has_as_clause ())
351 : : {
352 : 208202 : auto &type_path = path_type.get_as_type_path ();
353 : 208202 : visit (type_path);
354 : 208202 : if (type_path.is_marked_for_strip ())
355 : 0 : rust_error_at (type_path.get_locus (),
356 : : "cannot strip type path in this position");
357 : : }
358 : 217926 : }
359 : :
360 : : void
361 : 45050 : CfgStrip::CfgStrip::maybe_strip_closure_params (
362 : : std::vector<AST::ClosureParam> ¶ms)
363 : : {
364 : 97590 : for (auto it = params.begin (); it != params.end ();)
365 : : {
366 : 52540 : auto ¶m = *it;
367 : :
368 : 52540 : auto ¶m_attrs = param.get_outer_attrs ();
369 : 52540 : expand_cfg_attrs (param_attrs);
370 : 52540 : if (fails_cfg_with_expand (param_attrs))
371 : : {
372 : 0 : it = params.erase (it);
373 : 0 : continue;
374 : : }
375 : :
376 : 52540 : auto &pattern = param.get_pattern ();
377 : 52540 : pattern.accept_vis (*this);
378 : 52540 : if (pattern.is_marked_for_strip ())
379 : 0 : rust_error_at (pattern.get_locus (),
380 : : "cannot strip pattern in this position");
381 : :
382 : 52540 : if (param.has_type_given ())
383 : : {
384 : 2612 : auto &type = param.get_type ();
385 : 2612 : type.accept_vis (*this);
386 : :
387 : 2612 : if (type.is_marked_for_strip ())
388 : 0 : rust_error_at (type.get_locus (),
389 : : "cannot strip type in this position");
390 : : }
391 : :
392 : : // increment if found nothing else so far
393 : 52540 : ++it;
394 : : }
395 : 45050 : }
396 : :
397 : : void
398 : 0 : CfgStrip::maybe_strip_where_clause (AST::WhereClause &where_clause)
399 : : {
400 : : // items cannot be stripped conceptually, so just accept visitor
401 : 0 : for (auto &item : where_clause.get_items ())
402 : 0 : item->accept_vis (*this);
403 : 0 : }
404 : :
405 : : void
406 : 14264491 : CfgStrip::visit (AST::IdentifierExpr &ident_expr)
407 : : {
408 : : // strip test based on outer attrs
409 : 14264491 : AST::DefaultASTVisitor::visit (ident_expr);
410 : 14264491 : expand_cfg_attrs (ident_expr.get_outer_attrs ());
411 : 14264491 : if (fails_cfg_with_expand (ident_expr.get_outer_attrs ()))
412 : : {
413 : 0 : ident_expr.mark_for_strip ();
414 : 0 : return;
415 : : }
416 : : }
417 : :
418 : : void
419 : 121326 : CfgStrip::visit (AST::MacroInvocation ¯o_invoc)
420 : : {
421 : : // initial strip test based on outer attrs
422 : 121326 : expand_cfg_attrs (macro_invoc.get_outer_attrs ());
423 : 121326 : if (fails_cfg_with_expand (macro_invoc.get_outer_attrs ()))
424 : : {
425 : 18 : macro_invoc.mark_for_strip ();
426 : 18 : return;
427 : : }
428 : :
429 : : // can't strip simple path
430 : :
431 : : // I don't think any macro token trees can be stripped in any way
432 : :
433 : : // TODO: maybe have cfg! macro stripping behaviour here?
434 : : }
435 : :
436 : : void
437 : 6098218 : CfgStrip::visit (AST::PathInExpression &path)
438 : : {
439 : : // initial strip test based on outer attrs
440 : 6098218 : expand_cfg_attrs (path.get_outer_attrs ());
441 : 6098218 : if (fails_cfg_with_expand (path.get_outer_attrs ()))
442 : : {
443 : 0 : path.mark_for_strip ();
444 : 0 : return;
445 : : }
446 : :
447 : 6098218 : if (!path.is_lang_item ())
448 : : {
449 : 13709937 : for (auto &segment : path.get_segments ())
450 : : {
451 : 15249654 : if (segment.has_generic_args ())
452 : 40214 : maybe_strip_generic_args (segment.get_generic_args ());
453 : : }
454 : : }
455 : : }
456 : :
457 : : void
458 : 1265868 : CfgStrip::visit (AST::TypePathSegmentGeneric &segment)
459 : : {
460 : : // TODO: strip inside generic args
461 : :
462 : 1265868 : if (!segment.has_generic_args ())
463 : : return;
464 : :
465 : 1265864 : maybe_strip_generic_args (segment.get_generic_args ());
466 : : }
467 : : void
468 : 41643 : CfgStrip::visit (AST::TypePathSegmentFunction &segment)
469 : : {
470 : 41643 : AST::DefaultASTVisitor::visit (segment);
471 : 41643 : auto &type_path_function = segment.get_type_path_function ();
472 : :
473 : 103331 : for (auto &type : type_path_function.get_params ())
474 : : {
475 : 61688 : if (type->is_marked_for_strip ())
476 : 0 : rust_error_at (type->get_locus (),
477 : : "cannot strip type in this position");
478 : : }
479 : :
480 : 41643 : if (type_path_function.has_return_type ())
481 : : {
482 : 40278 : auto &return_type = type_path_function.get_return_type ();
483 : :
484 : 40278 : if (return_type.is_marked_for_strip ())
485 : 0 : rust_error_at (return_type.get_locus (),
486 : : "cannot strip type in this position");
487 : : }
488 : 41643 : }
489 : :
490 : : void
491 : 11982 : CfgStrip::visit (AST::QualifiedPathInExpression &path)
492 : : {
493 : : // initial strip test based on outer attrs
494 : 11982 : AST::DefaultASTVisitor::visit (path);
495 : :
496 : 11982 : expand_cfg_attrs (path.get_outer_attrs ());
497 : 11982 : if (fails_cfg_with_expand (path.get_outer_attrs ()))
498 : : {
499 : 0 : path.mark_for_strip ();
500 : 0 : return;
501 : : }
502 : :
503 : 11982 : maybe_strip_qualified_path_type (path.get_qualified_path_type ());
504 : :
505 : 23964 : for (auto &segment : path.get_segments ())
506 : : {
507 : 23964 : if (segment.has_generic_args ())
508 : 0 : maybe_strip_generic_args (segment.get_generic_args ());
509 : : }
510 : : }
511 : :
512 : : void
513 : 205944 : CfgStrip::visit (AST::QualifiedPathInType &path)
514 : : {
515 : 205944 : maybe_strip_qualified_path_type (path.get_qualified_path_type ());
516 : :
517 : : // this shouldn't strip any segments, but can strip inside them
518 : 205944 : AST::DefaultASTVisitor::visit (path);
519 : 205944 : }
520 : :
521 : : void
522 : 19401615 : CfgStrip::visit (AST::LiteralExpr &expr)
523 : : {
524 : : // initial strip test based on outer attrs
525 : 19401615 : expand_cfg_attrs (expr.get_outer_attrs ());
526 : 19401615 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
527 : : {
528 : 0 : expr.mark_for_strip ();
529 : 0 : return;
530 : : }
531 : : }
532 : :
533 : : void
534 : 871280 : CfgStrip::visit (AST::BorrowExpr &expr)
535 : : {
536 : 871280 : AST::DefaultASTVisitor::visit (expr);
537 : : // initial strip test based on outer attrs
538 : 871280 : expand_cfg_attrs (expr.get_outer_attrs ());
539 : 871280 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
540 : : {
541 : 0 : expr.mark_for_strip ();
542 : 0 : return;
543 : : }
544 : :
545 : : /* strip any internal sub-expressions - expression itself isn't
546 : : * allowed to have external attributes in this position so can't be
547 : : * stripped. */
548 : 871280 : auto &borrowed_expr = expr.get_borrowed_expr ();
549 : 871280 : if (borrowed_expr.is_marked_for_strip ())
550 : 0 : rust_error_at (borrowed_expr.get_locus (),
551 : : "cannot strip expression in this position - outer "
552 : : "attributes not allowed");
553 : : }
554 : : void
555 : 807761 : CfgStrip::visit (AST::DereferenceExpr &expr)
556 : : {
557 : : // initial strip test based on outer attrs
558 : 807761 : expand_cfg_attrs (expr.get_outer_attrs ());
559 : 807761 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
560 : : {
561 : 0 : expr.mark_for_strip ();
562 : 0 : return;
563 : : }
564 : :
565 : : /* strip any internal sub-expressions - expression itself isn't
566 : : * allowed to have external attributes in this position so can't be
567 : : * stripped. */
568 : 807761 : auto &dereferenced_expr = expr.get_dereferenced_expr ();
569 : 807761 : dereferenced_expr.accept_vis (*this);
570 : 807761 : if (dereferenced_expr.is_marked_for_strip ())
571 : 0 : rust_error_at (dereferenced_expr.get_locus (),
572 : : "cannot strip expression in this position - outer "
573 : : "attributes not allowed");
574 : : }
575 : : void
576 : 63562 : CfgStrip::visit (AST::ErrorPropagationExpr &expr)
577 : : {
578 : 63562 : AST::DefaultASTVisitor::visit (expr);
579 : :
580 : : // initial strip test based on outer attrs
581 : 63562 : expand_cfg_attrs (expr.get_outer_attrs ());
582 : 63562 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
583 : : {
584 : 0 : expr.mark_for_strip ();
585 : 0 : return;
586 : : }
587 : :
588 : : /* strip any internal sub-expressions - expression itself isn't
589 : : * allowed to have external attributes in this position so can't be
590 : : * stripped. */
591 : 63562 : auto &propagating_expr = expr.get_propagating_expr ();
592 : 63562 : if (propagating_expr.is_marked_for_strip ())
593 : 0 : rust_error_at (propagating_expr.get_locus (),
594 : : "cannot strip expression in this position - outer "
595 : : "attributes not allowed");
596 : : }
597 : : void
598 : 83775 : CfgStrip::visit (AST::NegationExpr &expr)
599 : : {
600 : 83775 : AST::DefaultASTVisitor::visit (expr);
601 : : // initial strip test based on outer attrs
602 : 83775 : expand_cfg_attrs (expr.get_outer_attrs ());
603 : 83775 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
604 : : {
605 : 0 : expr.mark_for_strip ();
606 : 0 : return;
607 : : }
608 : :
609 : : /* strip any internal sub-expressions - expression itself isn't
610 : : * allowed to have external attributes in this position so can't be
611 : : * stripped. */
612 : 83775 : auto &negated_expr = expr.get_negated_expr ();
613 : 83775 : if (negated_expr.is_marked_for_strip ())
614 : 0 : rust_error_at (negated_expr.get_locus (),
615 : : "cannot strip expression in this position - outer "
616 : : "attributes not allowed");
617 : : }
618 : : void
619 : 2557085 : CfgStrip::visit (AST::ArithmeticOrLogicalExpr &expr)
620 : : {
621 : 2557085 : AST::DefaultASTVisitor::visit (expr);
622 : : /* outer attributes never allowed before these. while cannot strip
623 : : * two direct descendant expressions, can strip ones below that */
624 : :
625 : : // ensure that they are not marked for strip
626 : 2557085 : if (expr.get_left_expr ().is_marked_for_strip ())
627 : 0 : rust_error_at (expr.get_left_expr ().get_locus (),
628 : : "cannot strip expression in this position - outer "
629 : : "attributes are never allowed "
630 : : "before binary op exprs");
631 : 2557085 : if (expr.get_right_expr ().is_marked_for_strip ())
632 : 0 : rust_error_at (expr.get_right_expr ().get_locus (),
633 : : "cannot strip expression in this position - outer "
634 : : "attributes not allowed");
635 : 2557085 : }
636 : :
637 : : void
638 : 252884 : CfgStrip::visit (AST::ComparisonExpr &expr)
639 : : {
640 : : /* outer attributes never allowed before these. while cannot strip
641 : : * two direct descendant expressions, can strip ones below that */
642 : 252884 : AST::DefaultASTVisitor::visit (expr);
643 : :
644 : : // ensure that they are not marked for strip
645 : 252884 : if (expr.get_left_expr ().is_marked_for_strip ())
646 : 0 : rust_error_at (expr.get_left_expr ().get_locus (),
647 : : "cannot strip expression in this position - outer "
648 : : "attributes are never allowed "
649 : : "before binary op exprs");
650 : 252884 : if (expr.get_right_expr ().is_marked_for_strip ())
651 : 0 : rust_error_at (expr.get_right_expr ().get_locus (),
652 : : "cannot strip expression in this position - outer "
653 : : "attributes not allowed");
654 : 252884 : }
655 : :
656 : : void
657 : 61330 : CfgStrip::visit (AST::LazyBooleanExpr &expr)
658 : : {
659 : : /* outer attributes never allowed before these. while cannot strip
660 : : * two direct descendant expressions, can strip ones below that */
661 : 61330 : AST::DefaultASTVisitor::visit (expr);
662 : :
663 : : // ensure that they are not marked for strip
664 : 61330 : if (expr.get_left_expr ().is_marked_for_strip ())
665 : 0 : rust_error_at (expr.get_left_expr ().get_locus (),
666 : : "cannot strip expression in this position - outer "
667 : : "attributes are never allowed "
668 : : "before binary op exprs");
669 : 61330 : if (expr.get_right_expr ().is_marked_for_strip ())
670 : 0 : rust_error_at (expr.get_right_expr ().get_locus (),
671 : : "cannot strip expression in this position - outer "
672 : : "attributes not allowed");
673 : 61330 : }
674 : :
675 : : void
676 : 516635 : CfgStrip::visit (AST::TypeCastExpr &expr)
677 : : {
678 : : /* outer attributes never allowed before these. while cannot strip
679 : : * direct descendant expression, can strip ones below that */
680 : 516635 : AST::DefaultASTVisitor::visit (expr);
681 : :
682 : 516635 : auto &casted_expr = expr.get_casted_expr ();
683 : : // ensure that they are not marked for strip
684 : 516635 : if (casted_expr.is_marked_for_strip ())
685 : 0 : rust_error_at (casted_expr.get_locus (),
686 : : "cannot strip expression in this position - outer "
687 : : "attributes are never allowed before cast exprs");
688 : :
689 : : // TODO: strip sub-types of type
690 : 516635 : auto &type = expr.get_type_to_cast_to ();
691 : 516635 : if (type.is_marked_for_strip ())
692 : 0 : rust_error_at (type.get_locus (), "cannot strip type in this position");
693 : 516635 : }
694 : : void
695 : 251975 : CfgStrip::visit (AST::AssignmentExpr &expr)
696 : : {
697 : 251975 : expand_cfg_attrs (expr.get_outer_attrs ());
698 : 251975 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
699 : : {
700 : 14 : expr.mark_for_strip ();
701 : 14 : return;
702 : : }
703 : 251961 : AST::DefaultASTVisitor::visit (expr);
704 : :
705 : : // ensure that they are not marked for strip
706 : 251961 : if (expr.get_left_expr ().is_marked_for_strip ())
707 : 0 : rust_error_at (expr.get_left_expr ().get_locus (),
708 : : "cannot strip expression in this position - outer "
709 : : "attributes are never allowed "
710 : : "before binary op exprs");
711 : 251961 : if (expr.get_right_expr ().is_marked_for_strip ())
712 : 0 : rust_error_at (expr.get_right_expr ().get_locus (),
713 : : "cannot strip expression in this position - outer "
714 : : "attributes not allowed");
715 : : }
716 : : void
717 : 140550 : CfgStrip::visit (AST::CompoundAssignmentExpr &expr)
718 : : {
719 : : /* outer attributes never allowed before these. while cannot strip
720 : : * two direct descendant expressions, can strip ones below that */
721 : 140550 : AST::DefaultASTVisitor::visit (expr);
722 : :
723 : : // ensure that they are not marked for strip
724 : 140550 : if (expr.get_left_expr ().is_marked_for_strip ())
725 : 0 : rust_error_at (expr.get_left_expr ().get_locus (),
726 : : "cannot strip expression in this position - outer "
727 : : "attributes are never allowed "
728 : : "before binary op exprs");
729 : 140550 : if (expr.get_right_expr ().is_marked_for_strip ())
730 : 0 : rust_error_at (expr.get_right_expr ().get_locus (),
731 : : "cannot strip expression in this position - outer "
732 : : "attributes not allowed");
733 : 140550 : }
734 : : void
735 : 468740 : CfgStrip::visit (AST::GroupedExpr &expr)
736 : : {
737 : : // initial strip test based on outer attrs
738 : 468740 : expand_cfg_attrs (expr.get_outer_attrs ());
739 : 468740 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
740 : : {
741 : 0 : expr.mark_for_strip ();
742 : 0 : return;
743 : : }
744 : :
745 : : /* strip test based on inner attrs - spec says these are inner
746 : : * attributes, not outer attributes of inner expr */
747 : 468740 : expand_cfg_attrs (expr.get_inner_attrs ());
748 : 468740 : if (fails_cfg_with_expand (expr.get_inner_attrs ()))
749 : : {
750 : 0 : expr.mark_for_strip ();
751 : 0 : return;
752 : : }
753 : :
754 : : /* strip any internal sub-expressions - expression itself isn't
755 : : * allowed to have external attributes in this position so can't be
756 : : * stripped. */
757 : 468740 : AST::DefaultASTVisitor::visit (expr);
758 : :
759 : 468740 : auto &inner_expr = expr.get_expr_in_parens ();
760 : 468740 : if (inner_expr.is_marked_for_strip ())
761 : 0 : rust_error_at (inner_expr.get_locus (),
762 : : "cannot strip expression in this position - outer "
763 : : "attributes not allowed");
764 : : }
765 : : void
766 : 1205523 : CfgStrip::visit (AST::ArrayElemsValues &elems)
767 : : {
768 : : /* apparently outer attributes are allowed in "elements of array
769 : : * expressions" according to spec */
770 : 1205523 : maybe_strip_pointer_allow_strip (elems.get_values ());
771 : 1205523 : }
772 : : void
773 : 5472 : CfgStrip::visit (AST::ArrayElemsCopied &elems)
774 : : {
775 : : /* apparently outer attributes are allowed in "elements of array
776 : : * expressions" according to spec. on the other hand, it would not
777 : : * make conceptual sense to be able to remove either expression. As
778 : : * such, not implementing. TODO clear up the ambiguity here */
779 : 5472 : AST::DefaultASTVisitor::visit (elems);
780 : :
781 : : // only intend stripping for internal sub-expressions
782 : 5472 : auto &copied_expr = elems.get_elem_to_copy ();
783 : 5472 : if (copied_expr.is_marked_for_strip ())
784 : 0 : rust_error_at (copied_expr.get_locus (),
785 : : "cannot strip expression in this position - outer "
786 : : "attributes not allowed");
787 : :
788 : 5472 : auto ©_count = elems.get_num_copies ();
789 : 5472 : copy_count.accept_vis (*this);
790 : 5472 : if (copy_count.is_marked_for_strip ())
791 : 0 : rust_error_at (copy_count.get_locus (),
792 : : "cannot strip expression in this position - outer "
793 : : "attributes not allowed");
794 : 5472 : }
795 : : void
796 : 1210995 : CfgStrip::visit (AST::ArrayExpr &expr)
797 : : {
798 : : // initial strip test based on outer attrs
799 : 1210995 : expand_cfg_attrs (expr.get_outer_attrs ());
800 : 1210995 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
801 : : {
802 : 0 : expr.mark_for_strip ();
803 : 0 : return;
804 : : }
805 : :
806 : : /* strip test based on inner attrs - spec says there are separate
807 : : * inner attributes, not just outer attributes of inner exprs */
808 : 1210995 : expand_cfg_attrs (expr.get_inner_attrs ());
809 : 1210995 : if (fails_cfg_with_expand (expr.get_inner_attrs ()))
810 : : {
811 : 0 : expr.mark_for_strip ();
812 : 0 : return;
813 : : }
814 : :
815 : : /* assuming you can't strip away the ArrayElems type, but can strip
816 : : * internal expressions and whatever */
817 : 1210995 : AST::DefaultASTVisitor::visit (expr);
818 : : }
819 : :
820 : : void
821 : 113006 : CfgStrip::visit (AST::ArrayIndexExpr &expr)
822 : : {
823 : : /* it is unclear whether outer attributes are supposed to be
824 : : * allowed, but conceptually it wouldn't make much sense, but
825 : : * having expansion code anyway. TODO */
826 : : // initial strip test based on outer attrs
827 : 113006 : expand_cfg_attrs (expr.get_outer_attrs ());
828 : 113006 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
829 : : {
830 : 0 : expr.mark_for_strip ();
831 : 0 : return;
832 : : }
833 : :
834 : : /* strip any internal sub-expressions - expression itself isn't
835 : : * allowed to have external attributes in this position so can't be
836 : : * stripped. */
837 : 113006 : AST::DefaultASTVisitor::visit (expr);
838 : :
839 : 113006 : const auto &array_expr = expr.get_array_expr ();
840 : 113006 : if (array_expr.is_marked_for_strip ())
841 : 0 : rust_error_at (array_expr.get_locus (),
842 : : "cannot strip expression in this position - outer "
843 : : "attributes not allowed");
844 : :
845 : 113006 : const auto &index_expr = expr.get_index_expr ();
846 : 113006 : if (index_expr.is_marked_for_strip ())
847 : 0 : rust_error_at (index_expr.get_locus (),
848 : : "cannot strip expression in this position - outer "
849 : : "attributes not allowed");
850 : : }
851 : : void
852 : 228113 : CfgStrip::visit (AST::TupleExpr &expr)
853 : : {
854 : : /* according to spec, outer attributes are allowed on "elements of
855 : : * tuple expressions" */
856 : :
857 : : // initial strip test based on outer attrs
858 : 228113 : expand_cfg_attrs (expr.get_outer_attrs ());
859 : 228113 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
860 : : {
861 : 0 : expr.mark_for_strip ();
862 : 0 : return;
863 : : }
864 : :
865 : : /* strip test based on inner attrs - spec says these are inner
866 : : * attributes, not outer attributes of inner expr */
867 : 228113 : expand_cfg_attrs (expr.get_inner_attrs ());
868 : 228113 : if (fails_cfg_with_expand (expr.get_inner_attrs ()))
869 : : {
870 : 0 : expr.mark_for_strip ();
871 : 0 : return;
872 : : }
873 : :
874 : : /* apparently outer attributes are allowed in "elements of tuple
875 : : * expressions" according to spec */
876 : 228113 : maybe_strip_pointer_allow_strip (expr.get_tuple_elems ());
877 : : }
878 : : void
879 : 412607 : CfgStrip::visit (AST::TupleIndexExpr &expr)
880 : : {
881 : : // initial strip test based on outer attrs
882 : 412607 : expand_cfg_attrs (expr.get_outer_attrs ());
883 : 412607 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
884 : : {
885 : 0 : expr.mark_for_strip ();
886 : 0 : return;
887 : : }
888 : :
889 : 412607 : AST::DefaultASTVisitor::visit (expr);
890 : : /* wouldn't strip this directly (as outer attrs should be
891 : : * associated with this level), but any sub-expressions would be
892 : : * stripped. Thus, no need to erase when strip check called. */
893 : 412607 : auto &tuple_expr = expr.get_tuple_expr ();
894 : 412607 : if (tuple_expr.is_marked_for_strip ())
895 : 0 : rust_error_at (tuple_expr.get_locus (),
896 : : "cannot strip expression in this position - outer "
897 : : "attributes not allowed");
898 : : }
899 : :
900 : : void
901 : 1930 : CfgStrip::visit (AST::StructExprStruct &expr)
902 : : {
903 : : // initial strip test based on outer attrs
904 : 1930 : expand_cfg_attrs (expr.get_outer_attrs ());
905 : 1930 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
906 : : {
907 : 0 : expr.mark_for_strip ();
908 : 0 : return;
909 : : }
910 : :
911 : : /* strip test based on inner attrs - spec says these are inner
912 : : * attributes, not outer attributes of inner expr */
913 : 1930 : expand_cfg_attrs (expr.get_inner_attrs ());
914 : 1930 : if (fails_cfg_with_expand (expr.get_inner_attrs ()))
915 : : {
916 : 0 : expr.mark_for_strip ();
917 : 0 : return;
918 : : }
919 : :
920 : : // strip sub-exprs of path
921 : 1930 : auto &struct_name = expr.get_struct_name ();
922 : 1930 : visit (struct_name);
923 : 1930 : if (struct_name.is_marked_for_strip ())
924 : 0 : rust_error_at (struct_name.get_locus (),
925 : : "cannot strip path in this position");
926 : : }
927 : :
928 : : void
929 : 86476 : CfgStrip::visit (AST::StructExprFieldIdentifierValue &field)
930 : : {
931 : : /* as no attrs possible (at moment, at least), only sub-expression
932 : : * stripping is possible */
933 : 86476 : AST::DefaultASTVisitor::visit (field);
934 : :
935 : 86476 : auto &value = field.get_value ();
936 : 86476 : if (value.is_marked_for_strip ())
937 : 0 : rust_error_at (value.get_locus (),
938 : : "cannot strip expression in this position - outer "
939 : : "attributes not allowed");
940 : 86476 : }
941 : : void
942 : 176 : CfgStrip::visit (AST::StructExprFieldIndexValue &field)
943 : : {
944 : : /* as no attrs possible (at moment, at least), only sub-expression
945 : : * stripping is possible */
946 : 176 : AST::DefaultASTVisitor::visit (field);
947 : :
948 : 176 : auto &value = field.get_value ();
949 : 176 : if (value.is_marked_for_strip ())
950 : 0 : rust_error_at (value.get_locus (),
951 : : "cannot strip expression in this position - outer "
952 : : "attributes not allowed");
953 : 176 : }
954 : : void
955 : 53665 : CfgStrip::visit (AST::StructExprStructFields &expr)
956 : : {
957 : : // initial strip test based on outer attrs
958 : 53665 : expand_cfg_attrs (expr.get_outer_attrs ());
959 : 53665 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
960 : : {
961 : 0 : expr.mark_for_strip ();
962 : 0 : return;
963 : : }
964 : :
965 : : /* strip test based on inner attrs - spec says these are inner
966 : : * attributes, not outer attributes of inner expr */
967 : 53665 : expand_cfg_attrs (expr.get_inner_attrs ());
968 : 53665 : if (fails_cfg_with_expand (expr.get_inner_attrs ()))
969 : : {
970 : 0 : expr.mark_for_strip ();
971 : 0 : return;
972 : : }
973 : :
974 : : // strip sub-exprs of path
975 : 53665 : auto &struct_name = expr.get_struct_name ();
976 : 53665 : visit (struct_name);
977 : 53665 : if (struct_name.is_marked_for_strip ())
978 : 0 : rust_error_at (struct_name.get_locus (),
979 : : "cannot strip path in this position");
980 : :
981 : : /* spec does not specify whether expressions are allowed to be
982 : : * stripped at top level of struct fields, but I wouldn't think
983 : : * that they would be, so operating under the assumption that only
984 : : * sub-expressions can be stripped. */
985 : 53665 : AST::DefaultASTVisitor::visit (expr);
986 : :
987 : : /* struct base presumably can't be stripped, as the '..' is before
988 : : * the expression. as such, can only strip sub-expressions. */
989 : 53665 : if (expr.has_struct_base ())
990 : : {
991 : 528 : auto &base_struct_expr = expr.get_struct_base ().get_base_struct ();
992 : 528 : base_struct_expr.accept_vis (*this);
993 : 528 : if (base_struct_expr.is_marked_for_strip ())
994 : 0 : rust_error_at (base_struct_expr.get_locus (),
995 : : "cannot strip expression in this position - outer "
996 : : "attributes not allowed");
997 : : }
998 : :
999 : 53665 : maybe_strip_struct_expr_fields (expr.get_fields ());
1000 : : }
1001 : :
1002 : : void
1003 : 0 : CfgStrip::visit (AST::StructExprStructBase &expr)
1004 : : {
1005 : : // initial strip test based on outer attrs
1006 : 0 : expand_cfg_attrs (expr.get_outer_attrs ());
1007 : 0 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1008 : : {
1009 : 0 : expr.mark_for_strip ();
1010 : 0 : return;
1011 : : }
1012 : :
1013 : : /* strip test based on inner attrs - spec says these are inner
1014 : : * attributes, not outer attributes of inner expr */
1015 : 0 : expand_cfg_attrs (expr.get_inner_attrs ());
1016 : 0 : if (fails_cfg_with_expand (expr.get_inner_attrs ()))
1017 : : {
1018 : 0 : expr.mark_for_strip ();
1019 : 0 : return;
1020 : : }
1021 : :
1022 : : // strip sub-exprs of path
1023 : 0 : auto &struct_name = expr.get_struct_name ();
1024 : 0 : visit (struct_name);
1025 : 0 : if (struct_name.is_marked_for_strip ())
1026 : 0 : rust_error_at (struct_name.get_locus (),
1027 : : "cannot strip path in this position");
1028 : :
1029 : : /* struct base presumably can't be stripped, as the '..' is before
1030 : : * the expression. as such, can only strip sub-expressions. */
1031 : 0 : rust_assert (!expr.get_struct_base ().is_invalid ());
1032 : 0 : auto &base_struct_expr = expr.get_struct_base ().get_base_struct ();
1033 : 0 : base_struct_expr.accept_vis (*this);
1034 : 0 : if (base_struct_expr.is_marked_for_strip ())
1035 : 0 : rust_error_at (base_struct_expr.get_locus (),
1036 : : "cannot strip expression in this position - outer "
1037 : : "attributes not allowed");
1038 : : }
1039 : : void
1040 : 4129051 : CfgStrip::visit (AST::CallExpr &expr)
1041 : : {
1042 : : // initial strip test based on outer attrs
1043 : 4129051 : expand_cfg_attrs (expr.get_outer_attrs ());
1044 : 4129051 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1045 : : {
1046 : 0 : expr.mark_for_strip ();
1047 : 0 : return;
1048 : : }
1049 : :
1050 : : /* should not be outer attrs on "function" expression - outer attrs
1051 : : * should be associated with call expr as a whole. only sub-expr
1052 : : * expansion is possible. */
1053 : 4129051 : AST::DefaultASTVisitor::visit (expr);
1054 : :
1055 : 4129051 : auto &function = expr.get_function_expr ();
1056 : 4129051 : if (function.is_marked_for_strip ())
1057 : 0 : rust_error_at (function.get_locus (),
1058 : : "cannot strip expression in this position - outer "
1059 : : "attributes not allowed");
1060 : :
1061 : : /* spec says outer attributes are specifically allowed for elements
1062 : : * of call expressions, so full stripping possible */
1063 : : // FIXME: Arthur: Figure out how to refactor this - This is similar to
1064 : : // expanding items in the crate or stmts in blocks
1065 : 4129051 : maybe_strip_pointer_allow_strip (expr.get_params ());
1066 : : }
1067 : : void
1068 : 3436337 : CfgStrip::visit (AST::MethodCallExpr &expr)
1069 : : {
1070 : : // initial strip test based on outer attrs
1071 : 3436337 : expand_cfg_attrs (expr.get_outer_attrs ());
1072 : 3436337 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1073 : : {
1074 : 0 : expr.mark_for_strip ();
1075 : 0 : return;
1076 : : }
1077 : :
1078 : : /* should not be outer attrs on "receiver" expression - outer attrs
1079 : : * should be associated with call expr as a whole. only sub-expr
1080 : : * expansion is possible. */
1081 : 3436337 : AST::DefaultASTVisitor::visit (expr);
1082 : :
1083 : 3436337 : auto &receiver = expr.get_receiver_expr ();
1084 : 3436337 : if (receiver.is_marked_for_strip ())
1085 : 0 : rust_error_at (receiver.get_locus (),
1086 : : "cannot strip expression in this position - outer "
1087 : : "attributes not allowed");
1088 : :
1089 : 3436337 : auto &method_name = expr.get_method_name ();
1090 : 3436337 : if (method_name.has_generic_args ())
1091 : 2608 : maybe_strip_generic_args (method_name.get_generic_args ());
1092 : :
1093 : : /* spec says outer attributes are specifically allowed for elements
1094 : : * of method call expressions, so full stripping possible */
1095 : 3436337 : maybe_strip_pointer_allow_strip (expr.get_params ());
1096 : : }
1097 : : void
1098 : 640866 : CfgStrip::visit (AST::FieldAccessExpr &expr)
1099 : : {
1100 : : // initial strip test based on outer attrs
1101 : 640866 : expand_cfg_attrs (expr.get_outer_attrs ());
1102 : 640866 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1103 : : {
1104 : 0 : expr.mark_for_strip ();
1105 : 0 : return;
1106 : : }
1107 : :
1108 : : /* should not be outer attrs on "receiver" expression - outer attrs
1109 : : * should be associated with field expr as a whole. only sub-expr
1110 : : * expansion is possible. */
1111 : 640866 : AST::DefaultASTVisitor::visit (expr);
1112 : :
1113 : 640866 : auto &receiver = expr.get_receiver_expr ();
1114 : 640866 : if (receiver.is_marked_for_strip ())
1115 : 0 : rust_error_at (receiver.get_locus (),
1116 : : "cannot strip expression in this position - outer "
1117 : : "attributes not allowed");
1118 : : }
1119 : : void
1120 : 44860 : CfgStrip::visit (AST::ClosureExprInner &expr)
1121 : : {
1122 : : // initial strip test based on outer attrs
1123 : 44860 : expand_cfg_attrs (expr.get_outer_attrs ());
1124 : 44860 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1125 : : {
1126 : 0 : expr.mark_for_strip ();
1127 : 0 : return;
1128 : : }
1129 : :
1130 : : /* strip closure parameters if required - this is specifically
1131 : : * allowed by spec */
1132 : 44860 : maybe_strip_closure_params (expr.get_params ());
1133 : :
1134 : 44860 : AST::DefaultASTVisitor::visit (expr);
1135 : :
1136 : : // can't strip expression itself, but can strip sub-expressions
1137 : 44860 : auto &definition_expr = expr.get_definition_expr ();
1138 : 44860 : if (definition_expr.is_marked_for_strip ())
1139 : 0 : rust_error_at (definition_expr.get_locus (),
1140 : : "cannot strip expression in this position - outer "
1141 : : "attributes not allowed");
1142 : : }
1143 : :
1144 : : void
1145 : 1339379 : CfgStrip::visit (AST::BlockExpr &expr)
1146 : : {
1147 : : // initial strip test based on outer attrs
1148 : 1339379 : expand_cfg_attrs (expr.get_outer_attrs ());
1149 : 1339379 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1150 : : {
1151 : 107 : expr.mark_for_strip ();
1152 : 107 : return;
1153 : : }
1154 : :
1155 : : /* strip test based on inner attrs - spec says there are inner
1156 : : * attributes, not just outer attributes of inner stmts */
1157 : 1339272 : expand_cfg_attrs (expr.get_inner_attrs ());
1158 : 1339272 : if (fails_cfg_with_expand (expr.get_inner_attrs ()))
1159 : : {
1160 : 0 : expr.mark_for_strip ();
1161 : 0 : return;
1162 : : }
1163 : :
1164 : 1339272 : maybe_strip_pointer_allow_strip (expr.get_statements ());
1165 : :
1166 : 1339272 : AST::DefaultASTVisitor::visit (expr);
1167 : :
1168 : : // strip tail expression if exists - can actually fully remove it
1169 : 1339272 : if (expr.has_tail_expr ())
1170 : : {
1171 : 1112907 : auto &tail_expr = expr.get_tail_expr ();
1172 : :
1173 : 1112907 : if (tail_expr.is_marked_for_strip ())
1174 : 83 : expr.strip_tail_expr ();
1175 : : }
1176 : : }
1177 : :
1178 : : void
1179 : 190 : CfgStrip::visit (AST::ClosureExprInnerTyped &expr)
1180 : : {
1181 : : // initial strip test based on outer attrs
1182 : 190 : expand_cfg_attrs (expr.get_outer_attrs ());
1183 : 190 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1184 : : {
1185 : 0 : expr.mark_for_strip ();
1186 : 0 : return;
1187 : : }
1188 : :
1189 : : /* strip closure parameters if required - this is specifically
1190 : : * allowed by spec */
1191 : 190 : maybe_strip_closure_params (expr.get_params ());
1192 : :
1193 : 190 : AST::DefaultASTVisitor::visit (expr);
1194 : :
1195 : : // can't strip return type, but can strip sub-types
1196 : 190 : auto &type = expr.get_return_type ();
1197 : :
1198 : 190 : if (type.is_marked_for_strip ())
1199 : 0 : rust_error_at (type.get_locus (), "cannot strip type in this position");
1200 : :
1201 : : // can't strip expression itself, but can strip sub-expressions
1202 : 190 : auto &definition_block = expr.get_definition_expr ();
1203 : 190 : definition_block.accept_vis (*this);
1204 : 190 : if (definition_block.is_marked_for_strip ())
1205 : 0 : rust_error_at (definition_block.get_locus (),
1206 : : "cannot strip block expression in this position - outer "
1207 : : "attributes not allowed");
1208 : : }
1209 : : void
1210 : 4556 : CfgStrip::visit (AST::ContinueExpr &expr)
1211 : : {
1212 : : // initial strip test based on outer attrs
1213 : 4556 : expand_cfg_attrs (expr.get_outer_attrs ());
1214 : 4556 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1215 : : {
1216 : 0 : expr.mark_for_strip ();
1217 : 0 : return;
1218 : : }
1219 : : }
1220 : : void
1221 : 9150 : CfgStrip::visit (AST::BreakExpr &expr)
1222 : : {
1223 : : // initial strip test based on outer attrs
1224 : 9150 : expand_cfg_attrs (expr.get_outer_attrs ());
1225 : 9150 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1226 : : {
1227 : 0 : expr.mark_for_strip ();
1228 : 0 : return;
1229 : : }
1230 : 9150 : AST::DefaultASTVisitor::visit (expr);
1231 : :
1232 : : /* spec does not say that you can have outer attributes on
1233 : : * expression, so assuming you can't. stripping for sub-expressions
1234 : : * is the only thing that can be done */
1235 : 9150 : if (expr.has_break_expr ())
1236 : : {
1237 : 248 : auto &break_expr = expr.get_break_expr ();
1238 : :
1239 : 248 : if (break_expr.is_marked_for_strip ())
1240 : 0 : rust_error_at (break_expr.get_locus (),
1241 : : "cannot strip expression in this position - outer "
1242 : : "attributes not allowed");
1243 : : }
1244 : : }
1245 : : void
1246 : 39259 : CfgStrip::visit (AST::RangeFromToExpr &expr)
1247 : : {
1248 : : /* outer attributes never allowed before these. while cannot strip
1249 : : * two direct descendant expressions, can strip ones below that */
1250 : 39259 : AST::DefaultASTVisitor::visit (expr);
1251 : :
1252 : : // ensure that they are not marked for strip
1253 : 39259 : if (expr.get_from_expr ().is_marked_for_strip ())
1254 : 0 : rust_error_at (expr.get_from_expr ().get_locus (),
1255 : : "cannot strip expression in this position - outer "
1256 : : "attributes are never allowed "
1257 : : "before range exprs");
1258 : 39259 : if (expr.get_to_expr ().is_marked_for_strip ())
1259 : 0 : rust_error_at (expr.get_to_expr ().get_locus (),
1260 : : "cannot strip expression in this position - outer "
1261 : : "attributes not allowed");
1262 : 39259 : }
1263 : : void
1264 : 15710 : CfgStrip::visit (AST::RangeFromExpr &expr)
1265 : : {
1266 : : /* outer attributes never allowed before these. while cannot strip
1267 : : * direct descendant expression, can strip ones below that */
1268 : :
1269 : 15710 : AST::DefaultASTVisitor::visit (expr);
1270 : : /* should have no possibility for outer attrs as would be parsed
1271 : : * with outer expr */
1272 : 15710 : auto &from_expr = expr.get_from_expr ();
1273 : 15710 : if (from_expr.is_marked_for_strip ())
1274 : 0 : rust_error_at (from_expr.get_locus (),
1275 : : "cannot strip expression in this position - outer "
1276 : : "attributes are never allowed before range exprs");
1277 : 15710 : }
1278 : : void
1279 : 21768 : CfgStrip::visit (AST::RangeToExpr &expr)
1280 : : {
1281 : : /* outer attributes never allowed before these. while cannot strip
1282 : : * direct descendant expression, can strip ones below that */
1283 : :
1284 : 21768 : AST::DefaultASTVisitor::visit (expr);
1285 : : /* should syntactically not have outer attributes, though this may
1286 : : * not have worked in practice */
1287 : 21768 : auto &to_expr = expr.get_to_expr ();
1288 : 21768 : if (to_expr.is_marked_for_strip ())
1289 : 0 : rust_error_at (to_expr.get_locus (),
1290 : : "cannot strip expression in this position - outer "
1291 : : "attributes not allowed");
1292 : 21768 : }
1293 : :
1294 : : void
1295 : 448 : CfgStrip::visit (AST::RangeFromToInclExpr &expr)
1296 : : {
1297 : : /* outer attributes never allowed before these. while cannot strip
1298 : : * two direct descendant expressions, can strip ones below that */
1299 : :
1300 : 448 : AST::DefaultASTVisitor::visit (expr);
1301 : :
1302 : : // ensure that they are not marked for strip
1303 : 448 : if (expr.get_from_expr ().is_marked_for_strip ())
1304 : 0 : rust_error_at (expr.get_from_expr ().get_locus (),
1305 : : "cannot strip expression in this position - outer "
1306 : : "attributes are never allowed "
1307 : : "before range exprs");
1308 : 448 : if (expr.get_to_expr ().is_marked_for_strip ())
1309 : 0 : rust_error_at (expr.get_to_expr ().get_locus (),
1310 : : "cannot strip expression in this position - outer "
1311 : : "attributes not allowed");
1312 : 448 : }
1313 : : void
1314 : 0 : CfgStrip::visit (AST::RangeToInclExpr &expr)
1315 : : {
1316 : : /* outer attributes never allowed before these. while cannot strip
1317 : : * direct descendant expression, can strip ones below that */
1318 : :
1319 : 0 : AST::DefaultASTVisitor::visit (expr);
1320 : : /* should syntactically not have outer attributes, though this may
1321 : : * not have worked in practice */
1322 : 0 : auto &to_expr = expr.get_to_expr ();
1323 : 0 : if (to_expr.is_marked_for_strip ())
1324 : 0 : rust_error_at (to_expr.get_locus (),
1325 : : "cannot strip expression in this position - outer "
1326 : : "attributes not allowed");
1327 : 0 : }
1328 : : void
1329 : 92650 : CfgStrip::visit (AST::ReturnExpr &expr)
1330 : : {
1331 : : // initial strip test based on outer attrs
1332 : 92650 : expand_cfg_attrs (expr.get_outer_attrs ());
1333 : 92650 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1334 : : {
1335 : 0 : expr.mark_for_strip ();
1336 : 0 : return;
1337 : : }
1338 : :
1339 : 92650 : AST::DefaultASTVisitor::visit (expr);
1340 : :
1341 : : /* spec does not say that you can have outer attributes on
1342 : : * expression, so assuming you can't. stripping for sub-expressions
1343 : : * is the only thing that can be done */
1344 : 92650 : if (expr.has_returned_expr ())
1345 : : {
1346 : 90264 : auto &returned_expr = expr.get_returned_expr ();
1347 : 90264 : if (returned_expr.is_marked_for_strip ())
1348 : 0 : rust_error_at (returned_expr.get_locus (),
1349 : : "cannot strip expression in this position - outer "
1350 : : "attributes not allowed");
1351 : : }
1352 : : /* TODO: conceptually, you would maybe be able to remove a returned
1353 : : * expr - e.g. if you had conditional compilation returning void or
1354 : : * returning a type. On the other hand, I think that function
1355 : : * return type cannot be conditionally compiled, so I assumed you
1356 : : * can't do this either. */
1357 : : }
1358 : : void
1359 : 122342 : CfgStrip::visit (AST::UnsafeBlockExpr &expr)
1360 : : {
1361 : : // initial strip test based on outer attrs
1362 : 122342 : expand_cfg_attrs (expr.get_outer_attrs ());
1363 : 122342 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1364 : : {
1365 : 7 : expr.mark_for_strip ();
1366 : 7 : return;
1367 : : }
1368 : :
1369 : 122335 : AST::DefaultASTVisitor::visit (expr);
1370 : :
1371 : : // can't strip block itself, but can strip sub-expressions
1372 : 122335 : auto &block_expr = expr.get_block_expr ();
1373 : 122335 : if (block_expr.is_marked_for_strip ())
1374 : 0 : rust_error_at (block_expr.get_locus (),
1375 : : "cannot strip block expression in this position - outer "
1376 : : "attributes not allowed");
1377 : : }
1378 : : void
1379 : 2859 : CfgStrip::visit (AST::LoopExpr &expr)
1380 : : {
1381 : : // initial strip test based on outer attrs
1382 : 2859 : expand_cfg_attrs (expr.get_outer_attrs ());
1383 : 2859 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1384 : : {
1385 : 0 : expr.mark_for_strip ();
1386 : 0 : return;
1387 : : }
1388 : :
1389 : 2859 : AST::DefaultASTVisitor::visit (expr);
1390 : :
1391 : : // can't strip block itself, but can strip sub-expressions
1392 : 2859 : auto &loop_block = expr.get_loop_block ();
1393 : 2859 : if (loop_block.is_marked_for_strip ())
1394 : 0 : rust_error_at (loop_block.get_locus (),
1395 : : "cannot strip block expression in this position - outer "
1396 : : "attributes not allowed");
1397 : : }
1398 : : void
1399 : 14216 : CfgStrip::visit (AST::WhileLoopExpr &expr)
1400 : : {
1401 : : // initial strip test based on outer attrs
1402 : 14216 : expand_cfg_attrs (expr.get_outer_attrs ());
1403 : 14216 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1404 : : {
1405 : 0 : expr.mark_for_strip ();
1406 : 0 : return;
1407 : : }
1408 : :
1409 : 14216 : AST::DefaultASTVisitor::visit (expr);
1410 : : // can't strip predicate expr itself, but can strip sub-expressions
1411 : 14216 : auto &predicate_expr = expr.get_predicate_expr ();
1412 : 14216 : if (predicate_expr.is_marked_for_strip ())
1413 : 0 : rust_error_at (predicate_expr.get_locus (),
1414 : : "cannot strip expression in this position - outer "
1415 : : "attributes not allowed");
1416 : :
1417 : : // can't strip block itself, but can strip sub-expressions
1418 : 14216 : auto &loop_block = expr.get_loop_block ();
1419 : 14216 : if (loop_block.is_marked_for_strip ())
1420 : 0 : rust_error_at (loop_block.get_locus (),
1421 : : "cannot strip block expression in this position - outer "
1422 : : "attributes not allowed");
1423 : : }
1424 : : void
1425 : 2962 : CfgStrip::visit (AST::WhileLetLoopExpr &expr)
1426 : : {
1427 : : // initial strip test based on outer attrs
1428 : 2962 : expand_cfg_attrs (expr.get_outer_attrs ());
1429 : 2962 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1430 : : {
1431 : 0 : expr.mark_for_strip ();
1432 : 0 : return;
1433 : : }
1434 : :
1435 : 2962 : AST::DefaultASTVisitor::visit (expr);
1436 : :
1437 : 5924 : for (auto &pattern : expr.get_patterns ())
1438 : 2962 : if (pattern->is_marked_for_strip ())
1439 : 0 : rust_error_at (pattern->get_locus (),
1440 : : "cannot strip pattern in this position");
1441 : :
1442 : : // can't strip scrutinee expr itself, but can strip sub-expressions
1443 : 2962 : auto &scrutinee_expr = expr.get_scrutinee_expr ();
1444 : 2962 : if (scrutinee_expr.is_marked_for_strip ())
1445 : 0 : rust_error_at (scrutinee_expr.get_locus (),
1446 : : "cannot strip expression in this position - outer "
1447 : : "attributes not allowed");
1448 : :
1449 : : // can't strip block itself, but can strip sub-expressions
1450 : 2962 : auto &loop_block = expr.get_loop_block ();
1451 : 2962 : if (loop_block.is_marked_for_strip ())
1452 : 0 : rust_error_at (loop_block.get_locus (),
1453 : : "cannot strip block expression in this position - outer "
1454 : : "attributes not allowed");
1455 : : }
1456 : : void
1457 : 15737 : CfgStrip::visit (AST::ForLoopExpr &expr)
1458 : : {
1459 : : // initial strip test based on outer attrs
1460 : 15737 : expand_cfg_attrs (expr.get_outer_attrs ());
1461 : 15737 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1462 : : {
1463 : 0 : expr.mark_for_strip ();
1464 : 0 : return;
1465 : : }
1466 : :
1467 : 15737 : AST::DefaultASTVisitor::visit (expr);
1468 : : // strip sub-patterns of pattern
1469 : 15737 : auto &pattern = expr.get_pattern ();
1470 : 15737 : if (pattern.is_marked_for_strip ())
1471 : 0 : rust_error_at (pattern.get_locus (),
1472 : : "cannot strip pattern in this position");
1473 : :
1474 : : // can't strip scrutinee expr itself, but can strip sub-expressions
1475 : 15737 : auto &iterator_expr = expr.get_iterator_expr ();
1476 : 15737 : if (iterator_expr.is_marked_for_strip ())
1477 : 0 : rust_error_at (iterator_expr.get_locus (),
1478 : : "cannot strip expression in this position - outer "
1479 : : "attributes not allowed");
1480 : :
1481 : : // can't strip block itself, but can strip sub-expressions
1482 : 15737 : auto &loop_block = expr.get_loop_block ();
1483 : 15737 : if (loop_block.is_marked_for_strip ())
1484 : 0 : rust_error_at (loop_block.get_locus (),
1485 : : "cannot strip block expression in this position - outer "
1486 : : "attributes not allowed");
1487 : : }
1488 : : void
1489 : 200658 : CfgStrip::visit (AST::IfExpr &expr)
1490 : : {
1491 : : // rust playground test shows that IfExpr does support outer attrs, at least
1492 : : // when used as statement
1493 : :
1494 : : // initial strip test based on outer attrs
1495 : 200658 : expand_cfg_attrs (expr.get_outer_attrs ());
1496 : 200658 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1497 : : {
1498 : 0 : expr.mark_for_strip ();
1499 : 0 : return;
1500 : : }
1501 : :
1502 : 200658 : AST::DefaultASTVisitor::visit (expr);
1503 : :
1504 : : // can't strip condition expr itself, but can strip sub-expressions
1505 : 200658 : auto &condition_expr = expr.get_condition_expr ();
1506 : 200658 : if (condition_expr.is_marked_for_strip ())
1507 : 0 : rust_error_at (condition_expr.get_locus (),
1508 : : "cannot strip expression in this position - outer "
1509 : : "attributes not allowed");
1510 : :
1511 : : // can't strip if block itself, but can strip sub-expressions
1512 : 200658 : auto &if_block = expr.get_if_block ();
1513 : 200658 : if (if_block.is_marked_for_strip ())
1514 : 0 : rust_error_at (if_block.get_locus (),
1515 : : "cannot strip block expression in this position - outer "
1516 : : "attributes not allowed");
1517 : : }
1518 : :
1519 : : void
1520 : 94610 : CfgStrip::visit (AST::IfExprConseqElse &expr)
1521 : : {
1522 : : // initial strip test based on outer attrs
1523 : 94610 : expand_cfg_attrs (expr.get_outer_attrs ());
1524 : 94610 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1525 : : {
1526 : 0 : expr.mark_for_strip ();
1527 : 0 : return;
1528 : : }
1529 : :
1530 : 94610 : AST::DefaultASTVisitor::visit (expr);
1531 : :
1532 : : // can't strip condition expr itself, but can strip sub-expressions
1533 : 94610 : auto &condition_expr = expr.get_condition_expr ();
1534 : 94610 : if (condition_expr.is_marked_for_strip ())
1535 : 0 : rust_error_at (condition_expr.get_locus (),
1536 : : "cannot strip expression in this position - outer "
1537 : : "attributes not allowed");
1538 : :
1539 : : // can't strip if block itself, but can strip sub-expressions
1540 : 94610 : auto &if_block = expr.get_if_block ();
1541 : 94610 : if (if_block.is_marked_for_strip ())
1542 : 0 : rust_error_at (if_block.get_locus (),
1543 : : "cannot strip block expression in this position - outer "
1544 : : "attributes not allowed");
1545 : :
1546 : : // can't strip else block itself, but can strip sub-expressions
1547 : 94610 : auto &else_block = expr.get_else_block ();
1548 : 94610 : if (else_block.is_marked_for_strip ())
1549 : 0 : rust_error_at (else_block.get_locus (),
1550 : : "cannot strip block expression in this position - outer "
1551 : : "attributes not allowed");
1552 : : }
1553 : :
1554 : : void
1555 : 10664 : CfgStrip::visit (AST::IfLetExpr &expr)
1556 : : {
1557 : : // initial strip test based on outer attrs
1558 : 10664 : expand_cfg_attrs (expr.get_outer_attrs ());
1559 : 10664 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1560 : : {
1561 : 0 : expr.mark_for_strip ();
1562 : 0 : return;
1563 : : }
1564 : :
1565 : 10664 : AST::DefaultASTVisitor::visit (expr);
1566 : :
1567 : 21328 : for (auto &pattern : expr.get_patterns ())
1568 : 10664 : if (pattern->is_marked_for_strip ())
1569 : 0 : rust_error_at (pattern->get_locus (),
1570 : : "cannot strip pattern in this position");
1571 : :
1572 : : // can't strip value expr itself, but can strip sub-expressions
1573 : 10664 : auto &value_expr = expr.get_value_expr ();
1574 : 10664 : if (value_expr.is_marked_for_strip ())
1575 : 0 : rust_error_at (value_expr.get_locus (),
1576 : : "cannot strip expression in this position - outer "
1577 : : "attributes not allowed");
1578 : :
1579 : : // can't strip if block itself, but can strip sub-expressions
1580 : 10664 : auto &if_block = expr.get_if_block ();
1581 : 10664 : if (if_block.is_marked_for_strip ())
1582 : 0 : rust_error_at (if_block.get_locus (),
1583 : : "cannot strip block expression in this position - outer "
1584 : : "attributes not allowed");
1585 : : }
1586 : : void
1587 : 2550 : CfgStrip::visit (AST::IfLetExprConseqElse &expr)
1588 : : {
1589 : : // initial strip test based on outer attrs
1590 : 2550 : expand_cfg_attrs (expr.get_outer_attrs ());
1591 : 2550 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1592 : : {
1593 : 0 : expr.mark_for_strip ();
1594 : 0 : return;
1595 : : }
1596 : :
1597 : 2550 : AST::DefaultASTVisitor::visit (expr);
1598 : :
1599 : 5100 : for (auto &pattern : expr.get_patterns ())
1600 : 2550 : if (pattern->is_marked_for_strip ())
1601 : 0 : rust_error_at (pattern->get_locus (),
1602 : : "cannot strip pattern in this position");
1603 : :
1604 : : // can't strip value expr itself, but can strip sub-expressions
1605 : 2550 : auto &value_expr = expr.get_value_expr ();
1606 : 2550 : if (value_expr.is_marked_for_strip ())
1607 : 0 : rust_error_at (value_expr.get_locus (),
1608 : : "cannot strip expression in this position - outer "
1609 : : "attributes not allowed");
1610 : :
1611 : : // can't strip if block itself, but can strip sub-expressions
1612 : 2550 : auto &if_block = expr.get_if_block ();
1613 : 2550 : if (if_block.is_marked_for_strip ())
1614 : 0 : rust_error_at (if_block.get_locus (),
1615 : : "cannot strip block expression in this position - outer "
1616 : : "attributes not allowed");
1617 : :
1618 : : // can't strip else block itself, but can strip sub-expressions
1619 : 2550 : auto &else_block = expr.get_else_block ();
1620 : 2550 : if (else_block.is_marked_for_strip ())
1621 : 0 : rust_error_at (else_block.get_locus (),
1622 : : "cannot strip block expression in this position - outer "
1623 : : "attributes not allowed");
1624 : : }
1625 : : void
1626 : 285376 : CfgStrip::visit (AST::MatchExpr &expr)
1627 : : {
1628 : : // initial strip test based on outer attrs
1629 : 285376 : expand_cfg_attrs (expr.get_outer_attrs ());
1630 : 285376 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1631 : : {
1632 : 0 : expr.mark_for_strip ();
1633 : 0 : return;
1634 : : }
1635 : :
1636 : : // inner attr strip test
1637 : 285376 : expand_cfg_attrs (expr.get_inner_attrs ());
1638 : 285376 : if (fails_cfg_with_expand (expr.get_inner_attrs ()))
1639 : : {
1640 : 0 : expr.mark_for_strip ();
1641 : 0 : return;
1642 : : }
1643 : :
1644 : 285376 : AST::DefaultASTVisitor::visit (expr);
1645 : :
1646 : : // can't strip scrutinee expr itself, but can strip sub-expressions
1647 : 285376 : auto &scrutinee_expr = expr.get_scrutinee_expr ();
1648 : 285376 : if (scrutinee_expr.is_marked_for_strip ())
1649 : 0 : rust_error_at (scrutinee_expr.get_locus (),
1650 : : "cannot strip expression in this position - outer "
1651 : : "attributes not allowed");
1652 : :
1653 : : // strip match cases
1654 : 285376 : auto &match_cases = expr.get_match_cases ();
1655 : 2988296 : for (auto it = match_cases.begin (); it != match_cases.end ();)
1656 : : {
1657 : 2702920 : auto &match_case = *it;
1658 : :
1659 : : // strip match case based on outer attributes in match arm
1660 : 2702920 : auto &match_arm = match_case.get_arm ();
1661 : 2702920 : expand_cfg_attrs (match_arm.get_outer_attrs ());
1662 : 2702920 : if (fails_cfg_with_expand (match_arm.get_outer_attrs ()))
1663 : : {
1664 : : // strip match case
1665 : 0 : it = match_cases.erase (it);
1666 : 0 : continue;
1667 : : }
1668 : :
1669 : 5405840 : for (auto &pattern : match_arm.get_patterns ())
1670 : 2702920 : if (pattern->is_marked_for_strip ())
1671 : 0 : rust_error_at (pattern->get_locus (),
1672 : : "cannot strip pattern in this position");
1673 : :
1674 : : /* assuming that guard expression cannot be stripped as
1675 : : * strictly speaking you would have to strip the whole guard to
1676 : : * make syntactical sense, which you can't do. as such, only
1677 : : * strip sub-expressions */
1678 : 2702920 : if (match_arm.has_match_arm_guard ())
1679 : : {
1680 : 2507 : auto &guard_expr = match_arm.get_guard_expr ();
1681 : 2507 : if (guard_expr.is_marked_for_strip ())
1682 : 0 : rust_error_at (guard_expr.get_locus (),
1683 : : "cannot strip expression in this position - outer "
1684 : : "attributes not allowed");
1685 : : }
1686 : :
1687 : : // strip sub-expressions from match cases
1688 : 2702920 : auto &case_expr = match_case.get_expr ();
1689 : 2702920 : if (case_expr.is_marked_for_strip ())
1690 : 0 : rust_error_at (case_expr.get_locus (),
1691 : : "cannot strip expression in this position - outer "
1692 : : "attributes not allowed");
1693 : :
1694 : : // increment to next case if haven't continued
1695 : 2702920 : ++it;
1696 : : }
1697 : : }
1698 : :
1699 : : void
1700 : 0 : CfgStrip::visit (AST::AwaitExpr &expr)
1701 : : {
1702 : : // initial strip test based on outer attrs
1703 : 0 : expand_cfg_attrs (expr.get_outer_attrs ());
1704 : 0 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1705 : : {
1706 : 0 : expr.mark_for_strip ();
1707 : 0 : return;
1708 : : }
1709 : :
1710 : : /* can't strip awaited expr itself, but can strip sub-expressions
1711 : : * - this is because you can't have no expr to await */
1712 : 0 : auto &awaited_expr = expr.get_awaited_expr ();
1713 : 0 : awaited_expr->accept_vis (*this);
1714 : 0 : if (awaited_expr->is_marked_for_strip ())
1715 : 0 : rust_error_at (awaited_expr->get_locus (),
1716 : : "cannot strip expression in this position - outer "
1717 : : "attributes not allowed");
1718 : : }
1719 : :
1720 : : void
1721 : 0 : CfgStrip::visit (AST::AsyncBlockExpr &expr)
1722 : : {
1723 : : // initial strip test based on outer attrs
1724 : 0 : expand_cfg_attrs (expr.get_outer_attrs ());
1725 : 0 : if (fails_cfg_with_expand (expr.get_outer_attrs ()))
1726 : : {
1727 : 0 : expr.mark_for_strip ();
1728 : 0 : return;
1729 : : }
1730 : :
1731 : 0 : AST::DefaultASTVisitor::visit (expr);
1732 : :
1733 : : // can't strip block itself, but can strip sub-expressions
1734 : 0 : auto &block_expr = expr.get_block_expr ();
1735 : 0 : if (block_expr->is_marked_for_strip ())
1736 : 0 : rust_error_at (block_expr->get_locus (),
1737 : : "cannot strip block expression in this position - outer "
1738 : : "attributes not allowed");
1739 : : }
1740 : :
1741 : : void
1742 : 320907 : CfgStrip::visit (AST::TypeParam ¶m)
1743 : : {
1744 : : // outer attributes don't actually do anything, so ignore them
1745 : :
1746 : 320907 : AST::DefaultASTVisitor::visit (param);
1747 : :
1748 : 320907 : if (param.has_type () && param.get_type ().is_marked_for_strip ())
1749 : 0 : rust_error_at (param.get_type ().get_locus (),
1750 : : "cannot strip type in this position");
1751 : 320907 : }
1752 : :
1753 : : void
1754 : 60083 : CfgStrip::visit (AST::TypeBoundWhereClauseItem &item)
1755 : : {
1756 : : // for lifetimes shouldn't require
1757 : 60083 : AST::DefaultASTVisitor::visit (item);
1758 : :
1759 : 60083 : auto &type = item.get_type ();
1760 : 60083 : if (type.is_marked_for_strip ())
1761 : 0 : rust_error_at (type.get_locus (), "cannot strip type in this position");
1762 : 60083 : }
1763 : :
1764 : : void
1765 : 11578 : CfgStrip::visit (AST::Module &module)
1766 : : {
1767 : : // strip test based on outer attrs
1768 : 11578 : expand_cfg_attrs (module.get_outer_attrs ());
1769 : 11578 : if (fails_cfg_with_expand (module.get_outer_attrs ()))
1770 : : {
1771 : 67 : module.mark_for_strip ();
1772 : 67 : return;
1773 : : }
1774 : :
1775 : 11511 : if (module.get_kind () == AST::Module::UNLOADED)
1776 : : {
1777 : 282 : module.load_items ();
1778 : : }
1779 : :
1780 : : // strip test based on inner attrs
1781 : 11511 : expand_cfg_attrs (module.get_inner_attrs ());
1782 : 11511 : if (fails_cfg_with_expand (module.get_inner_attrs ()))
1783 : : {
1784 : 0 : module.mark_for_strip ();
1785 : 0 : return;
1786 : : }
1787 : :
1788 : : // strip items if required
1789 : 11511 : maybe_strip_pointer_allow_strip (module.get_items ());
1790 : : }
1791 : :
1792 : : void
1793 : 51 : CfgStrip::visit (AST::ExternCrate &extern_crate)
1794 : : {
1795 : : // strip test based on outer attrs
1796 : 51 : expand_cfg_attrs (extern_crate.get_outer_attrs ());
1797 : 51 : if (fails_cfg_with_expand (extern_crate.get_outer_attrs ()))
1798 : : {
1799 : 0 : extern_crate.mark_for_strip ();
1800 : 0 : return;
1801 : : }
1802 : :
1803 : 51 : if (!extern_crate.references_self ())
1804 : : {
1805 : 51 : Session &session = Session::get_instance ();
1806 : 51 : session.load_extern_crate (extern_crate.get_referenced_crate (),
1807 : : extern_crate.get_locus ());
1808 : : }
1809 : : }
1810 : :
1811 : : void
1812 : 29158 : CfgStrip::visit (AST::UseDeclaration &use_decl)
1813 : : {
1814 : : // strip test based on outer attrs
1815 : 29158 : expand_cfg_attrs (use_decl.get_outer_attrs ());
1816 : 29158 : if (fails_cfg_with_expand (use_decl.get_outer_attrs ()))
1817 : : {
1818 : 46 : use_decl.mark_for_strip ();
1819 : 46 : return;
1820 : : }
1821 : : }
1822 : :
1823 : : void
1824 : 800228 : CfgStrip::visit (AST::Function &function)
1825 : : {
1826 : : // initial test based on outer attrs
1827 : 800228 : expand_cfg_attrs (function.get_outer_attrs ());
1828 : 800228 : if (fails_cfg_with_expand (function.get_outer_attrs ()))
1829 : : {
1830 : 47 : function.mark_for_strip ();
1831 : 47 : return;
1832 : : }
1833 : :
1834 : 800181 : AST::DefaultASTVisitor::visit (function);
1835 : :
1836 : : /* strip function parameters if required - this is specifically
1837 : : * allowed by spec */
1838 : 800181 : maybe_strip_function_params (function.get_function_params ());
1839 : :
1840 : 800181 : if (function.has_return_type ())
1841 : : {
1842 : 687074 : auto &return_type = function.get_return_type ();
1843 : 687074 : if (return_type.is_marked_for_strip ())
1844 : 0 : rust_error_at (return_type.get_locus (),
1845 : : "cannot strip type in this position");
1846 : : }
1847 : :
1848 : : /* body should always exist - if error state, should have returned
1849 : : * before now */
1850 : : // can't strip block itself, but can strip sub-expressions
1851 : 800181 : if (function.has_body ())
1852 : : {
1853 : 745899 : auto &block_expr = function.get_definition ();
1854 : 745899 : if (block_expr.value ()->is_marked_for_strip ())
1855 : 0 : rust_error_at (block_expr.value ()->get_locus (),
1856 : : "cannot strip block expression in this position - outer "
1857 : : "attributes not allowed");
1858 : : }
1859 : : }
1860 : :
1861 : : void
1862 : 178455 : CfgStrip::visit (AST::TypeAlias &type_alias)
1863 : : {
1864 : : // initial test based on outer attrs
1865 : 178455 : expand_cfg_attrs (type_alias.get_outer_attrs ());
1866 : 178455 : if (fails_cfg_with_expand (type_alias.get_outer_attrs ()))
1867 : : {
1868 : 0 : type_alias.mark_for_strip ();
1869 : 0 : return;
1870 : : }
1871 : :
1872 : 178455 : AST::DefaultASTVisitor::visit (type_alias);
1873 : :
1874 : 178455 : auto &type = type_alias.get_type_aliased ();
1875 : 178455 : if (type.is_marked_for_strip ())
1876 : 0 : rust_error_at (type.get_locus (), "cannot strip type in this position");
1877 : : }
1878 : :
1879 : : void
1880 : 112022 : CfgStrip::visit (AST::StructStruct &struct_item)
1881 : : {
1882 : : // initial test based on outer attrs
1883 : 112022 : expand_cfg_attrs (struct_item.get_outer_attrs ());
1884 : 112022 : if (fails_cfg_with_expand (struct_item.get_outer_attrs ()))
1885 : : {
1886 : 7 : struct_item.mark_for_strip ();
1887 : 7 : return;
1888 : : }
1889 : :
1890 : 112015 : AST::DefaultASTVisitor::visit (struct_item);
1891 : :
1892 : : /* strip struct fields if required - this is presumably
1893 : : * allowed by spec */
1894 : 112015 : maybe_strip_struct_fields (struct_item.get_fields ());
1895 : : }
1896 : : void
1897 : 6023 : CfgStrip::visit (AST::TupleStruct &tuple_struct)
1898 : : {
1899 : : // initial test based on outer attrs
1900 : 6023 : expand_cfg_attrs (tuple_struct.get_outer_attrs ());
1901 : 6023 : if (fails_cfg_with_expand (tuple_struct.get_outer_attrs ()))
1902 : : {
1903 : 0 : tuple_struct.mark_for_strip ();
1904 : 0 : return;
1905 : : }
1906 : :
1907 : 6023 : AST::DefaultASTVisitor::visit (tuple_struct);
1908 : :
1909 : : /* strip struct fields if required - this is presumably
1910 : : * allowed by spec */
1911 : 6023 : maybe_strip_tuple_fields (tuple_struct.get_fields ());
1912 : : }
1913 : : void
1914 : 6226 : CfgStrip::visit (AST::EnumItem &item)
1915 : : {
1916 : : // initial test based on outer attrs
1917 : 6226 : expand_cfg_attrs (item.get_outer_attrs ());
1918 : 6226 : if (fails_cfg_with_expand (item.get_outer_attrs ()))
1919 : : {
1920 : 0 : item.mark_for_strip ();
1921 : 0 : return;
1922 : : }
1923 : : }
1924 : :
1925 : : void
1926 : 4132 : CfgStrip::visit (AST::EnumItemTuple &item)
1927 : : {
1928 : : // initial test based on outer attrs
1929 : 4132 : expand_cfg_attrs (item.get_outer_attrs ());
1930 : 4132 : if (fails_cfg_with_expand (item.get_outer_attrs ()))
1931 : : {
1932 : 0 : item.mark_for_strip ();
1933 : 0 : return;
1934 : : }
1935 : :
1936 : : /* strip item fields if required - this is presumably
1937 : : * allowed by spec */
1938 : 4132 : maybe_strip_tuple_fields (item.get_tuple_fields ());
1939 : : }
1940 : :
1941 : : void
1942 : 428 : CfgStrip::visit (AST::EnumItemStruct &item)
1943 : : {
1944 : : // initial test based on outer attrs
1945 : 428 : expand_cfg_attrs (item.get_outer_attrs ());
1946 : 428 : if (fails_cfg_with_expand (item.get_outer_attrs ()))
1947 : : {
1948 : 0 : item.mark_for_strip ();
1949 : 0 : return;
1950 : : }
1951 : :
1952 : : /* strip item fields if required - this is presumably
1953 : : * allowed by spec */
1954 : 428 : maybe_strip_struct_fields (item.get_struct_fields ());
1955 : : }
1956 : :
1957 : : void
1958 : 1588 : CfgStrip::visit (AST::EnumItemDiscriminant &item)
1959 : : {
1960 : : // initial test based on outer attrs
1961 : 1588 : expand_cfg_attrs (item.get_outer_attrs ());
1962 : 1588 : if (fails_cfg_with_expand (item.get_outer_attrs ()))
1963 : : {
1964 : 0 : item.mark_for_strip ();
1965 : 0 : return;
1966 : : }
1967 : :
1968 : 1588 : AST::DefaultASTVisitor::visit (item);
1969 : : /* strip any internal sub-expressions - expression itself isn't
1970 : : * allowed to have external attributes in this position so can't be
1971 : : * stripped. */
1972 : 1588 : auto &expr = item.get_expr ();
1973 : 1588 : if (expr.is_marked_for_strip ())
1974 : 0 : rust_error_at (expr.get_locus (),
1975 : : "cannot strip expression in this position - outer "
1976 : : "attributes not allowed");
1977 : : }
1978 : : void
1979 : 2371 : CfgStrip::visit (AST::Enum &enum_item)
1980 : : {
1981 : : // initial test based on outer attrs
1982 : 2371 : expand_cfg_attrs (enum_item.get_outer_attrs ());
1983 : 2371 : if (fails_cfg_with_expand (enum_item.get_outer_attrs ()))
1984 : : {
1985 : 0 : enum_item.mark_for_strip ();
1986 : 0 : return;
1987 : : }
1988 : :
1989 : 2371 : AST::DefaultASTVisitor::visit (enum_item);
1990 : :
1991 : : /* strip enum fields if required - this is presumably
1992 : : * allowed by spec */
1993 : 2371 : maybe_strip_pointer_allow_strip (enum_item.get_variants ());
1994 : : }
1995 : : void
1996 : 292 : CfgStrip::visit (AST::Union &union_item)
1997 : : {
1998 : : // initial test based on outer attrs
1999 : 292 : expand_cfg_attrs (union_item.get_outer_attrs ());
2000 : 292 : if (fails_cfg_with_expand (union_item.get_outer_attrs ()))
2001 : : {
2002 : 0 : union_item.mark_for_strip ();
2003 : 0 : return;
2004 : : }
2005 : :
2006 : 292 : AST::DefaultASTVisitor::visit (union_item);
2007 : :
2008 : : /* strip union fields if required - this is presumably
2009 : : * allowed by spec */
2010 : 292 : maybe_strip_struct_fields (union_item.get_variants ());
2011 : : }
2012 : : void
2013 : 33579 : CfgStrip::visit (AST::ConstantItem &const_item)
2014 : : {
2015 : : // initial test based on outer attrs
2016 : 33579 : expand_cfg_attrs (const_item.get_outer_attrs ());
2017 : 33579 : if (fails_cfg_with_expand (const_item.get_outer_attrs ()))
2018 : : {
2019 : 1 : const_item.mark_for_strip ();
2020 : 1 : return;
2021 : : }
2022 : :
2023 : 33578 : AST::DefaultASTVisitor::visit (const_item);
2024 : :
2025 : : // strip any sub-types
2026 : 33578 : auto &type = const_item.get_type ();
2027 : 33578 : if (type.is_marked_for_strip ())
2028 : 0 : rust_error_at (type.get_locus (), "cannot strip type in this position");
2029 : :
2030 : : /* strip any internal sub-expressions - expression itself isn't
2031 : : * allowed to have external attributes in this position so can't be
2032 : : * stripped. */
2033 : 33578 : if (const_item.has_expr ())
2034 : : {
2035 : 32106 : auto &expr = const_item.get_expr ();
2036 : 32106 : if (expr.is_marked_for_strip ())
2037 : 0 : rust_error_at (expr.get_locus (),
2038 : : "cannot strip expression in this position - outer "
2039 : : "attributes not allowed");
2040 : : }
2041 : : }
2042 : : void
2043 : 1337 : CfgStrip::visit (AST::StaticItem &static_item)
2044 : : {
2045 : : // initial test based on outer attrs
2046 : 1337 : expand_cfg_attrs (static_item.get_outer_attrs ());
2047 : 1337 : if (fails_cfg_with_expand (static_item.get_outer_attrs ()))
2048 : : {
2049 : 0 : static_item.mark_for_strip ();
2050 : 0 : return;
2051 : : }
2052 : :
2053 : 1337 : AST::DefaultASTVisitor::visit (static_item);
2054 : :
2055 : : // strip any sub-types
2056 : 1337 : auto &type = static_item.get_type ();
2057 : :
2058 : 1337 : if (type.is_marked_for_strip ())
2059 : 0 : rust_error_at (type.get_locus (), "cannot strip type in this position");
2060 : :
2061 : : /* strip any internal sub-expressions - expression itself isn't
2062 : : * allowed to have external attributes in this position so can't be
2063 : : * stripped. */
2064 : 1337 : auto &expr = static_item.get_expr ();
2065 : 1337 : if (expr.is_marked_for_strip ())
2066 : 0 : rust_error_at (expr.get_locus (),
2067 : : "cannot strip expression in this position - outer "
2068 : : "attributes not allowed");
2069 : : }
2070 : :
2071 : : void
2072 : 6236 : CfgStrip::visit (AST::TraitItemType &item)
2073 : : {
2074 : : // initial test based on outer attrs
2075 : 6236 : expand_cfg_attrs (item.get_outer_attrs ());
2076 : 6236 : if (fails_cfg_with_expand (item.get_outer_attrs ()))
2077 : : {
2078 : 0 : item.mark_for_strip ();
2079 : 0 : return;
2080 : : }
2081 : :
2082 : 6236 : AST::DefaultASTVisitor::visit (item);
2083 : : }
2084 : :
2085 : : void
2086 : 13251 : CfgStrip::visit (AST::Trait &trait)
2087 : : {
2088 : : // initial strip test based on outer attrs
2089 : 13251 : expand_cfg_attrs (trait.get_outer_attrs ());
2090 : 13251 : if (fails_cfg_with_expand (trait.get_outer_attrs ()))
2091 : : {
2092 : 1 : trait.mark_for_strip ();
2093 : 1 : return;
2094 : : }
2095 : :
2096 : : // strip test based on inner attrs
2097 : 13250 : expand_cfg_attrs (trait.get_inner_attrs ());
2098 : 13250 : if (fails_cfg_with_expand (trait.get_inner_attrs ()))
2099 : : {
2100 : 0 : trait.mark_for_strip ();
2101 : 0 : return;
2102 : : }
2103 : :
2104 : 13250 : AST::DefaultASTVisitor::visit (trait);
2105 : :
2106 : 13250 : maybe_strip_pointer_allow_strip (trait.get_trait_items ());
2107 : : }
2108 : :
2109 : : void
2110 : 12274 : CfgStrip::visit (AST::InherentImpl &impl)
2111 : : {
2112 : : // initial strip test based on outer attrs
2113 : 12274 : expand_cfg_attrs (impl.get_outer_attrs ());
2114 : 12274 : if (fails_cfg_with_expand (impl.get_outer_attrs ()))
2115 : : {
2116 : 8 : impl.mark_for_strip ();
2117 : 8 : return;
2118 : : }
2119 : :
2120 : : // strip test based on inner attrs
2121 : 12266 : expand_cfg_attrs (impl.get_inner_attrs ());
2122 : 12266 : if (fails_cfg_with_expand (impl.get_inner_attrs ()))
2123 : : {
2124 : 0 : impl.mark_for_strip ();
2125 : 0 : return;
2126 : : }
2127 : :
2128 : 12266 : AST::DefaultASTVisitor::visit (impl);
2129 : :
2130 : 12266 : auto &type = impl.get_type ();
2131 : :
2132 : 12266 : if (type.is_marked_for_strip ())
2133 : 0 : rust_error_at (type.get_locus (), "cannot strip type in this position");
2134 : :
2135 : 12266 : maybe_strip_pointer_allow_strip (impl.get_impl_items ());
2136 : : }
2137 : :
2138 : : void
2139 : 233719 : CfgStrip::visit (AST::TraitImpl &impl)
2140 : : {
2141 : : // initial strip test based on outer attrs
2142 : 233719 : expand_cfg_attrs (impl.get_outer_attrs ());
2143 : 233719 : if (fails_cfg_with_expand (impl.get_outer_attrs ()))
2144 : : {
2145 : 11 : impl.mark_for_strip ();
2146 : 11 : return;
2147 : : }
2148 : :
2149 : : // strip test based on inner attrs
2150 : 233708 : expand_cfg_attrs (impl.get_inner_attrs ());
2151 : 233708 : if (fails_cfg_with_expand (impl.get_inner_attrs ()))
2152 : : {
2153 : 0 : impl.mark_for_strip ();
2154 : 0 : return;
2155 : : }
2156 : :
2157 : 233708 : AST::DefaultASTVisitor::visit (impl);
2158 : :
2159 : 233708 : auto &type = impl.get_type ();
2160 : 233708 : if (type.is_marked_for_strip ())
2161 : 0 : rust_error_at (type.get_locus (), "cannot strip type in this position");
2162 : :
2163 : 233708 : auto &trait_path = impl.get_trait_path ();
2164 : 233708 : visit (trait_path);
2165 : 233708 : if (trait_path.is_marked_for_strip ())
2166 : 0 : rust_error_at (trait_path.get_locus (),
2167 : : "cannot strip typepath in this position");
2168 : :
2169 : 233708 : maybe_strip_pointer_allow_strip (impl.get_impl_items ());
2170 : : }
2171 : :
2172 : : void
2173 : 37 : CfgStrip::visit (AST::ExternalTypeItem &item)
2174 : : {
2175 : 37 : expand_cfg_attrs (item.get_outer_attrs ());
2176 : :
2177 : 37 : if (fails_cfg_with_expand (item.get_outer_attrs ()))
2178 : 0 : item.mark_for_strip ();
2179 : :
2180 : : // TODO: Can we do anything like expand a macro here?
2181 : : // extern "C" { type ffi_ty!(); }
2182 : : // ?
2183 : 37 : }
2184 : :
2185 : : void
2186 : 2 : CfgStrip::visit (AST::ExternalStaticItem &item)
2187 : : {
2188 : : // strip test based on outer attrs
2189 : 2 : expand_cfg_attrs (item.get_outer_attrs ());
2190 : 2 : if (fails_cfg_with_expand (item.get_outer_attrs ()))
2191 : : {
2192 : 0 : item.mark_for_strip ();
2193 : 0 : return;
2194 : : }
2195 : :
2196 : 2 : AST::DefaultASTVisitor::visit (item);
2197 : :
2198 : 2 : auto &type = item.get_type ();
2199 : 2 : if (type.is_marked_for_strip ())
2200 : 0 : rust_error_at (type.get_locus (), "cannot strip type in this position");
2201 : : }
2202 : :
2203 : : void
2204 : 4992 : CfgStrip::visit (AST::ExternBlock &block)
2205 : : {
2206 : : // initial strip test based on outer attrs
2207 : 4992 : expand_cfg_attrs (block.get_outer_attrs ());
2208 : 4992 : if (fails_cfg_with_expand (block.get_outer_attrs ()))
2209 : : {
2210 : 0 : block.mark_for_strip ();
2211 : 0 : return;
2212 : : }
2213 : :
2214 : : // strip test based on inner attrs
2215 : 4992 : expand_cfg_attrs (block.get_inner_attrs ());
2216 : 4992 : if (fails_cfg_with_expand (block.get_inner_attrs ()))
2217 : : {
2218 : 0 : block.mark_for_strip ();
2219 : 0 : return;
2220 : : }
2221 : :
2222 : 4992 : maybe_strip_pointer_allow_strip (block.get_extern_items ());
2223 : : }
2224 : :
2225 : : void
2226 : 47483 : CfgStrip::visit (AST::MacroRulesDefinition &rules_def)
2227 : : {
2228 : : // initial strip test based on outer attrs
2229 : 47483 : expand_cfg_attrs (rules_def.get_outer_attrs ());
2230 : 47483 : if (fails_cfg_with_expand (rules_def.get_outer_attrs ()))
2231 : : {
2232 : 2 : rules_def.mark_for_strip ();
2233 : 2 : return;
2234 : : }
2235 : : }
2236 : :
2237 : : void
2238 : 2423360 : CfgStrip::visit (AST::IdentifierPattern &pattern)
2239 : : {
2240 : : // can only strip sub-patterns of the inner pattern to bind
2241 : 2423360 : if (!pattern.has_subpattern ())
2242 : : return;
2243 : :
2244 : 1248 : AST::DefaultASTVisitor::visit (pattern);
2245 : :
2246 : 1248 : auto &sub_pattern = pattern.get_subpattern ();
2247 : 1248 : if (sub_pattern.is_marked_for_strip ())
2248 : 0 : rust_error_at (sub_pattern.get_locus (),
2249 : : "cannot strip pattern in this position");
2250 : : }
2251 : :
2252 : : void
2253 : 42 : CfgStrip::visit (AST::RangePatternBoundPath &bound)
2254 : : {
2255 : : // can expand path, but not strip it directly
2256 : 42 : auto &path = bound.get_path ();
2257 : 42 : visit (path);
2258 : 42 : if (path.is_marked_for_strip ())
2259 : 0 : rust_error_at (path.get_locus (), "cannot strip path in this position");
2260 : 42 : }
2261 : :
2262 : : void
2263 : 0 : CfgStrip::visit (AST::RangePatternBoundQualPath &bound)
2264 : : {
2265 : : // can expand path, but not strip it directly
2266 : 0 : auto &path = bound.get_qualified_path ();
2267 : 0 : visit (path);
2268 : 0 : if (path.is_marked_for_strip ())
2269 : 0 : rust_error_at (path.get_locus (), "cannot strip path in this position");
2270 : 0 : }
2271 : :
2272 : : void
2273 : 21548 : CfgStrip::visit (AST::ReferencePattern &pattern)
2274 : : {
2275 : 21548 : AST::DefaultASTVisitor::visit (pattern);
2276 : :
2277 : 21548 : auto &sub_pattern = pattern.get_referenced_pattern ();
2278 : 21548 : if (sub_pattern.is_marked_for_strip ())
2279 : 0 : rust_error_at (sub_pattern.get_locus (),
2280 : : "cannot strip pattern in this position");
2281 : 21548 : }
2282 : : void
2283 : 4 : CfgStrip::visit (AST::StructPatternFieldTuplePat &field)
2284 : : {
2285 : : // initial strip test based on outer attrs
2286 : 4 : expand_cfg_attrs (field.get_outer_attrs ());
2287 : 4 : if (fails_cfg_with_expand (field.get_outer_attrs ()))
2288 : : {
2289 : 0 : field.mark_for_strip ();
2290 : 0 : return;
2291 : : }
2292 : :
2293 : 4 : AST::DefaultASTVisitor::visit (field);
2294 : :
2295 : : // strip sub-patterns (can't strip top-level pattern)
2296 : 4 : auto &sub_pattern = field.get_index_pattern ();
2297 : 4 : if (sub_pattern.is_marked_for_strip ())
2298 : 0 : rust_error_at (sub_pattern.get_locus (),
2299 : : "cannot strip pattern in this position");
2300 : : }
2301 : :
2302 : : void
2303 : 3394 : CfgStrip::visit (AST::StructPatternFieldIdentPat &field)
2304 : : {
2305 : : // initial strip test based on outer attrs
2306 : 3394 : expand_cfg_attrs (field.get_outer_attrs ());
2307 : 3394 : if (fails_cfg_with_expand (field.get_outer_attrs ()))
2308 : : {
2309 : 0 : field.mark_for_strip ();
2310 : 0 : return;
2311 : : }
2312 : :
2313 : 3394 : AST::DefaultASTVisitor::visit (field);
2314 : : // strip sub-patterns (can't strip top-level pattern)
2315 : 3394 : auto &sub_pattern = field.get_ident_pattern ();
2316 : 3394 : if (sub_pattern.is_marked_for_strip ())
2317 : 0 : rust_error_at (sub_pattern.get_locus (),
2318 : : "cannot strip pattern in this position");
2319 : : }
2320 : : void
2321 : 1648 : CfgStrip::visit (AST::StructPatternFieldIdent &field)
2322 : : {
2323 : : // initial strip test based on outer attrs
2324 : 1648 : expand_cfg_attrs (field.get_outer_attrs ());
2325 : 1648 : if (fails_cfg_with_expand (field.get_outer_attrs ()))
2326 : : {
2327 : 0 : field.mark_for_strip ();
2328 : 0 : return;
2329 : : }
2330 : : }
2331 : :
2332 : : void
2333 : 3618 : CfgStrip::visit (AST::StructPattern &pattern)
2334 : : {
2335 : : // expand (but don't strip) path
2336 : 3618 : auto &path = pattern.get_path ();
2337 : 3618 : visit (path);
2338 : 3618 : if (path.is_marked_for_strip ())
2339 : 0 : rust_error_at (path.get_locus (), "cannot strip path in this position");
2340 : :
2341 : : /* TODO: apparently struct pattern fields can have outer attrs. so can they
2342 : : * be stripped? */
2343 : 3618 : if (!pattern.has_struct_pattern_elems ())
2344 : : return;
2345 : :
2346 : 3616 : auto &elems = pattern.get_struct_pattern_elems ();
2347 : :
2348 : : // assuming you can strip struct pattern fields
2349 : 3616 : maybe_strip_pointer_allow_strip (elems.get_struct_pattern_fields ());
2350 : :
2351 : : // assuming you can strip the ".." part
2352 : 3616 : if (elems.has_rest ())
2353 : : {
2354 : 74 : expand_cfg_attrs (elems.get_etc_outer_attrs ());
2355 : 74 : if (fails_cfg_with_expand (elems.get_etc_outer_attrs ()))
2356 : 0 : elems.strip_etc ();
2357 : : }
2358 : : }
2359 : :
2360 : : void
2361 : 80866 : CfgStrip::visit (AST::TupleStructItemsNoRest &tuple_items)
2362 : : {
2363 : 80866 : AST::DefaultASTVisitor::visit (tuple_items);
2364 : : // can't strip individual patterns, only sub-patterns
2365 : 164138 : for (auto &pattern : tuple_items.get_patterns ())
2366 : : {
2367 : 83272 : if (pattern->is_marked_for_strip ())
2368 : 0 : rust_error_at (pattern->get_locus (),
2369 : : "cannot strip pattern in this position");
2370 : : // TODO: quit stripping now? or keep going?
2371 : : }
2372 : 80866 : }
2373 : : void
2374 : 778 : CfgStrip::visit (AST::TupleStructItemsHasRest &tuple_items)
2375 : : {
2376 : 778 : AST::DefaultASTVisitor::visit (tuple_items);
2377 : : // can't strip individual patterns, only sub-patterns
2378 : 896 : for (auto &lower_pattern : tuple_items.get_lower_patterns ())
2379 : : {
2380 : 118 : if (lower_pattern->is_marked_for_strip ())
2381 : 0 : rust_error_at (lower_pattern->get_locus (),
2382 : : "cannot strip pattern in this position");
2383 : : // TODO: quit stripping now? or keep going?
2384 : : }
2385 : 844 : for (auto &upper_pattern : tuple_items.get_upper_patterns ())
2386 : : {
2387 : 66 : if (upper_pattern->is_marked_for_strip ())
2388 : 0 : rust_error_at (upper_pattern->get_locus (),
2389 : : "cannot strip pattern in this position");
2390 : : // TODO: quit stripping now? or keep going?
2391 : : }
2392 : 778 : }
2393 : :
2394 : : void
2395 : 81644 : CfgStrip::visit (AST::TupleStructPattern &pattern)
2396 : : {
2397 : : // expand (but don't strip) path
2398 : 81644 : auto &path = pattern.get_path ();
2399 : 81644 : visit (path);
2400 : 81644 : if (path.is_marked_for_strip ())
2401 : 0 : rust_error_at (path.get_locus (), "cannot strip path in this position");
2402 : :
2403 : 81644 : AST::DefaultASTVisitor::visit (pattern);
2404 : 81644 : }
2405 : :
2406 : : void
2407 : 215651 : CfgStrip::visit (AST::TuplePatternItemsNoRest &tuple_items)
2408 : : {
2409 : 215651 : AST::DefaultASTVisitor::visit (tuple_items);
2410 : :
2411 : : // can't strip individual patterns, only sub-patterns
2412 : 700095 : for (auto &pattern : tuple_items.get_patterns ())
2413 : : {
2414 : 484444 : if (pattern->is_marked_for_strip ())
2415 : 0 : rust_error_at (pattern->get_locus (),
2416 : : "cannot strip pattern in this position");
2417 : : // TODO: quit stripping now? or keep going?
2418 : : }
2419 : 215651 : }
2420 : :
2421 : : void
2422 : 102 : CfgStrip::visit (AST::TuplePatternItemsHasRest &tuple_items)
2423 : : {
2424 : 102 : AST::DefaultASTVisitor::visit (tuple_items);
2425 : :
2426 : : // can't strip individual patterns, only sub-patterns
2427 : 206 : for (auto &lower_pattern : tuple_items.get_lower_patterns ())
2428 : : {
2429 : 104 : if (lower_pattern->is_marked_for_strip ())
2430 : 0 : rust_error_at (lower_pattern->get_locus (),
2431 : : "cannot strip pattern in this position");
2432 : : // TODO: quit stripping now? or keep going?
2433 : : }
2434 : 214 : for (auto &upper_pattern : tuple_items.get_upper_patterns ())
2435 : : {
2436 : 112 : if (upper_pattern->is_marked_for_strip ())
2437 : 0 : rust_error_at (upper_pattern->get_locus (),
2438 : : "cannot strip pattern in this position");
2439 : : // TODO: quit stripping now? or keep going?
2440 : : }
2441 : 102 : }
2442 : :
2443 : : void
2444 : 176 : CfgStrip::visit (AST::GroupedPattern &pattern)
2445 : : {
2446 : 176 : AST::DefaultASTVisitor::visit (pattern);
2447 : : // can't strip inner pattern, only sub-patterns
2448 : 176 : auto &pattern_in_parens = pattern.get_pattern_in_parens ();
2449 : :
2450 : 176 : if (pattern_in_parens.is_marked_for_strip ())
2451 : 0 : rust_error_at (pattern_in_parens.get_locus (),
2452 : : "cannot strip pattern in this position");
2453 : 176 : }
2454 : :
2455 : : void
2456 : 680 : CfgStrip::visit (AST::SlicePatternItemsNoRest &items)
2457 : : {
2458 : 680 : AST::DefaultASTVisitor::visit (items);
2459 : : // can't strip individual patterns, only sub-patterns
2460 : 1550 : for (auto &pattern : items.get_patterns ())
2461 : : {
2462 : 870 : if (pattern->is_marked_for_strip ())
2463 : 0 : rust_error_at (pattern->get_locus (),
2464 : : "cannot strip pattern in this position");
2465 : : }
2466 : 680 : }
2467 : :
2468 : : void
2469 : 732 : CfgStrip::visit (AST::SlicePatternItemsHasRest &items)
2470 : : {
2471 : 732 : AST::DefaultASTVisitor::visit (items);
2472 : : // can't strip individual patterns, only sub-patterns
2473 : 1742 : for (auto &pattern : items.get_lower_patterns ())
2474 : : {
2475 : 1010 : if (pattern->is_marked_for_strip ())
2476 : 0 : rust_error_at (pattern->get_locus (),
2477 : : "cannot strip pattern in this position");
2478 : : }
2479 : 1042 : for (auto &pattern : items.get_upper_patterns ())
2480 : : {
2481 : 310 : if (pattern->is_marked_for_strip ())
2482 : 0 : rust_error_at (pattern->get_locus (),
2483 : : "cannot strip pattern in this position");
2484 : : }
2485 : 732 : }
2486 : :
2487 : : void
2488 : 1412 : CfgStrip::visit (AST::SlicePattern &pattern)
2489 : : {
2490 : 1412 : AST::DefaultASTVisitor::visit (pattern);
2491 : 1412 : }
2492 : :
2493 : : void
2494 : 3948 : CfgStrip::visit (AST::AltPattern &pattern)
2495 : : {
2496 : 3948 : AST::DefaultASTVisitor::visit (pattern);
2497 : : // can't strip individual patterns, only sub-patterns
2498 : 14291 : for (auto &alt : pattern.get_alts ())
2499 : : {
2500 : 10343 : if (alt->is_marked_for_strip ())
2501 : 0 : rust_error_at (alt->get_locus (),
2502 : : "cannot strip pattern in this position");
2503 : : // TODO: quit stripping now? or keep going?
2504 : : }
2505 : 3948 : }
2506 : :
2507 : : void
2508 : 504518 : CfgStrip::visit (AST::LetStmt &stmt)
2509 : : {
2510 : : // initial strip test based on outer attrs
2511 : 504518 : expand_cfg_attrs (stmt.get_outer_attrs ());
2512 : 504518 : if (fails_cfg_with_expand (stmt.get_outer_attrs ()))
2513 : : {
2514 : 0 : stmt.mark_for_strip ();
2515 : 0 : return;
2516 : : }
2517 : :
2518 : 504518 : AST::DefaultASTVisitor::visit (stmt);
2519 : : // can't strip pattern, but call for sub-patterns
2520 : 504518 : auto &pattern = stmt.get_pattern ();
2521 : 504518 : if (pattern.is_marked_for_strip ())
2522 : 0 : rust_error_at (pattern.get_locus (),
2523 : : "cannot strip pattern in this position");
2524 : :
2525 : : // similar for type
2526 : 504518 : if (stmt.has_type ())
2527 : : {
2528 : 54746 : auto &type = stmt.get_type ();
2529 : :
2530 : 54746 : if (type.is_marked_for_strip ())
2531 : 0 : rust_error_at (type.get_locus (), "cannot strip type in this position");
2532 : : }
2533 : :
2534 : : /* strip any internal sub-expressions - expression itself isn't
2535 : : * allowed to have external attributes in this position so can't be
2536 : : * stripped */
2537 : 504518 : if (stmt.has_init_expr ())
2538 : : {
2539 : 486714 : auto &init_expr = stmt.get_init_expr ();
2540 : :
2541 : 486714 : if (init_expr.is_marked_for_strip ())
2542 : 0 : rust_error_at (init_expr.get_locus (),
2543 : : "cannot strip expression in this position - outer "
2544 : : "attributes not allowed");
2545 : : }
2546 : : }
2547 : :
2548 : : void
2549 : 840441 : CfgStrip::visit (AST::ExprStmt &stmt)
2550 : : {
2551 : : // outer attributes associated with expr, so rely on expr
2552 : :
2553 : : // guard - should prevent null pointer expr
2554 : 840441 : if (stmt.is_marked_for_strip ())
2555 : : return;
2556 : :
2557 : 840441 : AST::DefaultASTVisitor::visit (stmt);
2558 : : // strip if expr is to be stripped
2559 : 840441 : auto &expr = stmt.get_expr ();
2560 : 840441 : if (expr.is_marked_for_strip ())
2561 : : {
2562 : 45 : stmt.mark_for_strip ();
2563 : 45 : return;
2564 : : }
2565 : : }
2566 : :
2567 : : void
2568 : 200868 : CfgStrip::visit (AST::TraitBound &bound)
2569 : : {
2570 : : // nothing in for lifetimes to strip
2571 : :
2572 : : // expand but don't strip type path
2573 : 200868 : auto &path = bound.get_type_path ();
2574 : 200868 : visit (path);
2575 : 200868 : if (path.is_marked_for_strip ())
2576 : 0 : rust_error_at (path.get_locus (),
2577 : : "cannot strip type path in this position");
2578 : 200868 : }
2579 : :
2580 : : void
2581 : 714 : CfgStrip::visit (AST::ParenthesisedType &type)
2582 : : {
2583 : 714 : AST::DefaultASTVisitor::visit (type);
2584 : : // expand but don't strip inner type
2585 : 714 : auto &inner_type = type.get_type_in_parens ();
2586 : 714 : if (inner_type->is_marked_for_strip ())
2587 : 0 : rust_error_at (inner_type->get_locus (),
2588 : : "cannot strip type in this position");
2589 : 714 : }
2590 : :
2591 : : void
2592 : 81352 : CfgStrip::visit (AST::TupleType &type)
2593 : : {
2594 : 81352 : AST::DefaultASTVisitor::visit (type);
2595 : : // TODO: assuming that types can't be stripped as types don't have outer
2596 : : // attributes
2597 : 256508 : for (auto &elem_type : type.get_elems ())
2598 : : {
2599 : 175156 : if (elem_type->is_marked_for_strip ())
2600 : 0 : rust_error_at (elem_type->get_locus (),
2601 : : "cannot strip type in this position");
2602 : : }
2603 : 81352 : }
2604 : :
2605 : : void
2606 : 170506 : CfgStrip::visit (AST::RawPointerType &type)
2607 : : {
2608 : 170506 : AST::DefaultASTVisitor::visit (type);
2609 : : // expand but don't strip type pointed to
2610 : 170506 : auto &pointed_type = type.get_type_pointed_to ();
2611 : 170506 : if (pointed_type.is_marked_for_strip ())
2612 : 0 : rust_error_at (pointed_type.get_locus (),
2613 : : "cannot strip type in this position");
2614 : 170506 : }
2615 : :
2616 : : void
2617 : 742259 : CfgStrip::visit (AST::ReferenceType &type)
2618 : : {
2619 : 742259 : AST::DefaultASTVisitor::visit (type);
2620 : : // expand but don't strip type referenced
2621 : 742259 : auto &referenced_type = type.get_type_referenced ();
2622 : 742259 : if (referenced_type.is_marked_for_strip ())
2623 : 0 : rust_error_at (referenced_type.get_locus (),
2624 : : "cannot strip type in this position");
2625 : 742259 : }
2626 : :
2627 : : void
2628 : 27053 : CfgStrip::visit (AST::ArrayType &type)
2629 : : {
2630 : 27053 : AST::DefaultASTVisitor::visit (type);
2631 : : // expand but don't strip type referenced
2632 : 27053 : auto &base_type = type.get_elem_type ();
2633 : 27053 : if (base_type.is_marked_for_strip ())
2634 : 0 : rust_error_at (base_type.get_locus (),
2635 : : "cannot strip type in this position");
2636 : :
2637 : : // same for expression
2638 : 27053 : auto &size_expr = type.get_size_expr ();
2639 : 27053 : if (size_expr.is_marked_for_strip ())
2640 : 0 : rust_error_at (size_expr.get_locus (),
2641 : : "cannot strip expression in this position");
2642 : 27053 : }
2643 : : void
2644 : 54749 : CfgStrip::visit (AST::SliceType &type)
2645 : : {
2646 : 54749 : AST::DefaultASTVisitor::visit (type);
2647 : : // expand but don't strip elem type
2648 : 54749 : auto &elem_type = type.get_elem_type ();
2649 : 54749 : if (elem_type.is_marked_for_strip ())
2650 : 0 : rust_error_at (elem_type.get_locus (),
2651 : : "cannot strip type in this position");
2652 : 54749 : }
2653 : :
2654 : : void
2655 : 19140 : CfgStrip::visit (AST::BareFunctionType &type)
2656 : : {
2657 : : // seem to be no generics
2658 : 19140 : AST::DefaultASTVisitor::visit (type);
2659 : :
2660 : : // presumably function params can be stripped
2661 : 19140 : auto ¶ms = type.get_function_params ();
2662 : 129078 : for (auto it = params.begin (); it != params.end ();)
2663 : : {
2664 : 109938 : auto ¶m = *it;
2665 : :
2666 : 109938 : auto ¶m_attrs = param.get_outer_attrs ();
2667 : 109938 : expand_cfg_attrs (param_attrs);
2668 : 109938 : if (fails_cfg_with_expand (param_attrs))
2669 : : {
2670 : 0 : it = params.erase (it);
2671 : 0 : continue;
2672 : : }
2673 : :
2674 : 109938 : auto &type = param.get_type ();
2675 : 109938 : if (type.is_marked_for_strip ())
2676 : 0 : rust_error_at (type.get_locus (), "cannot strip type in this position");
2677 : :
2678 : : // increment if nothing else happens
2679 : 109938 : ++it;
2680 : : }
2681 : :
2682 : : /* TODO: assuming that variadic nature cannot be stripped. If this
2683 : : * is not true, then have code here to do so. */
2684 : :
2685 : 19140 : if (type.has_return_type ())
2686 : : {
2687 : : // FIXME: Can we have type expansion in this position?
2688 : : // In that case, we need to handle AST::TypeNoBounds on top of just
2689 : : // AST::Types
2690 : 18298 : auto &return_type = type.get_return_type ();
2691 : 18298 : if (return_type.is_marked_for_strip ())
2692 : 0 : rust_error_at (return_type.get_locus (),
2693 : : "cannot strip type in this position");
2694 : : }
2695 : :
2696 : : // no where clause, apparently
2697 : 19140 : }
2698 : :
2699 : : void
2700 : 585100 : CfgStrip::visit (AST::SelfParam ¶m)
2701 : : {
2702 : 585100 : AST::DefaultASTVisitor::visit (param);
2703 : :
2704 : 585100 : if (param.has_type ())
2705 : : {
2706 : 984 : auto &type = param.get_type ();
2707 : 984 : if (type.is_marked_for_strip ())
2708 : 0 : rust_error_at (type.get_locus (), "cannot strip type in this position");
2709 : : }
2710 : : /* TODO: maybe check for invariants being violated - e.g. both type and
2711 : : * lifetime? */
2712 : 585100 : }
2713 : :
2714 : : } // namespace Rust
|