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