@@ -59,35 +59,108 @@ namespace AST {
59
59
// └─┘ └─┘
60
60
// positions (could be names, numbers, empty, or `*`)
61
61
62
+ // FIXME: Merge with the class below this one?
62
63
class FormatArgumentKind
63
64
{
64
65
public:
66
+ enum class Kind
67
+ {
68
+ Normal,
69
+ Named,
70
+ Captured,
71
+ } kind;
72
+
65
73
Identifier &get_ident ()
66
74
{
67
75
rust_assert (kind == Kind::Captured || kind == Kind::Named);
68
76
69
77
return ident.value ();
70
78
}
71
79
72
- private:
73
- enum class Kind
80
+ FormatArgumentKind (Kind kind, tl::optional<Identifier> ident)
81
+ : kind (kind), ident (ident)
82
+ {}
83
+
84
+ FormatArgumentKind (const FormatArgumentKind &other)
74
85
{
75
- Normal,
76
- Named,
77
- Captured,
78
- } kind;
86
+ kind = other.kind ;
87
+ ident = other.ident ;
88
+ }
89
+
90
+ FormatArgumentKind operator = (const FormatArgumentKind &other)
91
+ {
92
+ kind = other.kind ;
93
+ ident = other.ident ;
79
94
95
+ return *this ;
96
+ }
97
+
98
+ private:
80
99
tl::optional<Identifier> ident;
81
100
};
82
101
83
102
class FormatArgument
84
103
{
104
+ public:
105
+ static FormatArgument normal (std::unique_ptr<Expr> expr)
106
+ {
107
+ return FormatArgument (FormatArgumentKind::Kind::Normal, tl::nullopt,
108
+ std::move (expr));
109
+ }
110
+
111
+ static FormatArgument named (Identifier ident, std::unique_ptr<Expr> expr)
112
+ {
113
+ return FormatArgument (FormatArgumentKind::Kind::Named, ident,
114
+ std::move (expr));
115
+ }
116
+
117
+ static FormatArgument captured (Identifier ident, std::unique_ptr<Expr> expr)
118
+ {
119
+ return FormatArgument (FormatArgumentKind::Kind::Captured, ident,
120
+ std::move (expr));
121
+ }
122
+
123
+ FormatArgument (const FormatArgument &other)
124
+ : kind (other.kind), expr (other.expr->clone_expr ())
125
+ {}
126
+
127
+ FormatArgument operator = (const FormatArgument &other)
128
+ {
129
+ kind = other.kind ;
130
+ expr = other.expr ->clone_expr ();
131
+
132
+ return *this ;
133
+ }
134
+
135
+ private:
136
+ FormatArgument (FormatArgumentKind::Kind kind, tl::optional<Identifier> ident,
137
+ std::unique_ptr<Expr> expr)
138
+ : kind (FormatArgumentKind (kind, ident)), expr (std::move (expr))
139
+ {}
140
+
85
141
FormatArgumentKind kind;
86
142
std::unique_ptr<Expr> expr;
87
143
};
88
144
89
145
class FormatArguments
90
146
{
147
+ public:
148
+ FormatArguments () {}
149
+ FormatArguments (FormatArguments &&) = default ;
150
+ FormatArguments (const FormatArguments &other)
151
+ {
152
+ args = std::vector<FormatArgument> ();
153
+ args.reserve (other.args .size ());
154
+
155
+ for (const auto &arg : other.args )
156
+ args.emplace_back (arg);
157
+ };
158
+
159
+ FormatArguments &operator = (const FormatArguments &other) = default ;
160
+
161
+ void push (FormatArgument &&elt) { args.emplace_back (std::move (elt)); }
162
+
163
+ private:
91
164
std::vector<FormatArgument> args;
92
165
};
93
166
@@ -100,7 +173,7 @@ class FormatArguments
100
173
// format_args!("result: {}", some_result))` -> `format_args!("heyo result: {}",
101
174
// some_result)`
102
175
// FIXME: Move to rust-macro.h
103
- class FormatArgs : public Visitable
176
+ class FormatArgs : public Expr
104
177
{
105
178
public:
106
179
enum class Newline
@@ -109,18 +182,56 @@ class FormatArgs : public Visitable
109
182
No
110
183
};
111
184
112
- FormatArgs (location_t loc, Fmt::PieceSlice template_str,
113
- FormatArguments arguments)
185
+ FormatArgs (location_t loc, Fmt::Pieces && template_str,
186
+ FormatArguments && arguments)
114
187
: loc (loc), template_str (std::move (template_str)),
115
188
arguments (std::move (arguments))
116
189
{}
117
190
118
- void accept_vis (AST::ASTVisitor &vis);
191
+ FormatArgs (FormatArgs &&other)
192
+ : loc (std::move (other.loc)),
193
+ template_str (std::move (other.template_str)),
194
+ arguments (std::move (other.arguments))
195
+ {
196
+ std::cerr << " [ARTHUR] moving FormatArgs" << std::endl;
197
+ }
198
+
199
+ // FIXME: This might be invalid - we are reusing the same memory allocated
200
+ // on the Rust side for `other`. This is probably valid as long as we only
201
+ // ever read that memory and never write to it.
202
+ FormatArgs (const FormatArgs &other)
203
+ : loc (other.loc), template_str (other.template_str),
204
+ arguments (other.arguments)
205
+ {
206
+ std::cerr << " [ARTHUR] copying FormatArgs" << std::endl;
207
+ }
208
+
209
+ // FormatArgs &operator= (const FormatArgs &other) = default;
210
+ // : template_str (other.template_str), arguments (other.arguments)
211
+ // {}
212
+
213
+ void accept_vis (AST::ASTVisitor &vis) override ;
119
214
120
215
private:
121
216
location_t loc;
122
- Fmt::PieceSlice template_str;
217
+ // FIXME: This probably needs to be a separate type - it is one in rustc's
218
+ // expansion of format_args!(). There is extra handling associated with it.
219
+ // we can maybe do that in rust-fmt.cc? in collect_pieces()? like do the
220
+ // transformation into something we can handle better
221
+ Fmt::Pieces template_str;
123
222
FormatArguments arguments;
223
+
224
+ bool marked_for_strip = false ;
225
+
226
+ protected:
227
+ virtual std::string as_string () const override ;
228
+ virtual location_t get_locus () const override ;
229
+ virtual bool is_expr_without_block () const override ;
230
+ virtual void mark_for_strip () override ;
231
+ virtual bool is_marked_for_strip () const override ;
232
+ virtual std::vector<Attribute> &get_outer_attrs () override ;
233
+ virtual void set_outer_attrs (std::vector<Attribute>) override ;
234
+ virtual Expr *clone_expr_impl () const override ;
124
235
};
125
236
126
237
} // namespace AST
0 commit comments