Branch data Line data Source code
1 : : // Copyright (C) 2020-2024 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-compile-struct-field-expr.h"
20 : : #include "rust-compile-expr.h"
21 : :
22 : : namespace Rust {
23 : : namespace Compile {
24 : :
25 : 2073 : CompileStructExprField::CompileStructExprField (Context *ctx)
26 : 2073 : : HIRCompileBase (ctx), translated (error_mark_node)
27 : 2073 : {}
28 : :
29 : : tree
30 : 2073 : CompileStructExprField::Compile (HIR::StructExprField *field, Context *ctx)
31 : : {
32 : 2073 : CompileStructExprField compiler (ctx);
33 : 2073 : switch (field->get_kind ())
34 : : {
35 : 197 : case HIR::StructExprField::StructExprFieldKind::IDENTIFIER:
36 : 197 : compiler.visit (static_cast<HIR::StructExprFieldIdentifier &> (*field));
37 : 197 : break;
38 : :
39 : 1834 : case HIR::StructExprField::StructExprFieldKind::IDENTIFIER_VALUE:
40 : 1834 : compiler.visit (
41 : : static_cast<HIR::StructExprFieldIdentifierValue &> (*field));
42 : 1834 : break;
43 : :
44 : 42 : case HIR::StructExprField::StructExprFieldKind::INDEX_VALUE:
45 : 42 : compiler.visit (static_cast<HIR::StructExprFieldIndexValue &> (*field));
46 : 42 : break;
47 : : }
48 : 2073 : return compiler.translated;
49 : 2073 : }
50 : :
51 : : void
52 : 1834 : CompileStructExprField::visit (HIR::StructExprFieldIdentifierValue &field)
53 : : {
54 : 1834 : translated = CompileExpr::Compile (field.get_value ().get (), ctx);
55 : 1834 : }
56 : :
57 : : void
58 : 42 : CompileStructExprField::visit (HIR::StructExprFieldIndexValue &field)
59 : : {
60 : 42 : translated = CompileExpr::Compile (field.get_value ().get (), ctx);
61 : 42 : }
62 : :
63 : : void
64 : 197 : CompileStructExprField::visit (HIR::StructExprFieldIdentifier &field)
65 : : {
66 : : // we can make the field look like a path expr to take advantage of existing
67 : : // code
68 : :
69 : 197 : Analysis::NodeMapping mappings_copy1 = field.get_mappings ();
70 : 197 : Analysis::NodeMapping mappings_copy2 = field.get_mappings ();
71 : :
72 : 197 : HIR::PathIdentSegment ident_seg (field.get_field_name ().as_string ());
73 : 197 : HIR::PathExprSegment seg (mappings_copy1, ident_seg, field.get_locus (),
74 : 394 : HIR::GenericArgs::create_empty ());
75 : 197 : HIR::PathInExpression expr (mappings_copy2, {seg}, field.get_locus (), false,
76 : 394 : {});
77 : 197 : translated = CompileExpr::Compile (&expr, ctx);
78 : 394 : }
79 : :
80 : : } // namespace Compile
81 : : } // namespace Rust
|