| 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-ast-builder.h" |
| 20 |
#include "optional.h" |
| 21 |
#include "rust-ast-builder-type.h" |
| 22 |
#include "rust-ast.h" |
| 23 |
#include "rust-common.h" |
| 24 |
#include "rust-expr.h" |
| 25 |
#include "rust-keyword-values.h" |
| 26 |
#include "rust-path.h" |
| 27 |
#include "rust-item.h" |
| 28 |
#include "rust-path.h" |
| 29 |
#include "rust-pattern.h" |
| 30 |
#include "rust-system.h" |
| 31 |
#include "rust-token.h" |
| 32 |
#include <memory> |
| 33 |
|
| 34 |
namespace Rust { |
| 35 |
namespace AST { |
| 36 |
|
| 37 |
std::unique_ptr<Stmt> |
| 38 |
Builder::statementify (std::unique_ptr<Expr> &&value, |
| 39 |
bool semicolon_followed) const |
| 40 |
{ |
| 41 |
return std::make_unique<ExprStmt> (std::move (value), loc, |
| 42 |
semicolon_followed); |
| 43 |
} |
| 44 |
|
| 45 |
std::unique_ptr<Expr> |
| 46 |
Builder::literal_string (std::string &&content) const |
| 47 |
{ |
| 48 |
return std::unique_ptr<Expr> ( |
| 49 |
new AST::LiteralExpr (std::move (content), Literal::LitType::STRING, |
| 50 |
PrimitiveCoreType::CORETYPE_STR, {}, loc)); |
| 51 |
} |
| 52 |
|
| 53 |
std::unique_ptr<Expr> |
| 54 |
Builder::literal_bool (bool b) const |
| 55 |
{ |
| 56 |
auto str |
| 57 |
= b ? Values::Keywords::TRUE_LITERAL : Values::Keywords::FALSE_LITERAL; |
| 58 |
|
| 59 |
return std::unique_ptr<Expr> ( |
| 60 |
new AST::LiteralExpr (std::move (str), Literal::LitType::BOOL, |
| 61 |
PrimitiveCoreType::CORETYPE_BOOL, {}, loc)); |
| 62 |
} |
| 63 |
|
| 64 |
std::unique_ptr<Expr> |
| 65 |
Builder::call (std::unique_ptr<Expr> &&path, |
| 66 |
std::vector<std::unique_ptr<Expr>> &&args) const |
| 67 |
{ |
| 68 |
return std::unique_ptr<Expr> ( |
| 69 |
new CallExpr (std::move (path), std::move (args), {}, loc)); |
| 70 |
} |
| 71 |
|
| 72 |
std::unique_ptr<Expr> |
| 73 |
Builder::call (std::unique_ptr<Expr> &&path, std::unique_ptr<Expr> &&arg) const |
| 74 |
{ |
| 75 |
auto args = std::vector<std::unique_ptr<Expr>> (); |
| 76 |
args.emplace_back (std::move (arg)); |
| 77 |
|
| 78 |
return call (std::move (path), std::move (args)); |
| 79 |
} |
| 80 |
|
| 81 |
std::unique_ptr<Expr> |
| 82 |
Builder::array (std::vector<std::unique_ptr<Expr>> &&members) const |
| 83 |
{ |
| 84 |
auto elts = std::make_unique<ArrayElemsValues> (std::move (members), loc); |
| 85 |
|
| 86 |
return std::unique_ptr<Expr> (new ArrayExpr (std::move (elts), {}, {}, loc)); |
| 87 |
} |
| 88 |
|
| 89 |
std::unique_ptr<Expr> |
| 90 |
Builder::qualified_path_in_expression (std::unique_ptr<Type> &&type, |
| 91 |
TypePath trait, |
| 92 |
PathExprSegment segment) const |
| 93 |
{ |
| 94 |
auto segments = {segment}; |
| 95 |
|
| 96 |
return qualified_path_in_expression (std::move (type), trait, segments); |
| 97 |
} |
| 98 |
|
| 99 |
std::unique_ptr<Expr> |
| 100 |
Builder::qualified_path_in_expression ( |
| 101 |
std::unique_ptr<Type> &&type, TypePath trait, |
| 102 |
std::vector<PathExprSegment> &&segments) const |
| 103 |
{ |
| 104 |
auto qual_type = QualifiedPathType (std::move (type), loc, trait); |
| 105 |
|
| 106 |
return std::unique_ptr<QualifiedPathInExpression> ( |
| 107 |
new QualifiedPathInExpression (qual_type, std::move (segments), {}, loc)); |
| 108 |
} |
| 109 |
|
| 110 |
std::unique_ptr<Expr> |
| 111 |
Builder::identifier (std::string name) const |
| 112 |
{ |
| 113 |
return std::unique_ptr<Expr> (new IdentifierExpr (name, {}, loc)); |
| 114 |
} |
| 115 |
|
| 116 |
std::unique_ptr<Pattern> |
| 117 |
Builder::identifier_pattern (std::string name, bool mut) const |
| 118 |
{ |
| 119 |
return std::unique_ptr<Pattern> ( |
| 120 |
new IdentifierPattern (name, loc, false, mut)); |
| 121 |
} |
| 122 |
|
| 123 |
std::unique_ptr<Expr> |
| 124 |
Builder::tuple_idx (std::string receiver, int idx) const |
| 125 |
{ |
| 126 |
return std::unique_ptr<Expr> ( |
| 127 |
new TupleIndexExpr (identifier (receiver), idx, {}, loc)); |
| 128 |
} |
| 129 |
|
| 130 |
std::unique_ptr<Expr> |
| 131 |
Builder::tuple (std::vector<std::unique_ptr<Expr>> &&values) const |
| 132 |
{ |
| 133 |
return std::unique_ptr<TupleExpr> ( |
| 134 |
new TupleExpr (std::move (values), {}, {}, loc)); |
| 135 |
} |
| 136 |
|
| 137 |
std::unique_ptr<Param> |
| 138 |
Builder::self_ref_param (bool mutability) const |
| 139 |
{ |
| 140 |
return std::make_unique<SelfParam> (tl::nullopt, mutability, loc); |
| 141 |
} |
| 142 |
|
| 143 |
std::unique_ptr<Param> |
| 144 |
Builder::function_param (std::unique_ptr<Pattern> &&pattern, |
| 145 |
std::unique_ptr<Type> &&type) const |
| 146 |
{ |
| 147 |
return std::unique_ptr<FunctionParam> ( |
| 148 |
new FunctionParam (std::move (pattern), std::move (type), {}, loc)); |
| 149 |
} |
| 150 |
|
| 151 |
FunctionQualifiers |
| 152 |
Builder::fn_qualifiers () const |
| 153 |
{ |
| 154 |
return FunctionQualifiers (loc, Async::No, Const::No, Unsafety::Normal); |
| 155 |
} |
| 156 |
|
| 157 |
std::unique_ptr<Function> |
| 158 |
Builder::function (std::string function_name, |
| 159 |
std::vector<std::unique_ptr<Param>> params, |
| 160 |
std::unique_ptr<Type> return_type, |
| 161 |
std::unique_ptr<BlockExpr> block, |
| 162 |
std::vector<std::unique_ptr<GenericParam>> generic_params, |
| 163 |
FunctionQualifiers qualifiers, WhereClause where_clause, |
| 164 |
Visibility visibility) const |
| 165 |
{ |
| 166 |
return std::unique_ptr<Function> ( |
| 167 |
new Function (function_name, qualifiers, std::move (generic_params), |
| 168 |
std::move (params), std::move (return_type), where_clause, |
| 169 |
std::move (block), visibility, {}, loc)); |
| 170 |
} |
| 171 |
|
| 172 |
PathExprSegment |
| 173 |
Builder::path_segment (std::string seg) const |
| 174 |
{ |
| 175 |
return PathExprSegment (PathIdentSegment (seg, loc), loc); |
| 176 |
} |
| 177 |
|
| 178 |
std::unique_ptr<TypePathSegment> |
| 179 |
Builder::type_path_segment (std::string seg) const |
| 180 |
{ |
| 181 |
return std::unique_ptr<TypePathSegment> ( |
| 182 |
new TypePathSegment (seg, false, loc)); |
| 183 |
} |
| 184 |
|
| 185 |
std::unique_ptr<TypePathSegment> |
| 186 |
Builder::type_path_segment (LangItem::Kind lang_item) const |
| 187 |
{ |
| 188 |
return std::unique_ptr<TypePathSegment> ( |
| 189 |
new TypePathSegment (lang_item, loc)); |
| 190 |
} |
| 191 |
|
| 192 |
std::unique_ptr<TypePathSegment> |
| 193 |
Builder::type_path_segment_generic (std::string seg, GenericArgs args) const |
| 194 |
{ |
| 195 |
return std::unique_ptr<TypePathSegment> ( |
| 196 |
new TypePathSegmentGeneric (PathIdentSegment (seg, loc), false, args, loc)); |
| 197 |
} |
| 198 |
|
| 199 |
std::unique_ptr<TypePathSegment> |
| 200 |
Builder::type_path_segment_generic (LangItem::Kind lang_item, |
| 201 |
GenericArgs args) const |
| 202 |
{ |
| 203 |
return std::unique_ptr<TypePathSegment> ( |
| 204 |
new TypePathSegmentGeneric (lang_item, args, loc)); |
| 205 |
} |
| 206 |
|
| 207 |
std::unique_ptr<Type> |
| 208 |
Builder::single_type_path (std::string type) const |
| 209 |
{ |
| 210 |
auto segments = std::vector<std::unique_ptr<TypePathSegment>> (); |
| 211 |
segments.emplace_back (type_path_segment (type)); |
| 212 |
|
| 213 |
return std::unique_ptr<Type> (new TypePath (std::move (segments), loc)); |
| 214 |
} |
| 215 |
|
| 216 |
std::unique_ptr<Type> |
| 217 |
Builder::single_type_path (LangItem::Kind lang_item) const |
| 218 |
{ |
| 219 |
return std::unique_ptr<Type> (new TypePath (lang_item, {}, loc)); |
| 220 |
} |
| 221 |
|
| 222 |
std::unique_ptr<Type> |
| 223 |
Builder::single_generic_type_path (std::string type, GenericArgs args) const |
| 224 |
{ |
| 225 |
auto segments = std::vector<std::unique_ptr<TypePathSegment>> (); |
| 226 |
segments.emplace_back (type_path_segment_generic (type, args)); |
| 227 |
|
| 228 |
return std::unique_ptr<Type> (new TypePath (std::move (segments), loc)); |
| 229 |
} |
| 230 |
|
| 231 |
std::unique_ptr<Type> |
| 232 |
Builder::single_generic_type_path (LangItem::Kind lang_item, |
| 233 |
GenericArgs args) const |
| 234 |
{ |
| 235 |
auto segments = std::vector<std::unique_ptr<TypePathSegment>> (); |
| 236 |
segments.emplace_back (type_path_segment_generic (lang_item, args)); |
| 237 |
|
| 238 |
return std::unique_ptr<Type> (new TypePath (std::move (segments), loc)); |
| 239 |
} |
| 240 |
|
| 241 |
TypePath |
| 242 |
Builder::type_path (std::vector<std::unique_ptr<TypePathSegment>> &&segments, |
| 243 |
bool opening_scope) const |
| 244 |
{ |
| 245 |
return TypePath (std::move (segments), loc, opening_scope); |
| 246 |
} |
| 247 |
|
| 248 |
TypePath |
| 249 |
Builder::type_path (std::vector<std::string> &&segments, |
| 250 |
bool opening_scope) const |
| 251 |
{ |
| 252 |
auto type_segments = std::vector<std::unique_ptr<TypePathSegment>> (); |
| 253 |
|
| 254 |
for (auto &&segment : segments) |
| 255 |
type_segments.emplace_back (type_path_segment (segment)); |
| 256 |
|
| 257 |
return TypePath (std::move (type_segments), loc, opening_scope); |
| 258 |
} |
| 259 |
|
| 260 |
TypePath |
| 261 |
Builder::type_path (std::unique_ptr<TypePathSegment> &&segment) const |
| 262 |
{ |
| 263 |
auto segments = std::vector<std::unique_ptr<TypePathSegment>> (); |
| 264 |
segments.emplace_back (std::move (segment)); |
| 265 |
|
| 266 |
return type_path (std::move (segments)); |
| 267 |
} |
| 268 |
|
| 269 |
TypePath |
| 270 |
Builder::type_path (std::string type) const |
| 271 |
{ |
| 272 |
return type_path (type_path_segment (type)); |
| 273 |
} |
| 274 |
|
| 275 |
TypePath |
| 276 |
Builder::type_path (LangItem::Kind lang_item) const |
| 277 |
{ |
| 278 |
return type_path (type_path_segment (lang_item)); |
| 279 |
} |
| 280 |
|
| 281 |
std::unique_ptr<Type> |
| 282 |
Builder::reference_type (std::unique_ptr<TypeNoBounds> &&inner_type, |
| 283 |
bool mutability) const |
| 284 |
{ |
| 285 |
return std::make_unique<ReferenceType> (mutability, std::move (inner_type), |
| 286 |
loc); |
| 287 |
} |
| 288 |
|
| 289 |
PathInExpression |
| 290 |
Builder::path_in_expression (std::vector<std::string> &&segments, |
| 291 |
bool opening_scope) const |
| 292 |
{ |
| 293 |
auto path_segments = std::vector<PathExprSegment> (); |
| 294 |
for (auto &seg : segments) |
| 295 |
path_segments.emplace_back (path_segment (seg)); |
| 296 |
|
| 297 |
return PathInExpression (std::move (path_segments), {}, loc, opening_scope); |
| 298 |
} |
| 299 |
|
| 300 |
PathInExpression |
| 301 |
Builder::path_in_expression (LangItem::Kind lang_item) const |
| 302 |
{ |
| 303 |
return PathInExpression (lang_item, {}, loc); |
| 304 |
} |
| 305 |
|
| 306 |
PathInExpression |
| 307 |
Builder::variant_path (const std::string &enum_path, |
| 308 |
const std::string &variant) const |
| 309 |
{ |
| 310 |
return PathInExpression ({path_segment (enum_path), path_segment (variant)}, |
| 311 |
{}, loc, false); |
| 312 |
} |
| 313 |
|
| 314 |
std::unique_ptr<BlockExpr> |
| 315 |
Builder::block (tl::optional<std::unique_ptr<Stmt>> &&stmt, |
| 316 |
std::unique_ptr<Expr> &&tail_expr) const |
| 317 |
{ |
| 318 |
auto stmts = std::vector<std::unique_ptr<Stmt>> (); |
| 319 |
|
| 320 |
if (stmt) |
| 321 |
stmts.emplace_back (std::move (*stmt)); |
| 322 |
|
| 323 |
return block (std::move (stmts), std::move (tail_expr)); |
| 324 |
} |
| 325 |
|
| 326 |
std::unique_ptr<BlockExpr> |
| 327 |
Builder::block () const |
| 328 |
{ |
| 329 |
auto stmts = std::vector<std::unique_ptr<Stmt>> (); |
| 330 |
|
| 331 |
return block (std::move (stmts)); |
| 332 |
} |
| 333 |
|
| 334 |
std::unique_ptr<BlockExpr> |
| 335 |
Builder::block (std::vector<std::unique_ptr<Stmt>> &&stmts, |
| 336 |
std::unique_ptr<Expr> &&tail_expr) const |
| 337 |
{ |
| 338 |
return std::unique_ptr<BlockExpr> (new BlockExpr (std::move (stmts), |
| 339 |
std::move (tail_expr), {}, |
| 340 |
{}, tl::nullopt, loc, loc)); |
| 341 |
} |
| 342 |
|
| 343 |
std::unique_ptr<Expr> |
| 344 |
Builder::return_expr (std::unique_ptr<Expr> &&to_return) |
| 345 |
{ |
| 346 |
return std::unique_ptr<Expr> ( |
| 347 |
new ReturnExpr (std::move (to_return), {}, loc)); |
| 348 |
} |
| 349 |
|
| 350 |
std::unique_ptr<Stmt> |
| 351 |
Builder::let (std::unique_ptr<Pattern> &&pattern, std::unique_ptr<Type> &&type, |
| 352 |
std::unique_ptr<Expr> &&init) const |
| 353 |
{ |
| 354 |
return std::unique_ptr<Stmt> (new LetStmt (std::move (pattern), |
| 355 |
std::move (init), std::move (type), |
| 356 |
tl::nullopt, {}, loc)); |
| 357 |
} |
| 358 |
|
| 359 |
std::unique_ptr<Expr> |
| 360 |
Builder::ref (std::unique_ptr<Expr> &&of, bool mut) const |
| 361 |
{ |
| 362 |
auto mutability = mut ? Mutability::Mut : Mutability::Imm; |
| 363 |
return std::unique_ptr<Expr> ( |
| 364 |
new BorrowExpr (std::move (of), mutability, |
| 365 |
/* raw */ false, /* is double */ false, {}, loc)); |
| 366 |
} |
| 367 |
|
| 368 |
std::unique_ptr<Expr> |
| 369 |
Builder::deref (std::unique_ptr<Expr> &&of) const |
| 370 |
{ |
| 371 |
return std::unique_ptr<Expr> (new DereferenceExpr (std::move (of), {}, loc)); |
| 372 |
} |
| 373 |
|
| 374 |
std::unique_ptr<Expr> |
| 375 |
Builder::comparison_expr (std::unique_ptr<Expr> &&lhs, |
| 376 |
std::unique_ptr<Expr> &&rhs, |
| 377 |
ComparisonOperator op) const |
| 378 |
{ |
| 379 |
return std::make_unique<ComparisonExpr> (std::move (lhs), std::move (rhs), op, |
| 380 |
loc); |
| 381 |
} |
| 382 |
|
| 383 |
std::unique_ptr<Expr> |
| 384 |
Builder::boolean_operation (std::unique_ptr<Expr> &&lhs, |
| 385 |
std::unique_ptr<Expr> &&rhs, |
| 386 |
LazyBooleanOperator op) const |
| 387 |
{ |
| 388 |
return std::make_unique<LazyBooleanExpr> (std::move (lhs), std::move (rhs), |
| 389 |
op, loc); |
| 390 |
} |
| 391 |
|
| 392 |
std::unique_ptr<Stmt> |
| 393 |
Builder::struct_struct (std::string struct_name, |
| 394 |
std::vector<std::unique_ptr<GenericParam>> &&generics, |
| 395 |
std::vector<StructField> &&fields) |
| 396 |
{ |
| 397 |
auto is_unit = fields.empty (); |
| 398 |
|
| 399 |
return std::unique_ptr<Stmt> ( |
| 400 |
new StructStruct (std::move (fields), struct_name, std::move (generics), |
| 401 |
WhereClause::create_empty (), is_unit, |
| 402 |
Visibility::create_private (), {}, loc)); |
| 403 |
} |
| 404 |
|
| 405 |
std::unique_ptr<Expr> |
| 406 |
Builder::struct_expr_struct (std::string struct_name) const |
| 407 |
{ |
| 408 |
return std::unique_ptr<Expr> ( |
| 409 |
new StructExprStruct (path_in_expression ({struct_name}), {}, {}, loc)); |
| 410 |
} |
| 411 |
|
| 412 |
std::unique_ptr<Expr> |
| 413 |
Builder::struct_expr ( |
| 414 |
std::string struct_name, |
| 415 |
std::vector<std::unique_ptr<StructExprField>> &&fields) const |
| 416 |
{ |
| 417 |
return struct_expr (path_in_expression ({struct_name}), std::move (fields)); |
| 418 |
} |
| 419 |
|
| 420 |
std::unique_ptr<Expr> |
| 421 |
Builder::struct_expr ( |
| 422 |
PathInExpression struct_name, |
| 423 |
std::vector<std::unique_ptr<StructExprField>> &&fields) const |
| 424 |
{ |
| 425 |
return std::unique_ptr<Expr> ( |
| 426 |
new StructExprStructFields (struct_name, std::move (fields), loc)); |
| 427 |
} |
| 428 |
|
| 429 |
std::unique_ptr<StructExprField> |
| 430 |
Builder::struct_expr_field (std::string field_name, |
| 431 |
std::unique_ptr<Expr> &&value) const |
| 432 |
{ |
| 433 |
return std::unique_ptr<StructExprField> ( |
| 434 |
new StructExprFieldIdentifierValue (field_name, std::move (value), loc)); |
| 435 |
} |
| 436 |
|
| 437 |
std::unique_ptr<Expr> |
| 438 |
Builder::field_access (std::unique_ptr<Expr> &&instance, |
| 439 |
std::string field) const |
| 440 |
{ |
| 441 |
return std::unique_ptr<Expr> ( |
| 442 |
new FieldAccessExpr (std::move (instance), field, {}, loc)); |
| 443 |
} |
| 444 |
|
| 445 |
std::unique_ptr<Pattern> |
| 446 |
Builder::wildcard () const |
| 447 |
{ |
| 448 |
return std::unique_ptr<Pattern> (new WildcardPattern (loc)); |
| 449 |
} |
| 450 |
|
| 451 |
std::unique_ptr<Pattern> |
| 452 |
Builder::ref_pattern (std::unique_ptr<Pattern> &&inner) const |
| 453 |
{ |
| 454 |
return std::make_unique<ReferencePattern> (std::move (inner), false, false, |
| 455 |
loc); |
| 456 |
} |
| 457 |
|
| 458 |
std::unique_ptr<Path> |
| 459 |
Builder::lang_item_path (LangItem::Kind kind) const |
| 460 |
{ |
| 461 |
return std::unique_ptr<Path> (new PathInExpression (kind, {}, loc)); |
| 462 |
} |
| 463 |
|
| 464 |
std::unique_ptr<Expr> |
| 465 |
Builder::match (std::unique_ptr<Expr> &&scrutinee, |
| 466 |
std::vector<MatchCase> &&cases) |
| 467 |
{ |
| 468 |
return std::unique_ptr<Expr> ( |
| 469 |
new MatchExpr (std::move (scrutinee), std::move (cases), {}, {}, loc)); |
| 470 |
} |
| 471 |
|
| 472 |
MatchArm |
| 473 |
Builder::match_arm (std::unique_ptr<Pattern> &&pattern) |
| 474 |
{ |
| 475 |
auto patterns = std::vector<std::unique_ptr<Pattern>> (); |
| 476 |
patterns.emplace_back (std::move (pattern)); |
| 477 |
|
| 478 |
return MatchArm (std::move (patterns), loc); |
| 479 |
} |
| 480 |
|
| 481 |
MatchCase |
| 482 |
Builder::match_case (std::unique_ptr<Pattern> &&pattern, |
| 483 |
std::unique_ptr<Expr> &&expr) |
| 484 |
{ |
| 485 |
return MatchCase (match_arm (std::move (pattern)), std::move (expr)); |
| 486 |
} |
| 487 |
|
| 488 |
std::unique_ptr<Expr> |
| 489 |
Builder::loop (std::vector<std::unique_ptr<Stmt>> &&stmts) |
| 490 |
{ |
| 491 |
auto block_expr = block (std::move (stmts), nullptr); |
| 492 |
|
| 493 |
return std::unique_ptr<Expr> (new LoopExpr (std::move (block_expr), loc)); |
| 494 |
} |
| 495 |
|
| 496 |
std::unique_ptr<TypeParamBound> |
| 497 |
Builder::trait_bound (TypePath bound) |
| 498 |
{ |
| 499 |
return std::make_unique<TraitBound> (bound, loc); |
| 500 |
} |
| 501 |
|
| 502 |
std::unique_ptr<Item> |
| 503 |
Builder::trait_impl (TypePath trait_path, std::unique_ptr<Type> target, |
| 504 |
std::vector<std::unique_ptr<AssociatedItem>> trait_items, |
| 505 |
std::vector<std::unique_ptr<GenericParam>> generics, |
| 506 |
WhereClause where_clause, Visibility visibility) const |
| 507 |
{ |
| 508 |
return std::unique_ptr<Item> ( |
| 509 |
new TraitImpl (trait_path, /* unsafe */ false, |
| 510 |
/* exclam */ false, std::move (trait_items), |
| 511 |
std::move (generics), std::move (target), where_clause, |
| 512 |
visibility, {}, {}, loc)); |
| 513 |
} |
| 514 |
|
| 515 |
std::unique_ptr<GenericParam> |
| 516 |
Builder::generic_type_param ( |
| 517 |
std::string type_representation, |
| 518 |
std::vector<std::unique_ptr<TypeParamBound>> &&bounds, |
| 519 |
std::unique_ptr<Type> &&type) |
| 520 |
{ |
| 521 |
return std::make_unique<TypeParam> (type_representation, loc, |
| 522 |
std::move (bounds), std::move (type), |
| 523 |
std::vector<Attribute> ()); |
| 524 |
} |
| 525 |
|
| 526 |
std::unique_ptr<Type> |
| 527 |
Builder::new_type (Type &type) |
| 528 |
{ |
| 529 |
Type *t = ASTTypeBuilder::build (type); |
| 530 |
return std::unique_ptr<Type> (t); |
| 531 |
} |
| 532 |
|
| 533 |
std::unique_ptr<GenericParam> |
| 534 |
Builder::new_lifetime_param (LifetimeParam ¶m) |
| 535 |
{ |
| 536 |
Lifetime l = new_lifetime (param.get_lifetime ()); |
| 537 |
std::vector<Lifetime> lifetime_bounds; |
| 538 |
for (auto b : param.get_lifetime_bounds ()) |
| 539 |
{ |
| 540 |
Lifetime bl = new_lifetime (b); |
| 541 |
lifetime_bounds.push_back (bl); |
| 542 |
} |
| 543 |
|
| 544 |
auto p = new LifetimeParam (l, std::move (lifetime_bounds), |
| 545 |
param.get_outer_attrs (), param.get_locus ()); |
| 546 |
return std::unique_ptr<GenericParam> (p); |
| 547 |
} |
| 548 |
|
| 549 |
std::unique_ptr<GenericParam> |
| 550 |
Builder::new_type_param ( |
| 551 |
TypeParam ¶m, std::vector<std::unique_ptr<TypeParamBound>> extra_bounds) |
| 552 |
{ |
| 553 |
location_t locus = param.get_locus (); |
| 554 |
AST::AttrVec outer_attrs = param.get_outer_attrs (); |
| 555 |
Identifier type_representation = param.get_type_representation (); |
| 556 |
std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds; |
| 557 |
std::unique_ptr<Type> type = nullptr; |
| 558 |
|
| 559 |
if (param.has_type ()) |
| 560 |
type = new_type (param.get_type ()); |
| 561 |
|
| 562 |
for (auto &&extra_bound : extra_bounds) |
| 563 |
type_param_bounds.emplace_back (std::move (extra_bound)); |
| 564 |
|
| 565 |
for (const auto &b : param.get_type_param_bounds ()) |
| 566 |
{ |
| 567 |
switch (b->get_bound_type ()) |
| 568 |
{ |
| 569 |
case TypeParamBound::TypeParamBoundType::TRAIT: { |
| 570 |
const TraitBound &tb = (const TraitBound &) *b.get (); |
| 571 |
const TypePath &path = tb.get_type_path (); |
| 572 |
|
| 573 |
std::vector<LifetimeParam> for_lifetimes; |
| 574 |
for (const auto &lifetime : tb.get_for_lifetimes ()) |
| 575 |
{ |
| 576 |
std::vector<Lifetime> lifetime_bounds; |
| 577 |
for (const auto &b : lifetime.get_lifetime_bounds ()) |
| 578 |
{ |
| 579 |
Lifetime bl = new_lifetime (b); |
| 580 |
lifetime_bounds.push_back (std::move (bl)); |
| 581 |
} |
| 582 |
|
| 583 |
Lifetime nl = new_lifetime (lifetime.get_lifetime ()); |
| 584 |
LifetimeParam p (std::move (nl), std::move (lifetime_bounds), |
| 585 |
{}, lifetime.get_locus ()); |
| 586 |
for_lifetimes.push_back (std::move (p)); |
| 587 |
} |
| 588 |
|
| 589 |
std::vector<std::unique_ptr<TypePathSegment>> segments; |
| 590 |
for (auto &seg : path.get_segments ()) |
| 591 |
{ |
| 592 |
switch (seg->get_type ()) |
| 593 |
{ |
| 594 |
case TypePathSegment::REG: { |
| 595 |
const TypePathSegment &segment |
| 596 |
= (const TypePathSegment &) (*seg.get ()); |
| 597 |
TypePathSegment *s = new TypePathSegment ( |
| 598 |
segment.get_ident_segment (), |
| 599 |
segment.get_separating_scope_resolution (), |
| 600 |
segment.get_locus ()); |
| 601 |
std::unique_ptr<TypePathSegment> sg (s); |
| 602 |
segments.push_back (std::move (sg)); |
| 603 |
} |
| 604 |
break; |
| 605 |
|
| 606 |
case TypePathSegment::GENERIC: { |
| 607 |
TypePathSegmentGeneric &generic |
| 608 |
= (TypePathSegmentGeneric &) (*seg.get ()); |
| 609 |
|
| 610 |
GenericArgs args |
| 611 |
= new_generic_args (generic.get_generic_args ()); |
| 612 |
TypePathSegmentGeneric *s = new TypePathSegmentGeneric ( |
| 613 |
generic.get_ident_segment (), false, std::move (args), |
| 614 |
generic.get_locus ()); |
| 615 |
std::unique_ptr<TypePathSegment> sg (s); |
| 616 |
segments.push_back (std::move (sg)); |
| 617 |
} |
| 618 |
break; |
| 619 |
|
| 620 |
case TypePathSegment::FUNCTION: { |
| 621 |
rust_unreachable (); |
| 622 |
// TODO |
| 623 |
// const TypePathSegmentFunction &fn |
| 624 |
// = (const TypePathSegmentFunction &) (*seg.get ()); |
| 625 |
} |
| 626 |
break; |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
TypePath p (std::move (segments), path.get_locus (), |
| 631 |
path.has_opening_scope_resolution_op ()); |
| 632 |
|
| 633 |
TraitBound *b = new TraitBound (std::move (p), tb.get_locus (), |
| 634 |
tb.is_in_parens (), |
| 635 |
tb.has_opening_question_mark (), |
| 636 |
std::move (for_lifetimes)); |
| 637 |
std::unique_ptr<TypeParamBound> bound (b); |
| 638 |
type_param_bounds.push_back (std::move (bound)); |
| 639 |
} |
| 640 |
break; |
| 641 |
|
| 642 |
case TypeParamBound::TypeParamBoundType::LIFETIME: { |
| 643 |
const Lifetime &l = (const Lifetime &) *b.get (); |
| 644 |
|
| 645 |
auto bl = new Lifetime (l.get_lifetime_type (), |
| 646 |
l.get_lifetime_name (), l.get_locus ()); |
| 647 |
std::unique_ptr<TypeParamBound> bound (bl); |
| 648 |
type_param_bounds.push_back (std::move (bound)); |
| 649 |
} |
| 650 |
break; |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
auto type_param |
| 655 |
= new TypeParam (type_representation, locus, std::move (type_param_bounds), |
| 656 |
std::move (type), std::move (outer_attrs)); |
| 657 |
|
| 658 |
return std::unique_ptr<GenericParam> (type_param); |
| 659 |
} |
| 660 |
|
| 661 |
Lifetime |
| 662 |
Builder::new_lifetime (const Lifetime &lifetime) |
| 663 |
{ |
| 664 |
return Lifetime (lifetime.get_lifetime_type (), lifetime.get_lifetime_name (), |
| 665 |
lifetime.get_locus ()); |
| 666 |
} |
| 667 |
|
| 668 |
GenericArgs |
| 669 |
Builder::new_generic_args (GenericArgs &args) |
| 670 |
{ |
| 671 |
std::vector<Lifetime> lifetime_args; |
| 672 |
std::vector<GenericArg> generic_args; |
| 673 |
std::vector<GenericArgsBinding> binding_args; |
| 674 |
location_t locus = args.get_locus (); |
| 675 |
|
| 676 |
for (const auto &lifetime : args.get_lifetime_args ()) |
| 677 |
{ |
| 678 |
Lifetime l = new_lifetime (lifetime); |
| 679 |
lifetime_args.push_back (std::move (l)); |
| 680 |
} |
| 681 |
|
| 682 |
for (auto &binding : args.get_binding_args ()) |
| 683 |
{ |
| 684 |
Type &t = *binding.get_type_ptr ().get (); |
| 685 |
std::unique_ptr<Type> ty = new_type (t); |
| 686 |
GenericArgsBinding b (binding.get_identifier (), std::move (ty), |
| 687 |
binding.get_locus ()); |
| 688 |
binding_args.push_back (std::move (b)); |
| 689 |
} |
| 690 |
|
| 691 |
for (auto &arg : args.get_generic_args ()) |
| 692 |
{ |
| 693 |
switch (arg.get_kind ()) |
| 694 |
{ |
| 695 |
case GenericArg::Kind::Type: { |
| 696 |
std::unique_ptr<Type> ty = new_type (arg.get_type ()); |
| 697 |
GenericArg arg = GenericArg::create_type (std::move (ty)); |
| 698 |
} |
| 699 |
break; |
| 700 |
|
| 701 |
default: |
| 702 |
// FIXME |
| 703 |
rust_unreachable (); |
| 704 |
break; |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
return GenericArgs (std::move (lifetime_args), std::move (generic_args), |
| 709 |
std::move (binding_args), locus); |
| 710 |
} |
| 711 |
|
| 712 |
} // namespace AST |
| 713 |
} // namespace Rust |