https://gcc.gnu.org/g:3d0c831d9d0b3eb10678d9db58fd5b04090a2996
commit r16-4768-g3d0c831d9d0b3eb10678d9db58fd5b04090a2996 Author: Pierre-Emmanuel Patry <[email protected]> Date: Wed Aug 6 17:35:03 2025 +0200 gccrs: Improve vector usages and avoid copies. Several place built an object before copying it in a vector. This commit favorise in place construction and use readily available vector size to reserve memory beforehand. gcc/rust/ChangeLog: * ast/rust-expr.h (struct TupleClobber): Add constructor. * backend/rust-compile-context.h (struct fncontext): Likewise. * typecheck/rust-hir-dot-operator.h: Likewise. * typecheck/rust-tyty-variance-analysis-private.h (struct Constraint): Likewise. * typecheck/rust-unify.h: Likewise. * ast/rust-ast-builder.cc (Builder::new_lifetime_param): Add memory reservation and construct in place. (Builder::new_generic_args): Likewise. * ast/rust-ast-collector.cc (TokenCollector::newline): Likewise. (TokenCollector::indentation): Likewise. (TokenCollector::comment): Likewise. * ast/rust-desugar-apit.cc: Likewise. * ast/rust-path.cc (Path::convert_to_simple_path): Likewise. (TypePath::as_simple_path): Likewise. * ast/rust-path.h: Likewise. * backend/rust-compile-type.cc (TyTyResolveCompile::visit): Likewise. (TyTyResolveCompile::create_dyn_obj_record): Likewise. * checks/errors/rust-hir-pattern-analysis.cc (Matrix::specialize): Likewise. (WitnessMatrix::apply_constructor): Likewise. (check_match_usefulness): Likewise. * expand/rust-derive-clone.cc (DeriveClone::clone_fn): Likewise. * expand/rust-macro-builtins-asm.cc (parse_clobber_abi): Likewise. * expand/rust-macro-expand.cc (MacroExpander::parse_proc_macro_output): Likewise. * hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_generic_args): Likewise. (ASTLoweringBase::lower_extern_block): Likewise. * hir/rust-ast-lower-enumitem.h: Likewise. * hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Likewise. * hir/rust-ast-lower-extern.h: Likewise. * hir/rust-ast-lower-implitem.cc (ASTLowerImplItem::visit): Likewise. (ASTLowerTraitItem::visit): Likewise. * hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Likewise. * hir/rust-ast-lower-pattern.cc (ASTLoweringPattern::visit): Likewise. * hir/rust-ast-lower.cc (ASTLowering::go): Likewise. (ASTLoweringBlock::visit): Likewise. (ASTLoweringIfLetBlock::desugar_iflet): Likewise. (ASTLoweringExprWithBlock::visit): Likewise. (ASTLowerPathInExpression::visit): Likewise. (ASTLowerQualPathInExpression::visit): Likewise. * hir/tree/rust-hir.cc (PathPattern::convert_to_simple_path): Likewise. (TypePath::as_simple_path): Likewise. * metadata/rust-export-metadata.cc (ExportContext::emit_function): Likewise. * parse/rust-parse-impl.h (Parser::parse_decl_macro_def): Likewise. (Parser::parse_lifetime_params): Likewise. * resolve/rust-ast-resolve-item.cc (ResolveTraitItems::visit): Likewise. (ResolveItem::visit): Likewise. (flatten_list): Likewise. * resolve/rust-ast-resolve-pattern.cc (PatternDeclaration::visit): Likewise. * typecheck/rust-autoderef.cc (AutoderefCycle::try_autoderefed): Likewise. * typecheck/rust-coercion.cc (TypeCoercionRules::coerce_unsized): Likewise. * typecheck/rust-hir-dot-operator.cc: Likewise. * typecheck/rust-hir-path-probe.cc: Likewise. * typecheck/rust-hir-trait-resolve.cc (TraitResolver::resolve_trait): Likewise. * typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Likewise. (TypeCheckExpr::resolve_fn_trait_call): Likewise. * typecheck/rust-hir-type-check-implitem.cc (TypeCheckTopLevelExternItem::visit): Likewise. (TypeCheckImplItem::visit): Likewise. * typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): Likewise. * typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): Likewise. * typecheck/rust-hir-type-check-struct.cc (TypeCheckStructExpr::resolve): Likewise. * typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): Likewise. * typecheck/rust-hir-type-check.cc (TraitItemReference::get_type_from_fn): Likewise. * typecheck/rust-typecheck-context.cc (TypeCheckContext::push_return_type): Likewise. (TypeCheckContext::insert_associated_impl_mapping): Likewise. * typecheck/rust-tyty-bounds.cc (TypeBoundsProbe::scan): Likewise. (TypeBoundsProbe::add_trait_bound): Likewise. (TypeBoundPredicate::operator=): Likewise. (TypeBoundPredicateItem::get_tyty_for_receiver): Likewise. (TypeBoundPredicate::get_associated_type_items): Likewise. * typecheck/rust-tyty-call.cc (TypeCheckMethodCallExpr::go): Likewise. * typecheck/rust-tyty-subst.cc (SubstitutionRef::clone_substs): Likewise. (SubstitutionRef::infer_substitions): Likewise. (SubstitutionRef::are_mappings_bound): Likewise. * typecheck/rust-tyty-variance-analysis.cc (GenericTyPerCrateCtx::query_generic_variance): Likewise. (GenericTyVisitorCtx::add_constraint): Likewise. * typecheck/rust-tyty.cc (FnPtr::clone): Likewise. (DynamicObjectType::get_object_items): Likewise. * typecheck/rust-unify.cc (UnifyRules::Resolve): Likewise. (UnifyRules::go): Likewise. (UnifyRules::expect_tuple): Likewise. * util/rust-canonical-path.h: Likewise. * util/rust-token-converter.cc (convert): Likewise. Signed-off-by: Pierre-Emmanuel Patry <[email protected]> Diff: --- gcc/rust/ast/rust-ast-builder.cc | 56 ++++---- gcc/rust/ast/rust-ast-collector.cc | 6 +- gcc/rust/ast/rust-desugar-apit.cc | 26 ++-- gcc/rust/ast/rust-expr.h | 2 + gcc/rust/ast/rust-path.cc | 8 +- gcc/rust/ast/rust-path.h | 8 +- gcc/rust/backend/rust-compile-context.h | 10 +- gcc/rust/backend/rust-compile-type.cc | 58 ++++----- .../checks/errors/rust-hir-pattern-analysis.cc | 6 +- gcc/rust/expand/rust-derive-clone.cc | 7 +- gcc/rust/expand/rust-macro-builtins-asm.cc | 2 +- gcc/rust/expand/rust-macro-expand.cc | 4 +- gcc/rust/hir/rust-ast-lower-base.cc | 56 ++++---- gcc/rust/hir/rust-ast-lower-enumitem.h | 9 +- gcc/rust/hir/rust-ast-lower-expr.cc | 55 ++++---- gcc/rust/hir/rust-ast-lower-extern.h | 5 +- gcc/rust/hir/rust-ast-lower-implitem.cc | 16 +-- gcc/rust/hir/rust-ast-lower-item.cc | 143 +++++++++++---------- gcc/rust/hir/rust-ast-lower-pattern.cc | 21 ++- gcc/rust/hir/rust-ast-lower.cc | 52 ++++---- gcc/rust/hir/tree/rust-hir.cc | 9 +- gcc/rust/metadata/rust-export-metadata.cc | 4 +- gcc/rust/parse/rust-parse-impl.h | 13 +- gcc/rust/resolve/rust-ast-resolve-item.cc | 13 +- gcc/rust/resolve/rust-ast-resolve-pattern.cc | 7 +- gcc/rust/typecheck/rust-autoderef.cc | 6 +- gcc/rust/typecheck/rust-coercion.cc | 12 +- gcc/rust/typecheck/rust-hir-dot-operator.cc | 12 +- gcc/rust/typecheck/rust-hir-dot-operator.h | 16 +++ gcc/rust/typecheck/rust-hir-path-probe.cc | 6 +- gcc/rust/typecheck/rust-hir-trait-resolve.cc | 18 +-- gcc/rust/typecheck/rust-hir-type-check-expr.cc | 23 ++-- gcc/rust/typecheck/rust-hir-type-check-implitem.cc | 8 +- gcc/rust/typecheck/rust-hir-type-check-item.cc | 8 +- gcc/rust/typecheck/rust-hir-type-check-pattern.cc | 8 +- gcc/rust/typecheck/rust-hir-type-check-struct.cc | 11 +- gcc/rust/typecheck/rust-hir-type-check-type.cc | 8 +- gcc/rust/typecheck/rust-hir-type-check.cc | 6 +- gcc/rust/typecheck/rust-typecheck-context.cc | 4 +- gcc/rust/typecheck/rust-tyty-bounds.cc | 26 ++-- gcc/rust/typecheck/rust-tyty-call.cc | 4 +- gcc/rust/typecheck/rust-tyty-subst.cc | 33 ++--- .../rust-tyty-variance-analysis-private.h | 4 + gcc/rust/typecheck/rust-tyty-variance-analysis.cc | 4 +- gcc/rust/typecheck/rust-tyty.cc | 6 +- gcc/rust/typecheck/rust-unify.cc | 8 +- gcc/rust/typecheck/rust-unify.h | 10 ++ gcc/rust/util/rust-canonical-path.h | 2 + gcc/rust/util/rust-token-converter.cc | 4 +- 49 files changed, 418 insertions(+), 425 deletions(-) diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc index ed10ce7f9a18..632f9455e3b6 100644 --- a/gcc/rust/ast/rust-ast-builder.cc +++ b/gcc/rust/ast/rust-ast-builder.cc @@ -554,12 +554,12 @@ std::unique_ptr<GenericParam> Builder::new_lifetime_param (LifetimeParam ¶m) { Lifetime l = new_lifetime (param.get_lifetime ()); + std::vector<Lifetime> lifetime_bounds; + lifetime_bounds.reserve (param.get_lifetime_bounds ().size ()); + for (auto b : param.get_lifetime_bounds ()) - { - Lifetime bl = new_lifetime (b); - lifetime_bounds.push_back (bl); - } + lifetime_bounds.emplace_back (new_lifetime (b)); auto p = new LifetimeParam (l, std::move (lifetime_bounds), param.get_outer_attrs (), param.get_locus ()); @@ -605,11 +605,11 @@ Builder::new_type_param ( for (const auto &lifetime : tb.get_for_lifetimes ()) { std::vector<Lifetime> lifetime_bounds; + lifetime_bounds.reserve ( + lifetime.get_lifetime_bounds ().size ()); + for (const auto &b : lifetime.get_lifetime_bounds ()) - { - Lifetime bl = new_lifetime (b); - lifetime_bounds.push_back (std::move (bl)); - } + lifetime_bounds.emplace_back (new_lifetime (b)); Lifetime nl = new_lifetime (lifetime.get_lifetime ()); LifetimeParam p (std::move (nl), std::move (lifetime_bounds), @@ -626,12 +626,11 @@ Builder::new_type_param ( { const TypePathSegment &segment = (const TypePathSegment &) (*seg.get ()); - TypePathSegment *s = new TypePathSegment ( + + segments.emplace_back (new TypePathSegment ( segment.get_ident_segment (), segment.get_separating_scope_resolution (), - segment.get_locus ()); - std::unique_ptr<TypePathSegment> sg (s); - segments.push_back (std::move (sg)); + segment.get_locus ())); } break; @@ -642,11 +641,10 @@ Builder::new_type_param ( GenericArgs args = new_generic_args (generic.get_generic_args ()); - TypePathSegmentGeneric *s = new TypePathSegmentGeneric ( + + segments.emplace_back (new TypePathSegmentGeneric ( generic.get_ident_segment (), false, std::move (args), - generic.get_locus ()); - std::unique_ptr<TypePathSegment> sg (s); - segments.push_back (std::move (sg)); + generic.get_locus ())); } break; @@ -664,12 +662,9 @@ Builder::new_type_param ( TypePath p (std::move (segments), path.get_locus (), path.has_opening_scope_resolution_op ()); - TraitBound *b = new TraitBound (std::move (p), tb.get_locus (), - tb.is_in_parens (), - tb.has_opening_question_mark (), - std::move (for_lifetimes)); - std::unique_ptr<TypeParamBound> bound (b); - type_param_bounds.push_back (std::move (bound)); + type_param_bounds.emplace_back (new TraitBound ( + std::move (p), tb.get_locus (), tb.is_in_parens (), + tb.has_opening_question_mark (), std::move (for_lifetimes))); } break; @@ -677,10 +672,9 @@ Builder::new_type_param ( { const Lifetime &l = (const Lifetime &) *b.get (); - auto bl = new Lifetime (l.get_lifetime_type (), - l.get_lifetime_name (), l.get_locus ()); - std::unique_ptr<TypeParamBound> bound (bl); - type_param_bounds.push_back (std::move (bound)); + type_param_bounds.emplace_back ( + new Lifetime (l.get_lifetime_type (), l.get_lifetime_name (), + l.get_locus ())); } break; } @@ -709,18 +703,14 @@ Builder::new_generic_args (GenericArgs &args) location_t locus = args.get_locus (); for (const auto &lifetime : args.get_lifetime_args ()) - { - Lifetime l = new_lifetime (lifetime); - lifetime_args.push_back (std::move (l)); - } + lifetime_args.push_back (new_lifetime (lifetime)); for (auto &binding : args.get_binding_args ()) { Type &t = *binding.get_type_ptr ().get (); std::unique_ptr<Type> ty = t.reconstruct (); - GenericArgsBinding b (binding.get_identifier (), std::move (ty), - binding.get_locus ()); - binding_args.push_back (std::move (b)); + binding_args.emplace_back (binding.get_identifier (), std::move (ty), + binding.get_locus ()); } for (auto &arg : args.get_generic_args ()) diff --git a/gcc/rust/ast/rust-ast-collector.cc b/gcc/rust/ast/rust-ast-collector.cc index 3d9ea784d8b1..bd924d0746c1 100644 --- a/gcc/rust/ast/rust-ast-collector.cc +++ b/gcc/rust/ast/rust-ast-collector.cc @@ -76,13 +76,13 @@ TokenCollector::trailing_comma () void TokenCollector::newline () { - tokens.push_back ({CollectItem::Kind::Newline}); + tokens.emplace_back (CollectItem::Kind::Newline); } void TokenCollector::indentation () { - tokens.push_back ({indent_level}); + tokens.emplace_back (indent_level); } void @@ -101,7 +101,7 @@ TokenCollector::decrement_indentation () void TokenCollector::comment (std::string comment) { - tokens.push_back ({comment}); + tokens.emplace_back (comment); } void diff --git a/gcc/rust/ast/rust-desugar-apit.cc b/gcc/rust/ast/rust-desugar-apit.cc index bca14eef1eab..de34e15d889d 100644 --- a/gcc/rust/ast/rust-desugar-apit.cc +++ b/gcc/rust/ast/rust-desugar-apit.cc @@ -188,9 +188,10 @@ public: // Convert to TypePath by creating path segments std::vector<std::unique_ptr<TypePathSegment>> segments; - segments.push_back (std::unique_ptr<TypePathSegment> (new TypePathSegment ( - PathIdentSegment (ident.as_string (), type.get_locus ()), false, - type.get_locus ()))); + segments.emplace_back ( + new TypePathSegment (PathIdentSegment (ident.as_string (), + type.get_locus ()), + false, type.get_locus ())); // Create TypePath from segments auto type_path @@ -198,6 +199,8 @@ public: // Convert bounds from impl trait to generic parameter bounds std::vector<std::unique_ptr<TypeParamBound>> bounds; + bounds.reserve (type.get_type_param_bounds ().size ()); + for (auto &bound : type.get_type_param_bounds ()) bounds.push_back (bound->clone_type_param_bound ()); @@ -228,9 +231,10 @@ public: // Convert to TypePath by creating path segments std::vector<std::unique_ptr<TypePathSegment>> segments; - segments.push_back (std::unique_ptr<TypePathSegment> (new TypePathSegment ( - PathIdentSegment (ident.as_string (), type.get_locus ()), false, - type.get_locus ()))); + segments.emplace_back ( + new TypePathSegment (PathIdentSegment (ident.as_string (), + type.get_locus ()), + false, type.get_locus ())); // Create TypePath from segments auto type_path @@ -407,6 +411,9 @@ private: std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds; + type_param_bounds.reserve ( + tp.get_type_param_bounds ().size ()); + for (auto &b : tp.get_type_param_bounds ()) type_param_bounds.push_back (std::move (b)); tp.get_type_param_bounds ().clear (); @@ -459,9 +466,10 @@ private: std::vector<SimplePathSegment> simple_segs = {simple_seg}; auto simple_path = SimplePath (simple_segs, false, ident.get_locus ()); std::vector<std::unique_ptr<TypePathSegment>> segments; - segments.push_back (std::unique_ptr<TypePathSegment> (new TypePathSegment ( - PathIdentSegment (ident.as_string (), ident.get_locus ()), false, - ident.get_locus ()))); + segments.emplace_back ( + new TypePathSegment (PathIdentSegment (ident.as_string (), + ident.get_locus ()), + false, ident.get_locus ())); auto type_path = new TypePath (std::move (segments), ident.get_locus ()); return std::unique_ptr<Type> (type_path); } diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h index 7b0df25a1a36..03cad86de3d0 100644 --- a/gcc/rust/ast/rust-expr.h +++ b/gcc/rust/ast/rust-expr.h @@ -5494,6 +5494,8 @@ struct InlineAsmTemplatePiece struct TupleClobber { + TupleClobber (std::string symbol, location_t loc) : symbol (symbol), loc (loc) + {} // as gccrs still doesn't contain a symbol class I have put them as strings std::string symbol; location_t loc; diff --git a/gcc/rust/ast/rust-path.cc b/gcc/rust/ast/rust-path.cc index 793423aa6ee8..60ca4355cf07 100644 --- a/gcc/rust/ast/rust-path.cc +++ b/gcc/rust/ast/rust-path.cc @@ -173,8 +173,8 @@ Path::convert_to_simple_path (bool with_opening_scope_resolution) const // create segment and add to vector std::string segment_str = segment.as_string (); - simple_segments.push_back ( - SimplePathSegment (std::move (segment_str), segment.get_locus ())); + simple_segments.emplace_back (std::move (segment_str), + segment.get_locus ()); } // kind of a HACK to get locus depending on opening scope resolution @@ -258,8 +258,8 @@ TypePath::as_simple_path () const // create segment and add to vector std::string segment_str = segment->as_string (); - simple_segments.push_back ( - SimplePathSegment (std::move (segment_str), segment->get_locus ())); + simple_segments.emplace_back (std::move (segment_str), + segment->get_locus ()); } return SimplePath (std::move (simple_segments), has_opening_scope_resolution, diff --git a/gcc/rust/ast/rust-path.h b/gcc/rust/ast/rust-path.h index a1b19d559c33..c12f41f311ac 100644 --- a/gcc/rust/ast/rust-path.h +++ b/gcc/rust/ast/rust-path.h @@ -448,9 +448,7 @@ public: generic_args.clear (); generic_args.reserve (other.generic_args.size ()); for (const auto &arg : other.generic_args) - { - generic_args.push_back (GenericArg (arg)); - } + generic_args.emplace_back (arg); } ~GenericArgs () = default; @@ -465,9 +463,7 @@ public: generic_args.clear (); generic_args.reserve (other.generic_args.size ()); for (const auto &arg : other.generic_args) - { - generic_args.push_back (GenericArg (arg)); - } + generic_args.emplace_back (arg); return *this; } diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h index d4a642b653ca..69bba515882f 100644 --- a/gcc/rust/backend/rust-compile-context.h +++ b/gcc/rust/backend/rust-compile-context.h @@ -34,6 +34,10 @@ namespace Compile { struct fncontext { + fncontext (tree fndecl, ::Bvariable *ret_addr, TyTy::BaseType *retty) + : fndecl (fndecl), ret_addr (ret_addr), retty (retty) + {} + tree fndecl; ::Bvariable *ret_addr; TyTy::BaseType *retty; @@ -154,7 +158,7 @@ public: if (it == mono_fns.end ()) mono_fns[dId] = {}; - mono_fns[dId].push_back ({ref, fn}); + mono_fns[dId].emplace_back (ref, fn); } void insert_closure_decl (const TyTy::ClosureType *ref, tree fn) @@ -164,7 +168,7 @@ public: if (it == mono_closure_fns.end ()) mono_closure_fns[dId] = {}; - mono_closure_fns[dId].push_back ({ref, fn}); + mono_closure_fns[dId].emplace_back (ref, fn); } tree lookup_closure_decl (const TyTy::ClosureType *ref) @@ -279,7 +283,7 @@ public: void push_fn (tree fn, ::Bvariable *ret_addr, TyTy::BaseType *retty) { - fn_stack.push_back (fncontext{fn, ret_addr, retty}); + fn_stack.emplace_back (fn, ret_addr, retty); } void pop_fn () { fn_stack.pop_back (); } diff --git a/gcc/rust/backend/rust-compile-type.cc b/gcc/rust/backend/rust-compile-type.cc index 0622954b2a28..d58b53c5c92a 100644 --- a/gcc/rust/backend/rust-compile-type.cc +++ b/gcc/rust/backend/rust-compile-type.cc @@ -186,8 +186,8 @@ TyTyResolveCompile::visit (const TyTy::ClosureType &type) // this should be based on the closure move-ability tree decl_type = TyTyResolveCompile::compile (ctx, lookup); tree capture_type = build_reference_type (decl_type); - fields.push_back (Backend::typed_identifier (mappings_name, capture_type, - type.get_ident ().locus)); + fields.emplace_back (mappings_name, capture_type, + type.get_ident ().locus); } tree type_record = Backend::struct_type (fields); @@ -217,8 +217,7 @@ TyTyResolveCompile::visit (const TyTy::FnType &type) auto ret = TyTyResolveCompile::compile (ctx, hir_type, trait_object_mode); location_t return_type_locus = ctx->get_mappings ().lookup_location (hir_type->get_ref ()); - results.push_back ( - Backend::typed_identifier ("_", ret, return_type_locus)); + results.emplace_back ("_", ret, return_type_locus); } for (auto ¶m_pair : type.get_params ()) @@ -227,11 +226,10 @@ TyTyResolveCompile::visit (const TyTy::FnType &type) auto compiled_param_type = TyTyResolveCompile::compile (ctx, param_tyty, trait_object_mode); - auto compiled_param = Backend::typed_identifier ( - param_pair.get_pattern ().as_string (), compiled_param_type, - ctx->get_mappings ().lookup_location (param_tyty->get_ref ())); - - parameters.push_back (compiled_param); + parameters.emplace_back (param_pair.get_pattern ().as_string (), + compiled_param_type, + ctx->get_mappings ().lookup_location ( + param_tyty->get_ref ())); } if (!type.is_variadic ()) @@ -277,10 +275,9 @@ TyTyResolveCompile::visit (const TyTy::ADTType &type) tree compiled_field_ty = TyTyResolveCompile::compile (ctx, field->get_field_type ()); - Backend::typed_identifier f (field->get_name (), compiled_field_ty, - ctx->get_mappings ().lookup_location ( - type.get_ty_ref ())); - fields.push_back (std::move (f)); + fields.emplace_back (field->get_name (), compiled_field_ty, + ctx->get_mappings ().lookup_location ( + type.get_ty_ref ())); } type_record = type.is_union () ? Backend::union_type (fields, false) @@ -357,10 +354,9 @@ TyTyResolveCompile::visit (const TyTy::ADTType &type) == TyTy::VariantDef::VariantType::TUPLE) field_name = "__" + field->get_name (); - Backend::typed_identifier f ( - field_name, compiled_field_ty, - ctx->get_mappings ().lookup_location (type.get_ty_ref ())); - fields.push_back (std::move (f)); + fields.emplace_back (field_name, compiled_field_ty, + ctx->get_mappings ().lookup_location ( + type.get_ty_ref ())); } tree variant_record = Backend::struct_type (fields); @@ -382,10 +378,9 @@ TyTyResolveCompile::visit (const TyTy::ADTType &type) TyTy::VariantDef *variant = type.get_variants ().at (i++); std::string implicit_variant_name = variant->get_identifier (); - Backend::typed_identifier f (implicit_variant_name, variant_record, - ctx->get_mappings ().lookup_location ( - type.get_ty_ref ())); - enum_fields.push_back (std::move (f)); + enum_fields.emplace_back (implicit_variant_name, variant_record, + ctx->get_mappings ().lookup_location ( + type.get_ty_ref ())); } // @@ -460,10 +455,9 @@ TyTyResolveCompile::visit (const TyTy::TupleType &type) // this, rather than simply emitting the integer, is that this // approach makes it simpler to use a C-only debugger, or // GDB's C mode, when debugging Rust. - Backend::typed_identifier f ("__" + std::to_string (i), compiled_field_ty, - ctx->get_mappings ().lookup_location ( - type.get_ty_ref ())); - fields.push_back (std::move (f)); + fields.emplace_back ("__" + std::to_string (i), compiled_field_ty, + ctx->get_mappings ().lookup_location ( + type.get_ty_ref ())); } tree struct_type_record = Backend::struct_type (fields); @@ -772,17 +766,15 @@ TyTyResolveCompile::create_dyn_obj_record (const TyTy::DynamicObjectType &type) tree uint = Backend::integer_type (true, Backend::get_pointer_size ()); tree uintptr_ty = build_pointer_type (uint); - Backend::typed_identifier f ("pointer", uintptr_ty, - ctx->get_mappings ().lookup_location ( - type.get_ty_ref ())); - fields.push_back (std::move (f)); + fields.emplace_back ("pointer", uintptr_ty, + ctx->get_mappings ().lookup_location ( + type.get_ty_ref ())); tree vtable_size = build_int_cst (size_type_node, items.size ()); tree vtable_type = Backend::array_type (uintptr_ty, vtable_size); - Backend::typed_identifier vtf ("vtable", vtable_type, - ctx->get_mappings ().lookup_location ( - type.get_ty_ref ())); - fields.push_back (std::move (vtf)); + fields.emplace_back ("vtable", vtable_type, + ctx->get_mappings ().lookup_location ( + type.get_ty_ref ())); tree record = Backend::struct_type (fields); RS_DST_FLAG (record) = 1; diff --git a/gcc/rust/checks/errors/rust-hir-pattern-analysis.cc b/gcc/rust/checks/errors/rust-hir-pattern-analysis.cc index fee92599955e..7fe7f029f2aa 100644 --- a/gcc/rust/checks/errors/rust-hir-pattern-analysis.cc +++ b/gcc/rust/checks/errors/rust-hir-pattern-analysis.cc @@ -980,7 +980,7 @@ Matrix::specialize (const Constructor &ctor) const if (ctor.is_covered_by (hd.ctor ())) { pats.pop_head_constructor (ctor, subfields_place_info.size ()); - new_rows.push_back (MatrixRow (pats, row.is_under_guard ())); + new_rows.emplace_back (pats, row.is_under_guard ()); } } @@ -1186,7 +1186,7 @@ WitnessMatrix::apply_constructor (const Constructor &ctor, } } - stack.push_back (WitnessPat (ctor, subfield, ty)); + stack.emplace_back (ctor, subfield, ty); } } @@ -1597,7 +1597,7 @@ check_match_usefulness (Resolver::TypeCheckContext *ctx, MatchArm lowered = lower_arm (ctx, arm, scrutinee_ty); PatOrWild pat = PatOrWild::make_pattern (lowered.get_pat ()); pats.push (pat); - rows.push_back (MatrixRow (pats, lowered.has_guard ())); + rows.emplace_back (pats, lowered.has_guard ()); } std::vector<PlaceInfo> place_infos = {{PlaceInfo (scrutinee_ty)}}; diff --git a/gcc/rust/expand/rust-derive-clone.cc b/gcc/rust/expand/rust-derive-clone.cc index 321fa00d2ec8..5987d14db8a0 100644 --- a/gcc/rust/expand/rust-derive-clone.cc +++ b/gcc/rust/expand/rust-derive-clone.cc @@ -64,11 +64,10 @@ DeriveClone::clone_fn (std::unique_ptr<Expr> &&clone_expr) new BlockExpr ({}, std::move (clone_expr), {}, {}, tl::nullopt, loc, loc)); auto big_self_type = builder.single_type_path ("Self"); - std::unique_ptr<SelfParam> self (new SelfParam (tl::nullopt, - /* is_mut */ false, loc)); - std::vector<std::unique_ptr<Param>> params; - params.push_back (std::move (self)); + + params.emplace_back (new SelfParam (tl::nullopt, + /* is_mut */ false, loc)); return std::unique_ptr<AssociatedItem> ( new Function ({"clone"}, builder.fn_qualifiers (), /* generics */ {}, diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc b/gcc/rust/expand/rust-macro-builtins-asm.cc index 61222dbeacb2..f9d2947163d1 100644 --- a/gcc/rust/expand/rust-macro-builtins-asm.cc +++ b/gcc/rust/expand/rust-macro-builtins-asm.cc @@ -101,7 +101,7 @@ parse_clobber_abi (InlineAsmContext inline_asm_ctx) if (token->get_id () == STRING_LITERAL) { // TODO: Caring for span in here. - new_abis.push_back ({token->as_string (), token->get_locus ()}); + new_abis.emplace_back (token->as_string (), token->get_locus ()); } else { diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 4c54ceff98ac..5667a1a4f4f8 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -1165,7 +1165,7 @@ MacroExpander::parse_proc_macro_output (ProcMacro::TokenStream ts) auto result = parser.parse_item (false); if (result == nullptr) break; - nodes.push_back ({std::move (result)}); + nodes.emplace_back (std::move (result)); } break; case ContextType::STMT: @@ -1174,7 +1174,7 @@ MacroExpander::parse_proc_macro_output (ProcMacro::TokenStream ts) auto result = parser.parse_stmt (); if (result == nullptr) break; - nodes.push_back ({std::move (result)}); + nodes.emplace_back (std::move (result)); } break; case ContextType::TRAIT: diff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc index 9445658300cd..f3ae3ce71a3d 100644 --- a/gcc/rust/hir/rust-ast-lower-base.cc +++ b/gcc/rust/hir/rust-ast-lower-base.cc @@ -622,11 +622,10 @@ ASTLoweringBase::lower_generic_params ( std::vector<std::unique_ptr<AST::GenericParam>> ¶ms) { std::vector<std::unique_ptr<HIR::GenericParam>> lowered; + lowered.reserve (params.size ()); + for (auto &ast_param : params) - { - auto hir_param = ASTLowerGenericParam::translate (*ast_param); - lowered.push_back (std::unique_ptr<HIR::GenericParam> (hir_param)); - } + lowered.emplace_back (ASTLowerGenericParam::translate (*ast_param)); return lowered; } @@ -659,18 +658,16 @@ HIR::GenericArgs ASTLoweringBase::lower_generic_args (AST::GenericArgs &args) { std::vector<HIR::GenericArgsBinding> binding_args; + binding_args.reserve (args.get_binding_args ().size ()); + for (auto &binding : args.get_binding_args ()) - { - HIR::GenericArgsBinding b = lower_binding (binding); - binding_args.push_back (std::move (b)); - } + binding_args.emplace_back (lower_binding (binding)); std::vector<HIR::Lifetime> lifetime_args; + lifetime_args.reserve (args.get_lifetime_args ().size ()); + for (auto &lifetime : args.get_lifetime_args ()) - { - HIR::Lifetime l = lower_lifetime (lifetime); - lifetime_args.push_back (std::move (l)); - } + lifetime_args.emplace_back (lower_lifetime (lifetime)); std::vector<std::unique_ptr<HIR::Type>> type_args; std::vector<HIR::ConstGenericArg> const_args; @@ -681,16 +678,15 @@ ASTLoweringBase::lower_generic_args (AST::GenericArgs &args) { case AST::GenericArg::Kind::Type: { - auto type = ASTLoweringType::translate (arg.get_type ()); - type_args.emplace_back (std::unique_ptr<HIR::Type> (type)); + type_args.emplace_back ( + ASTLoweringType::translate (arg.get_type ())); break; } case AST::GenericArg::Kind::Const: { auto expr = ASTLoweringExpr::translate (arg.get_expression ()); - const_args.emplace_back ( - HIR::ConstGenericArg (std::unique_ptr<HIR::Expr> (expr), - expr->get_locus ())); + const_args.emplace_back (std::unique_ptr<HIR::Expr> (expr), + expr->get_locus ()); break; } default: @@ -881,11 +877,10 @@ ASTLoweringBase::lower_tuple_pattern_multiple ( AST::TuplePatternItemsMultiple &pattern) { std::vector<std::unique_ptr<HIR::Pattern>> patterns; + patterns.reserve (pattern.get_patterns ().size ()); + for (auto &p : pattern.get_patterns ()) - { - HIR::Pattern *translated = ASTLoweringPattern::translate (*p); - patterns.push_back (std::unique_ptr<HIR::Pattern> (translated)); - } + patterns.emplace_back (ASTLoweringPattern::translate (*p)); return std::unique_ptr<HIR::TuplePatternItems> ( new HIR::TuplePatternItemsNoRest (std::move (patterns))); @@ -896,19 +891,15 @@ ASTLoweringBase::lower_tuple_pattern_ranged ( AST::TuplePatternItemsRanged &pattern) { std::vector<std::unique_ptr<HIR::Pattern>> lower_patterns; + lower_patterns.reserve (pattern.get_lower_patterns ().size ()); std::vector<std::unique_ptr<HIR::Pattern>> upper_patterns; + upper_patterns.reserve (pattern.get_upper_patterns ().size ()); for (auto &p : pattern.get_lower_patterns ()) - { - HIR::Pattern *translated = ASTLoweringPattern::translate (*p); - lower_patterns.push_back (std::unique_ptr<HIR::Pattern> (translated)); - } + lower_patterns.emplace_back (ASTLoweringPattern::translate (*p)); for (auto &p : pattern.get_upper_patterns ()) - { - HIR::Pattern *translated = ASTLoweringPattern::translate (*p); - upper_patterns.push_back (std::unique_ptr<HIR::Pattern> (translated)); - } + upper_patterns.emplace_back (ASTLoweringPattern::translate (*p)); return std::unique_ptr<HIR::TuplePatternItems> ( new HIR::TuplePatternItemsHasRest (std::move (lower_patterns), @@ -1009,14 +1000,15 @@ ASTLoweringBase::lower_extern_block (AST::ExternBlock &extern_block) mappings.get_next_localdef_id (crate_num)); std::vector<std::unique_ptr<HIR::ExternalItem>> extern_items; + extern_items.reserve (extern_block.get_extern_items ().size ()); + for (auto &item : extern_block.get_extern_items ()) { if (item->is_marked_for_strip ()) continue; - HIR::ExternalItem *lowered - = ASTLoweringExternItem::translate (item.get (), mapping.get_hirid ()); - extern_items.push_back (std::unique_ptr<HIR::ExternalItem> (lowered)); + extern_items.emplace_back ( + ASTLoweringExternItem::translate (item.get (), mapping.get_hirid ())); } ABI abi = ABI::C; diff --git a/gcc/rust/hir/rust-ast-lower-enumitem.h b/gcc/rust/hir/rust-ast-lower-enumitem.h index 1b9534564251..8e28c08f007e 100644 --- a/gcc/rust/hir/rust-ast-lower-enumitem.h +++ b/gcc/rust/hir/rust-ast-lower-enumitem.h @@ -80,6 +80,8 @@ public: item.get_visibility ().as_string ().c_str ()); std::vector<HIR::TupleField> fields; + fields.reserve (item.get_tuple_fields ().size ()); + for (auto &field : item.get_tuple_fields ()) { HIR::Visibility vis = translate_visibility (field.get_visibility ()); @@ -90,11 +92,8 @@ public: crate_num, field.get_node_id (), mappings.get_next_hir_id (crate_num), mappings.get_next_localdef_id (crate_num)); - HIR::TupleField translated_field (field_mapping, - std::unique_ptr<HIR::Type> (type), - vis, field.get_locus (), - field.get_outer_attrs ()); - fields.push_back (std::move (translated_field)); + fields.emplace_back (field_mapping, std::unique_ptr<HIR::Type> (type), + vis, field.get_locus (), field.get_outer_attrs ()); } translated diff --git a/gcc/rust/hir/rust-ast-lower-expr.cc b/gcc/rust/hir/rust-ast-lower-expr.cc index 4ed51d9f3928..85171b0e886b 100644 --- a/gcc/rust/hir/rust-ast-lower-expr.cc +++ b/gcc/rust/hir/rust-ast-lower-expr.cc @@ -81,11 +81,10 @@ void ASTLoweringExpr::visit (AST::TupleExpr &expr) { std::vector<std::unique_ptr<HIR::Expr>> tuple_elements; + tuple_elements.reserve (expr.get_tuple_elems ().size ()); + for (auto &e : expr.get_tuple_elems ()) - { - HIR::Expr *t = ASTLoweringExpr::translate (*e); - tuple_elements.push_back (std::unique_ptr<HIR::Expr> (t)); - } + tuple_elements.emplace_back (ASTLoweringExpr::translate (*e)); auto crate_num = mappings.get_current_crate (); Analysis::NodeMapping mapping (crate_num, expr.get_node_id (), @@ -221,12 +220,12 @@ ASTLoweringExpr::visit (AST::CallExpr &expr) HIR::Expr *func = ASTLoweringExpr::translate (expr.get_function_expr ()); auto const &in_params = expr.get_params (); + std::vector<std::unique_ptr<HIR::Expr>> params; + params.reserve (in_params.size ()); + for (auto ¶m : in_params) - { - auto trans = ASTLoweringExpr::translate (*param); - params.push_back (std::unique_ptr<HIR::Expr> (trans)); - } + params.emplace_back (ASTLoweringExpr::translate (*param)); auto crate_num = mappings.get_current_crate (); Analysis::NodeMapping mapping ( @@ -248,11 +247,10 @@ ASTLoweringExpr::visit (AST::MethodCallExpr &expr) auto const &in_params = expr.get_params (); std::vector<std::unique_ptr<HIR::Expr>> params; + params.reserve (in_params.size ()); + for (auto ¶m : in_params) - { - auto trans = ASTLoweringExpr::translate (*param); - params.push_back (std::unique_ptr<HIR::Expr> (trans)); - } + params.emplace_back (ASTLoweringExpr::translate (*param)); auto crate_num = mappings.get_current_crate (); Analysis::NodeMapping mapping (crate_num, expr.get_node_id (), @@ -338,11 +336,10 @@ void ASTLoweringExpr::visit (AST::ArrayElemsValues &elems) { std::vector<std::unique_ptr<HIR::Expr>> elements; + elements.reserve (elems.get_values ().size ()); + for (auto &elem : elems.get_values ()) - { - HIR::Expr *translated_elem = ASTLoweringExpr::translate (*elem); - elements.push_back (std::unique_ptr<HIR::Expr> (translated_elem)); - } + elements.emplace_back (ASTLoweringExpr::translate (*elem)); auto crate_num = mappings.get_current_crate (); Analysis::NodeMapping mapping (mappings.get_current_crate (), @@ -569,13 +566,12 @@ ASTLoweringExpr::visit (AST::StructExprStructFields &struct_expr) } auto const &in_fields = struct_expr.get_fields (); + std::vector<std::unique_ptr<HIR::StructExprField>> fields; + fields.reserve (in_fields.size ()); + for (auto &field : in_fields) - { - HIR::StructExprField *translated - = ASTLowerStructExprField::translate (*field); - fields.push_back (std::unique_ptr<HIR::StructExprField> (translated)); - } + fields.emplace_back (ASTLowerStructExprField::translate (*field)); auto crate_num = mappings.get_current_crate (); Analysis::NodeMapping mapping (crate_num, struct_expr.get_node_id (), @@ -814,11 +810,10 @@ ASTLoweringExpr::visit (AST::ClosureExprInner &expr) = ASTLoweringExpr::translate (expr.get_definition_expr ()); std::vector<HIR::ClosureParam> closure_params; + closure_params.reserve (expr.get_params ().size ()); + for (auto ¶m : expr.get_params ()) - { - HIR::ClosureParam p = lower_closure_param (param); - closure_params.push_back (std::move (p)); - } + closure_params.emplace_back (lower_closure_param (param)); auto crate_num = mappings.get_current_crate (); Analysis::NodeMapping mapping (crate_num, expr.get_node_id (), @@ -841,11 +836,10 @@ ASTLoweringExpr::visit (AST::ClosureExprInnerTyped &expr) = ASTLoweringExpr::translate (expr.get_definition_expr ()); std::vector<HIR::ClosureParam> closure_params; + closure_params.reserve (expr.get_params ().size ()); + for (auto ¶m : expr.get_params ()) - { - HIR::ClosureParam p = lower_closure_param (param); - closure_params.push_back (std::move (p)); - } + closure_params.emplace_back (lower_closure_param (param)); auto crate_num = mappings.get_current_crate (); Analysis::NodeMapping mapping (crate_num, expr.get_node_id (), @@ -1009,7 +1003,10 @@ ASTLoweringExpr::visit (AST::LlvmInlineAsm &expr) mappings.get_next_localdef_id (crate_num)); std::vector<LlvmOperand> inputs; + inputs.reserve (expr.get_inputs ().size ()); + std::vector<LlvmOperand> outputs; + outputs.reserve (expr.get_outputs ().size ()); for (auto i : expr.get_inputs ()) { diff --git a/gcc/rust/hir/rust-ast-lower-extern.h b/gcc/rust/hir/rust-ast-lower-extern.h index 3dca1b62666f..1f3ceda66f74 100644 --- a/gcc/rust/hir/rust-ast-lower-extern.h +++ b/gcc/rust/hir/rust-ast-lower-extern.h @@ -109,9 +109,8 @@ public: mappings.get_next_localdef_id ( crate_num)); - function_params.push_back ( - HIR::NamedFunctionParam (mapping, param_name, - std::unique_ptr<HIR::Type> (param_type))); + function_params.emplace_back (mapping, param_name, + std::unique_ptr<HIR::Type> (param_type)); } auto crate_num = mappings.get_current_crate (); diff --git a/gcc/rust/hir/rust-ast-lower-implitem.cc b/gcc/rust/hir/rust-ast-lower-implitem.cc index 5db11cbfa6ab..9b25873af822 100644 --- a/gcc/rust/hir/rust-ast-lower-implitem.cc +++ b/gcc/rust/hir/rust-ast-lower-implitem.cc @@ -115,7 +115,7 @@ ASTLowerImplItem::visit (AST::Function &function) { HIR::WhereClauseItem *i = ASTLowerWhereClauseItem::translate (*item.get ()); - where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i)); + where_clause_items.emplace_back (i); } HIR::WhereClause where_clause (std::move (where_clause_items)); @@ -162,10 +162,9 @@ ASTLowerImplItem::visit (AST::Function &function) mappings.get_next_hir_id (crate_num), UNKNOWN_LOCAL_DEFID); - auto hir_param - = HIR::FunctionParam (mapping, std::move (translated_pattern), - std::move (translated_type), param.get_locus ()); - function_params.push_back (std::move (hir_param)); + function_params.emplace_back (mapping, std::move (translated_pattern), + std::move (translated_type), + param.get_locus ()); } bool terminated = false; @@ -272,10 +271,9 @@ ASTLowerTraitItem::visit (AST::Function &func) mappings.get_next_hir_id (crate_num), UNKNOWN_LOCAL_DEFID); - auto hir_param - = HIR::FunctionParam (mapping, std::move (translated_pattern), - std::move (translated_type), param.get_locus ()); - function_params.push_back (std::move (hir_param)); + function_params.emplace_back (mapping, std::move (translated_pattern), + std::move (translated_type), + param.get_locus ()); } if (func.has_self_param ()) diff --git a/gcc/rust/hir/rust-ast-lower-item.cc b/gcc/rust/hir/rust-ast-lower-item.cc index 4e5a747b2a98..c6ff50721965 100644 --- a/gcc/rust/hir/rust-ast-lower-item.cc +++ b/gcc/rust/hir/rust-ast-lower-item.cc @@ -72,7 +72,7 @@ ASTLoweringItem::visit (AST::Module &module) // The item may be null if it doesn't need to live in the HIR - for // example, macro rules definitions if (transitem) - items.push_back (std::unique_ptr<Item> (transitem)); + items.emplace_back (transitem); } // should be lowered/copied from module.get_in/outer_attrs() @@ -90,12 +90,11 @@ void ASTLoweringItem::visit (AST::TypeAlias &alias) { std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; + where_clause_items.reserve (alias.get_where_clause ().get_items ().size ()); + for (auto &item : alias.get_where_clause ().get_items ()) - { - HIR::WhereClauseItem *i - = ASTLowerWhereClauseItem::translate (*item.get ()); - where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i)); - } + where_clause_items.emplace_back ( + ASTLowerWhereClauseItem::translate (*item.get ())); HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::Visibility vis = translate_visibility (alias.get_visibility ()); @@ -130,17 +129,19 @@ ASTLoweringItem::visit (AST::TupleStruct &struct_decl) } std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; + where_clause_items.reserve ( + struct_decl.get_where_clause ().get_items ().size ()); + for (auto &item : struct_decl.get_where_clause ().get_items ()) - { - HIR::WhereClauseItem *i - = ASTLowerWhereClauseItem::translate (*item.get ()); - where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i)); - } + where_clause_items.emplace_back ( + ASTLowerWhereClauseItem::translate (*item.get ())); HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::Visibility vis = translate_visibility (struct_decl.get_visibility ()); std::vector<HIR::TupleField> fields; + fields.reserve (struct_decl.get_fields ().size ()); + for (AST::TupleField &field : struct_decl.get_fields ()) { if (field.get_field_type ().is_marked_for_strip ()) @@ -155,11 +156,8 @@ ASTLoweringItem::visit (AST::TupleStruct &struct_decl) mappings.get_next_hir_id (crate_num), mappings.get_next_localdef_id (crate_num)); - HIR::TupleField translated_field (mapping, - std::unique_ptr<HIR::Type> (type), vis, - field.get_locus (), - field.get_outer_attrs ()); - fields.push_back (std::move (translated_field)); + fields.emplace_back (mapping, std::unique_ptr<HIR::Type> (type), vis, + field.get_locus (), field.get_outer_attrs ()); } auto crate_num = mappings.get_current_crate (); @@ -185,12 +183,12 @@ ASTLoweringItem::visit (AST::StructStruct &struct_decl) } std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; + where_clause_items.reserve ( + struct_decl.get_where_clause ().get_items ().size ()); + for (auto &item : struct_decl.get_where_clause ().get_items ()) - { - HIR::WhereClauseItem *i - = ASTLowerWhereClauseItem::translate (*item.get ()); - where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i)); - } + where_clause_items.emplace_back ( + ASTLowerWhereClauseItem::translate (*item.get ())); HIR::WhereClause where_clause (std::move (where_clause_items)); @@ -245,25 +243,26 @@ ASTLoweringItem::visit (AST::Enum &enum_decl) } std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; + where_clause_items.reserve ( + enum_decl.get_where_clause ().get_items ().size ()); + for (auto &item : enum_decl.get_where_clause ().get_items ()) - { - HIR::WhereClauseItem *i - = ASTLowerWhereClauseItem::translate (*item.get ()); - where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i)); - } + where_clause_items.emplace_back ( + ASTLowerWhereClauseItem::translate (*item.get ())); HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::Visibility vis = translate_visibility (enum_decl.get_visibility ()); // bool is_unit = enum_decl.is_zero_variant (); std::vector<std::unique_ptr<HIR::EnumItem>> items; + items.reserve (enum_decl.get_variants ().size ()); + for (auto &variant : enum_decl.get_variants ()) { if (variant->is_marked_for_strip ()) continue; - HIR::EnumItem *hir_item = ASTLoweringEnumItem::translate (variant.get ()); - items.push_back (std::unique_ptr<HIR::EnumItem> (hir_item)); + items.emplace_back (ASTLoweringEnumItem::translate (variant.get ())); } auto crate_num = mappings.get_current_crate (); @@ -288,17 +287,16 @@ ASTLoweringItem::visit (AST::Union &union_decl) { std::vector<std::unique_ptr<HIR::GenericParam>> generic_params; if (union_decl.has_generics ()) - { - generic_params = lower_generic_params (union_decl.get_generic_params ()); - } + generic_params = lower_generic_params (union_decl.get_generic_params ()); std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; + where_clause_items.reserve ( + union_decl.get_where_clause ().get_items ().size ()); + for (auto &item : union_decl.get_where_clause ().get_items ()) - { - HIR::WhereClauseItem *i - = ASTLowerWhereClauseItem::translate (*item.get ()); - where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i)); - } + where_clause_items.emplace_back ( + ASTLowerWhereClauseItem::translate (*item.get ())); + HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::Visibility vis = translate_visibility (union_decl.get_visibility ()); @@ -390,12 +388,12 @@ ASTLoweringItem::visit (AST::Function &function) return; std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; + where_clause_items.reserve ( + function.get_where_clause ().get_items ().size ()); + for (auto &item : function.get_where_clause ().get_items ()) - { - HIR::WhereClauseItem *i - = ASTLowerWhereClauseItem::translate (*item.get ()); - where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i)); - } + where_clause_items.emplace_back ( + ASTLowerWhereClauseItem::translate (*item.get ())); HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::FunctionQualifiers qualifiers @@ -418,6 +416,8 @@ ASTLoweringItem::visit (AST::Function &function) : nullptr; std::vector<HIR::FunctionParam> function_params; + function_params.reserve (function.get_function_params ().size ()); + for (auto &p : function.get_function_params ()) { if (p->is_variadic () || p->is_self ()) @@ -434,10 +434,9 @@ ASTLoweringItem::visit (AST::Function &function) mappings.get_next_hir_id (crate_num), UNKNOWN_LOCAL_DEFID); - auto hir_param - = HIR::FunctionParam (mapping, std::move (translated_pattern), - std::move (translated_type), param.get_locus ()); - function_params.push_back (std::move (hir_param)); + function_params.emplace_back (mapping, std::move (translated_pattern), + std::move (translated_type), + param.get_locus ()); } bool terminated = false; @@ -479,11 +478,12 @@ void ASTLoweringItem::visit (AST::InherentImpl &impl_block) { std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; + where_clause_items.reserve ( + impl_block.get_where_clause ().get_items ().size ()); + for (auto &item : impl_block.get_where_clause ().get_items ()) - { - HIR::WhereClauseItem *i = ASTLowerWhereClauseItem::translate (*item); - where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i)); - } + where_clause_items.emplace_back ( + ASTLowerWhereClauseItem::translate (*item)); HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::Visibility vis = translate_visibility (impl_block.get_visibility ()); @@ -540,7 +540,7 @@ ASTLoweringItem::visit (AST::InherentImpl &impl_block) HIR::ImplItem *lowered = ASTLowerImplItem::translate (*impl_item, mapping.get_hirid ()); rust_assert (lowered != nullptr); - impl_items.push_back (std::unique_ptr<HIR::ImplItem> (lowered)); + impl_items.emplace_back (lowered); impl_item_ids.push_back (lowered->get_impl_mappings ().get_hirid ()); } @@ -563,12 +563,12 @@ void ASTLoweringItem::visit (AST::Trait &trait) { std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; + where_clause_items.reserve (trait.get_where_clause ().get_items ().size ()); + for (auto &item : trait.get_where_clause ().get_items ()) - { - HIR::WhereClauseItem *i - = ASTLowerWhereClauseItem::translate (*item.get ()); - where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i)); - } + where_clause_items.emplace_back ( + ASTLowerWhereClauseItem::translate (*item.get ())); + HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::Visibility vis = translate_visibility (trait.get_visibility ()); @@ -589,23 +589,24 @@ ASTLoweringItem::visit (AST::Trait &trait) if (trait.has_type_param_bounds ()) { for (auto &bound : trait.get_type_param_bounds ()) - { - HIR::TypeParamBound *b = lower_bound (*bound); - type_param_bounds.push_back ( - std::unique_ptr<HIR::TypeParamBound> (b)); - } + type_param_bounds.emplace_back (lower_bound (*bound)); } + auto trait_item_size = trait.get_trait_items ().size (); + std::vector<std::unique_ptr<HIR::TraitItem>> trait_items; + trait_items.reserve (trait_item_size); std::vector<HirId> trait_item_ids; + trait_item_ids.reserve (trait_item_size); + for (auto &item : trait.get_trait_items ()) { if (item->is_marked_for_strip ()) continue; HIR::TraitItem *lowered = ASTLowerTraitItem::translate (*item); - trait_items.push_back (std::unique_ptr<HIR::TraitItem> (lowered)); trait_item_ids.push_back (lowered->get_mappings ().get_hirid ()); + trait_items.emplace_back (lowered); } auto crate_num = mappings.get_current_crate (); @@ -636,13 +637,16 @@ ASTLoweringItem::visit (AST::Trait &trait) void ASTLoweringItem::visit (AST::TraitImpl &impl_block) { - std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; bool unsafe = impl_block.is_unsafe (); + + std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; + where_clause_items.reserve ( + impl_block.get_where_clause ().get_items ().size ()); + for (auto &item : impl_block.get_where_clause ().get_items ()) - { - HIR::WhereClauseItem *i = ASTLowerWhereClauseItem::translate (*item); - where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i)); - } + where_clause_items.emplace_back ( + ASTLowerWhereClauseItem::translate (*item)); + HIR::WhereClause where_clause (std::move (where_clause_items)); HIR::Visibility vis = translate_visibility (impl_block.get_visibility ()); @@ -690,8 +694,13 @@ ASTLoweringItem::visit (AST::TraitImpl &impl_block) mappings.get_next_hir_id (crate_num), mappings.get_next_localdef_id (crate_num)); + auto impl_items_size = impl_block.get_impl_items ().size (); + std::vector<std::unique_ptr<HIR::ImplItem>> impl_items; + impl_items.reserve (impl_items_size); std::vector<HirId> impl_item_ids; + impl_item_ids.reserve (impl_items_size); + for (auto &impl_item : impl_block.get_impl_items ()) { if (impl_item->is_marked_for_strip ()) @@ -700,8 +709,8 @@ ASTLoweringItem::visit (AST::TraitImpl &impl_block) HIR::ImplItem *lowered = ASTLowerImplItem::translate (*impl_item, mapping.get_hirid ()); rust_assert (lowered != nullptr); - impl_items.push_back (std::unique_ptr<HIR::ImplItem> (lowered)); impl_item_ids.push_back (lowered->get_impl_mappings ().get_hirid ()); + impl_items.emplace_back (lowered); } BoundPolarity polarity = impl_block.is_exclam () diff --git a/gcc/rust/hir/rust-ast-lower-pattern.cc b/gcc/rust/hir/rust-ast-lower-pattern.cc index 06650e6f9c15..065c5a81b426 100644 --- a/gcc/rust/hir/rust-ast-lower-pattern.cc +++ b/gcc/rust/hir/rust-ast-lower-pattern.cc @@ -94,11 +94,11 @@ ASTLoweringPattern::visit (AST::TupleStructPattern &pattern) = static_cast<AST::TupleStructItemsNoRange &> (items); std::vector<std::unique_ptr<HIR::Pattern>> patterns; + patterns.reserve (items_no_range.get_patterns ().size ()); + for (auto &inner_pattern : items_no_range.get_patterns ()) - { - HIR::Pattern *p = ASTLoweringPattern::translate (*inner_pattern); - patterns.push_back (std::unique_ptr<HIR::Pattern> (p)); - } + patterns.emplace_back ( + ASTLoweringPattern::translate (*inner_pattern)); lowered = new HIR::TupleStructItemsNoRest (std::move (patterns)); } @@ -196,7 +196,7 @@ ASTLoweringPattern::visit (AST::StructPattern &pattern) mappings.insert_node_to_hir (field_node_id, field_id); // add it to the lowered fields list - fields.push_back (std::unique_ptr<HIR::StructPatternField> (f)); + fields.emplace_back (f); } auto crate_num = mappings.get_current_crate (); @@ -331,10 +331,7 @@ ASTLoweringPattern::visit (AST::SlicePattern &pattern) AST::SlicePatternItemsNoRest &ref = static_cast<AST::SlicePatternItemsNoRest &> (pattern.get_items ()); for (auto &p : ref.get_patterns ()) - { - HIR::Pattern *item = ASTLoweringPattern::translate (*p); - items.push_back (std::unique_ptr<HIR::Pattern> (item)); - } + items.emplace_back (ASTLoweringPattern::translate (*p)); } break; case AST::SlicePatternItems::SlicePatternItemType::HAS_REST: @@ -364,12 +361,10 @@ ASTLoweringPattern::visit (AST::AltPattern &pattern) UNKNOWN_LOCAL_DEFID); std::vector<std::unique_ptr<HIR::Pattern>> alts; + alts.reserve (pattern.get_alts ().size ()); for (auto &alt : pattern.get_alts ()) - { - alts.push_back ( - std::unique_ptr<HIR::Pattern> (ASTLoweringPattern::translate (*alt))); - } + alts.emplace_back (ASTLoweringPattern::translate (*alt)); translated = new HIR::AltPattern (mapping, std::move (alts), pattern.get_locus ()); diff --git a/gcc/rust/hir/rust-ast-lower.cc b/gcc/rust/hir/rust-ast-lower.cc index e6e327fe1dac..5b8e7c514371 100644 --- a/gcc/rust/hir/rust-ast-lower.cc +++ b/gcc/rust/hir/rust-ast-lower.cc @@ -80,7 +80,7 @@ ASTLowering::go () { auto translated = ASTLoweringItem::translate (*item); if (translated != nullptr) - items.push_back (std::unique_ptr<HIR::Item> (translated)); + items.emplace_back (translated); } auto &mappings = Analysis::Mappings::get (); @@ -127,7 +127,7 @@ ASTLoweringBlock::visit (AST::BlockExpr &expr) block_did_terminate |= terminated; if (translated_stmt) - block_stmts.push_back (std::unique_ptr<HIR::Stmt> (translated_stmt)); + block_stmts.emplace_back (translated_stmt); } if (expr.has_tail_expr () && block_did_terminate) @@ -231,6 +231,7 @@ ASTLoweringIfLetBlock::desugar_iflet (AST::IfLetExpr &expr, { HIR::Expr *kase_expr; std::vector<std::unique_ptr<HIR::Pattern>> match_arm_patterns; + match_arm_patterns.reserve (expr.get_patterns ().size ()); *branch_value = ASTLoweringExpr::translate (expr.get_value_expr ()); kase_expr = ASTLoweringExpr::translate (expr.get_if_block ()); @@ -241,10 +242,7 @@ ASTLoweringIfLetBlock::desugar_iflet (AST::IfLetExpr &expr, rust_assert (expr.get_patterns ().size () == 1); for (auto &pattern : expr.get_patterns ()) - { - HIR::Pattern *ptrn = ASTLoweringPattern::translate (*pattern); - match_arm_patterns.push_back (std::unique_ptr<HIR::Pattern> (ptrn)); - } + match_arm_patterns.emplace_back (ASTLoweringPattern::translate (*pattern)); // The match arm corresponding to the if let pattern when it matches. HIR::MatchArm arm (std::move (match_arm_patterns), expr.get_locus (), nullptr, @@ -255,9 +253,8 @@ ASTLoweringIfLetBlock::desugar_iflet (AST::IfLetExpr &expr, mappings.get_next_hir_id (crate_num), UNKNOWN_LOCAL_DEFID); - HIR::MatchCase kase (std::move (mapping), std::move (arm), - std::unique_ptr<HIR::Expr> (kase_expr)); - match_arms.push_back (std::move (kase)); + match_arms.emplace_back (std::move (mapping), std::move (arm), + std::unique_ptr<HIR::Expr> (kase_expr)); // The default match arm when the if let pattern does not match std::vector<std::unique_ptr<HIR::Pattern>> match_arm_patterns_wildcard; @@ -265,19 +262,14 @@ ASTLoweringIfLetBlock::desugar_iflet (AST::IfLetExpr &expr, mappings.get_next_hir_id (crate_num), UNKNOWN_LOCAL_DEFID); - std::unique_ptr<HIR::WildcardPattern> wc - = std::unique_ptr<HIR::WildcardPattern> ( - new HIR::WildcardPattern (mapping_default, expr.get_locus ())); - - match_arm_patterns_wildcard.push_back (std::move (wc)); + match_arm_patterns_wildcard.emplace_back ( + new HIR::WildcardPattern (mapping_default, expr.get_locus ())); HIR::MatchArm arm_default (std::move (match_arm_patterns_wildcard), expr.get_locus (), nullptr, {}); - HIR::MatchCase kase_else (std::move (mapping_default), - std::move (arm_default), - std::unique_ptr<HIR::Expr> (kase_else_expr)); - match_arms.push_back (std::move (kase_else)); + match_arms.emplace_back (std::move (mapping_default), std::move (arm_default), + std::unique_ptr<HIR::Expr> (kase_else_expr)); } void @@ -442,11 +434,12 @@ ASTLoweringExprWithBlock::visit (AST::MatchExpr &expr) } std::vector<std::unique_ptr<HIR::Pattern>> match_arm_patterns; + match_arm_patterns.reserve ( + match_case.get_arm ().get_patterns ().size ()); + for (auto &pattern : match_case.get_arm ().get_patterns ()) - { - HIR::Pattern *ptrn = ASTLoweringPattern::translate (*pattern); - match_arm_patterns.push_back (std::unique_ptr<HIR::Pattern> (ptrn)); - } + match_arm_patterns.emplace_back ( + ASTLoweringPattern::translate (*pattern)); HIR::MatchArm arm (std::move (match_arm_patterns), expr.get_locus (), std::unique_ptr<HIR::Expr> (kase_guard_expr), @@ -457,9 +450,8 @@ ASTLoweringExprWithBlock::visit (AST::MatchExpr &expr) mappings.get_next_hir_id (crate_num), UNKNOWN_LOCAL_DEFID); - HIR::MatchCase kase (std::move (mapping), std::move (arm), - std::unique_ptr<HIR::Expr> (kase_expr)); - match_arms.push_back (std::move (kase)); + match_arms.emplace_back (std::move (mapping), std::move (arm), + std::unique_ptr<HIR::Expr> (kase_expr)); } auto crate_num = mappings.get_current_crate (); @@ -490,8 +482,11 @@ ASTLowerPathInExpression::visit (AST::PathInExpression &expr) return; } - std::vector<HIR::PathExprSegment> path_segments; auto &segments = expr.get_segments (); + + std::vector<HIR::PathExprSegment> path_segments; + path_segments.reserve (segments.size ()); + for (auto &s : segments) { path_segments.push_back (lower_path_expr_seg ((s))); @@ -531,8 +526,11 @@ ASTLowerQualPathInExpression::visit (AST::QualifiedPathInExpression &expr) HIR::QualifiedPathType qual_path_type = lower_qual_path_type (expr.get_qualified_path_type ()); - std::vector<HIR::PathExprSegment> path_segments; auto &segments = expr.get_segments (); + + std::vector<HIR::PathExprSegment> path_segments; + path_segments.reserve (segments.size ()); + for (auto &s : segments) { path_segments.push_back (lower_path_expr_seg ((s))); diff --git a/gcc/rust/hir/tree/rust-hir.cc b/gcc/rust/hir/tree/rust-hir.cc index 5412e072aded..39c0e2e1c292 100644 --- a/gcc/rust/hir/tree/rust-hir.cc +++ b/gcc/rust/hir/tree/rust-hir.cc @@ -2239,8 +2239,8 @@ PathPattern::convert_to_simple_path (bool with_opening_scope_resolution) const // create segment and add to vector std::string segment_str = segment.as_string (); - simple_segments.push_back ( - AST::SimplePathSegment (std::move (segment_str), segment.get_locus ())); + simple_segments.emplace_back (std::move (segment_str), + segment.get_locus ()); } // kind of a HACK to get locus depending on opening scope resolution @@ -2281,9 +2281,8 @@ TypePath::as_simple_path () const // create segment and add to vector std::string segment_str = segment->as_string (); - simple_segments.push_back ( - AST::SimplePathSegment (std::move (segment_str), - segment->get_locus ())); + simple_segments.emplace_back (std::move (segment_str), + segment->get_locus ()); } return AST::SimplePath (std::move (simple_segments), diff --git a/gcc/rust/metadata/rust-export-metadata.cc b/gcc/rust/metadata/rust-export-metadata.cc index 1829a85d2e47..4dfc28036c35 100644 --- a/gcc/rust/metadata/rust-export-metadata.cc +++ b/gcc/rust/metadata/rust-export-metadata.cc @@ -91,8 +91,8 @@ ExportContext::emit_function (const HIR::Function &fn) AST::Function &function = static_cast<AST::Function &> (vis_item); std::vector<std::unique_ptr<AST::ExternalItem>> external_items; - external_items.push_back (std::unique_ptr<AST::ExternalItem> ( - static_cast<AST::ExternalItem *> (&function))); + external_items.emplace_back ( + static_cast<AST::ExternalItem *> (&function)); AST::ExternBlock extern_block (get_string_from_abi (Rust::ABI::RUST), std::move (external_items), diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 14bccbde035f..5d7c5309fb0d 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -1716,10 +1716,9 @@ Parser<ManagedTokenSource>::parse_decl_macro_def (AST::Visibility vis, return nullptr; } - AST::MacroRule macro_rule - = AST::MacroRule (std::move (matcher), std::move (transcriber), locus); std::vector<AST::MacroRule> macro_rules; - macro_rules.push_back (macro_rule); + macro_rules.emplace_back (std::move (matcher), std::move (transcriber), + locus); return std::unique_ptr<AST::MacroRulesDefinition> ( AST::MacroRulesDefinition::decl_macro (std::move (rule_name), @@ -3316,8 +3315,8 @@ Parser<ManagedTokenSource>::parse_lifetime_params () break; } - lifetime_params.push_back (std::unique_ptr<AST::LifetimeParam> ( - new AST::LifetimeParam (std::move (lifetime_param.value ())))); + lifetime_params.emplace_back ( + new AST::LifetimeParam (std::move (lifetime_param.value ()))); if (lexer.peek_token ()->get_id () != COMMA) break; @@ -3356,8 +3355,8 @@ Parser<ManagedTokenSource>::parse_lifetime_params (EndTokenPred is_end_token) return {}; } - lifetime_params.push_back (std::unique_ptr<AST::LifetimeParam> ( - new AST::LifetimeParam (std::move (lifetime_param)))); + lifetime_params.emplace_back ( + new AST::LifetimeParam (std::move (lifetime_param))); if (lexer.peek_token ()->get_id () != COMMA) break; diff --git a/gcc/rust/resolve/rust-ast-resolve-item.cc b/gcc/rust/resolve/rust-ast-resolve-item.cc index 1d5ebed99696..99534d50ebcc 100644 --- a/gcc/rust/resolve/rust-ast-resolve-item.cc +++ b/gcc/rust/resolve/rust-ast-resolve-item.cc @@ -105,8 +105,8 @@ ResolveTraitItems::visit (AST::Function &function) { // here we implicitly make self have a type path of Self std::vector<std::unique_ptr<AST::TypePathSegment>> segments; - segments.push_back (std::unique_ptr<AST::TypePathSegment> ( - new AST::TypePathSegment ("Self", false, param.get_locus ()))); + segments.emplace_back ( + new AST::TypePathSegment ("Self", false, param.get_locus ())); AST::TypePath self_type_path (std::move (segments), param.get_locus ()); @@ -512,8 +512,8 @@ ResolveItem::visit (AST::Function &function) { // here we implicitly make self have a type path of Self std::vector<std::unique_ptr<AST::TypePathSegment>> segments; - segments.push_back (std::unique_ptr<AST::TypePathSegment> ( - new AST::TypePathSegment ("Self", false, self_param.get_locus ()))); + segments.emplace_back ( + new AST::TypePathSegment ("Self", false, self_param.get_locus ())); AST::TypePath self_type_path (std::move (segments), self_param.get_locus ()); @@ -906,9 +906,8 @@ flatten_list (const AST::UseTreeList &list, std::vector<Import> &imports) = AST::SimplePath ({}, prefix.has_opening_scope_resolution (), prefix.get_locus ()); for (auto &seg : prefix.get_segments ()) - prefix_copy.get_segments ().push_back ( - AST::SimplePathSegment (seg.get_segment_name (), - seg.get_locus ())); + prefix_copy.get_segments ().emplace_back (seg.get_segment_name (), + seg.get_locus ()); import->add_prefix (std::move (prefix_copy)); } diff --git a/gcc/rust/resolve/rust-ast-resolve-pattern.cc b/gcc/rust/resolve/rust-ast-resolve-pattern.cc index 3b80f9f0508f..cc633a9cc692 100644 --- a/gcc/rust/resolve/rust-ast-resolve-pattern.cc +++ b/gcc/rust/resolve/rust-ast-resolve-pattern.cc @@ -200,8 +200,7 @@ PatternDeclaration::visit (AST::AltPattern &pattern) // push a new set of 'Or' bindings to the stack. Accounts for the // alternatives. e.g. in `p_0 | p_1`, bindings to the same identifier between // p_0 and p_1 shouldn't cause an error. - bindings_with_ctx.push_back ( - PatternBinding (PatternBoundCtx::Or, std::set<Identifier> ())); + bindings_with_ctx.emplace_back (PatternBoundCtx::Or, std::set<Identifier> ()); // This is a hack to avoid creating a separate visitor class for the // consistency checks. We empty out the binding_info_map before each iteration @@ -219,8 +218,8 @@ PatternDeclaration::visit (AST::AltPattern &pattern) // push a new `Product` context to correctly reject multiple bindings // within this single alt. - bindings_with_ctx.push_back ( - PatternBinding (PatternBoundCtx::Product, std::set<Identifier> ())); + bindings_with_ctx.emplace_back (PatternBoundCtx::Product, + std::set<Identifier> ()); alt->accept_vis (*this); diff --git a/gcc/rust/typecheck/rust-autoderef.cc b/gcc/rust/typecheck/rust-autoderef.cc index 10a59bd24f74..8cc2b7bdfa4f 100644 --- a/gcc/rust/typecheck/rust-autoderef.cc +++ b/gcc/rust/typecheck/rust-autoderef.cc @@ -425,8 +425,7 @@ AutoderefCycle::try_autoderefed (TyTy::BaseType *r) TyTy::ReferenceType *r1 = new TyTy::ReferenceType (r->get_ref (), TyTy::TyVar (r->get_ref ()), Mutability::Imm); - adjustments.push_back ( - Adjustment (Adjustment::AdjustmentType::IMM_REF, r, r1)); + adjustments.emplace_back (Adjustment::AdjustmentType::IMM_REF, r, r1); if (select (*r1)) return true; @@ -436,8 +435,7 @@ AutoderefCycle::try_autoderefed (TyTy::BaseType *r) TyTy::ReferenceType *r2 = new TyTy::ReferenceType (r->get_ref (), TyTy::TyVar (r->get_ref ()), Mutability::Mut); - adjustments.push_back ( - Adjustment (Adjustment::AdjustmentType::MUT_REF, r, r2)); + adjustments.emplace_back (Adjustment::AdjustmentType::MUT_REF, r, r2); if (select (*r2)) return true; diff --git a/gcc/rust/typecheck/rust-coercion.cc b/gcc/rust/typecheck/rust-coercion.cc index fd12839c99bb..2117c2b24eee 100644 --- a/gcc/rust/typecheck/rust-coercion.cc +++ b/gcc/rust/typecheck/rust-coercion.cc @@ -356,8 +356,8 @@ TypeCoercionRules::coerce_unsized (TyTy::BaseType *source, needs_reborrow = true; expected_mutability = to_mutbl; - adjustments.push_back ( - Adjustment (Adjustment::AdjustmentType::INDIRECTION, source_ref, ty_a)); + adjustments.emplace_back (Adjustment::AdjustmentType::INDIRECTION, + source_ref, ty_a); } else if (source_is_ref && target_is_ptr) { @@ -381,8 +381,8 @@ TypeCoercionRules::coerce_unsized (TyTy::BaseType *source, needs_reborrow = true; expected_mutability = to_mutbl; - adjustments.push_back ( - Adjustment (Adjustment::AdjustmentType::INDIRECTION, source_ref, ty_a)); + adjustments.emplace_back (Adjustment::AdjustmentType::INDIRECTION, + source_ref, ty_a); } // FIXME @@ -411,7 +411,7 @@ TypeCoercionRules::coerce_unsized (TyTy::BaseType *source, // result->set_ref (a->get_ref ()); // append a dyn coercion adjustment - adjustments.push_back (Adjustment (Adjustment::UNSIZE, a, result)); + adjustments.emplace_back (Adjustment::UNSIZE, a, result); // reborrow if needed if (needs_reborrow) @@ -424,7 +424,7 @@ TypeCoercionRules::coerce_unsized (TyTy::BaseType *source, Adjustment::AdjustmentType borrow_type = expected_mutability == Mutability::Imm ? Adjustment::IMM_REF : Adjustment::MUT_REF; - adjustments.push_back (Adjustment (borrow_type, result, reborrow)); + adjustments.emplace_back (borrow_type, result, reborrow); result = reborrow; } diff --git a/gcc/rust/typecheck/rust-hir-dot-operator.cc b/gcc/rust/typecheck/rust-hir-dot-operator.cc index 7b7944c508dd..f0db7ac81029 100644 --- a/gcc/rust/typecheck/rust-hir-dot-operator.cc +++ b/gcc/rust/typecheck/rust-hir-dot-operator.cc @@ -175,7 +175,7 @@ MethodResolver::assemble_inherent_impl_candidates ( return true; } - inherent_impl_fns.push_back ({func, impl, fnty}); + inherent_impl_fns.emplace_back (func, impl, fnty); return true; }); @@ -259,7 +259,7 @@ MethodResolver::assemble_trait_impl_candidates ( continue; } - impl_candidates.push_back ({func, impl, fnty}); + impl_candidates.emplace_back (func, impl, fnty); return true; } @@ -285,8 +285,7 @@ MethodResolver::assemble_trait_impl_candidates ( rust_assert (ty->get_kind () == TyTy::TypeKind::FNDEF); TyTy::FnType *fnty = static_cast<TyTy::FnType *> (ty); - trait_item_candidate candidate{func, trait, fnty, trait_ref, item_ref}; - trait_candidates.push_back (candidate); + trait_candidates.emplace_back (func, trait, fnty, trait_ref, item_ref); return true; }); @@ -474,10 +473,7 @@ MethodResolver::get_predicate_items ( { TyTy::FnType *fnty = static_cast<TyTy::FnType *> (ty); if (fnty->is_method ()) - { - predicate_candidate candidate{lookup, fnty}; - predicate_items.push_back (candidate); - } + predicate_items.emplace_back (lookup, fnty); } } diff --git a/gcc/rust/typecheck/rust-hir-dot-operator.h b/gcc/rust/typecheck/rust-hir-dot-operator.h index cc40472e0faf..d2f11d1acdf7 100644 --- a/gcc/rust/typecheck/rust-hir-dot-operator.h +++ b/gcc/rust/typecheck/rust-hir-dot-operator.h @@ -49,6 +49,10 @@ class MethodResolver : private TypeCheckBase, protected AutoderefCycle public: struct predicate_candidate { + predicate_candidate (TyTy::TypeBoundPredicateItem lookup, + TyTy::FnType *fntype) + : lookup (lookup), fntype (fntype) + {} TyTy::TypeBoundPredicateItem lookup; TyTy::FnType *fntype; }; @@ -67,6 +71,11 @@ public: struct impl_item_candidate { + impl_item_candidate (HIR::Function *item, HIR::ImplBlock *impl_block, + TyTy::FnType *ty) + : item (item), impl_block (impl_block), ty (ty) + {} + HIR::Function *item; HIR::ImplBlock *impl_block; TyTy::FnType *ty; @@ -74,6 +83,13 @@ public: struct trait_item_candidate { + trait_item_candidate (const HIR::TraitItemFunc *item, + const HIR::Trait *trait, TyTy::FnType *ty, + const TraitReference *reference, + const TraitItemReference *item_ref) + : item (item), trait (trait), ty (ty), reference (reference), + item_ref (item_ref) + {} const HIR::TraitItemFunc *item; const HIR::Trait *trait; TyTy::FnType *ty; diff --git a/gcc/rust/typecheck/rust-hir-path-probe.cc b/gcc/rust/typecheck/rust-hir-path-probe.cc index c02702fbc250..6ed6e25858f9 100644 --- a/gcc/rust/typecheck/rust-hir-path-probe.cc +++ b/gcc/rust/typecheck/rust-hir-path-probe.cc @@ -425,10 +425,10 @@ PathProbeType::union_bounds ( } std::vector<std::pair<const TraitReference *, HIR::ImplBlock *>> union_set; + for (auto it = mapper.begin (); it != mapper.end (); it++) - { - union_set.push_back ({it->second.first, it->second.second}); - } + union_set.emplace_back (it->second.first, it->second.second); + return union_set; } diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.cc b/gcc/rust/typecheck/rust-hir-trait-resolve.cc index 0fd0147b45f3..b8dd0ad782af 100644 --- a/gcc/rust/typecheck/rust-hir-trait-resolve.cc +++ b/gcc/rust/typecheck/rust-hir-trait-resolve.cc @@ -224,8 +224,7 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference) apply_sized); context->insert_type (generic_param->get_mappings (), param_type); - substitutions.push_back ( - TyTy::SubstitutionParamMapping (typaram, param_type)); + substitutions.emplace_back (typaram, param_type); if (is_self) { @@ -247,6 +246,8 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference) // copy the substitition mappings std::vector<TyTy::SubstitutionParamMapping> self_subst_copy; + self_subst_copy.reserve (substitutions.size ()); + for (auto &sub : substitutions) self_subst_copy.push_back (sub.clone ()); @@ -291,6 +292,8 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference) { // make a copy of the substs std::vector<TyTy::SubstitutionParamMapping> item_subst; + item_subst.reserve (substitutions.size ()); + for (auto &sub : substitutions) item_subst.push_back (sub.clone ()); @@ -553,9 +556,9 @@ AssociatedImplTrait::setup_associated_types ( generic_param->get_mappings ().get_hirid (), &l); if (ok && l->get_kind () == TyTy::TypeKind::PARAM) { - substitutions.push_back (TyTy::SubstitutionParamMapping ( - static_cast<HIR::TypeParam &> (*generic_param), - static_cast<TyTy::ParamType *> (l))); + substitutions.emplace_back (static_cast<HIR::TypeParam &> ( + *generic_param), + static_cast<TyTy::ParamType *> (l)); } } break; @@ -579,14 +582,13 @@ AssociatedImplTrait::setup_associated_types ( if (p.needs_substitution () && infer) { TyTy::TyVar infer_var = TyTy::TyVar::get_implicit_infer_var (locus); - subst_args.push_back ( - TyTy::SubstitutionArg (&p, infer_var.get_tyty ())); + subst_args.emplace_back (&p, infer_var.get_tyty ()); } else { auto param = p.get_param_ty (); auto resolved = param->destructure (); - subst_args.push_back (TyTy::SubstitutionArg (&p, resolved)); + subst_args.emplace_back (&p, resolved); param_mappings[param->get_symbol ()] = resolved->get_ref (); } } diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc index 438200ba4214..33653f5a6512 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc @@ -160,7 +160,7 @@ TypeCheckExpr::visit (HIR::TupleExpr &expr) for (auto &elem : expr.get_tuple_elems ()) { auto field_ty = TypeCheckExpr::Resolve (*elem); - fields.push_back (TyTy::TyVar (field_ty->get_ref ())); + fields.emplace_back (field_ty->get_ref ()); } infered = new TyTy::TupleType (expr.get_mappings ().get_hirid (), expr.get_locus (), fields); @@ -730,7 +730,7 @@ TypeCheckExpr::visit (HIR::RangeFromToExpr &expr) // substitute it in std::vector<TyTy::SubstitutionArg> subst_mappings; const TyTy::SubstitutionParamMapping *param_ref = &adt->get_substs ().at (0); - subst_mappings.push_back (TyTy::SubstitutionArg (param_ref, unified)); + subst_mappings.emplace_back (param_ref, unified); TyTy::SubstitutionArgumentMappings subst ( subst_mappings, {}, adt->get_substitution_arguments ().get_regions (), @@ -774,7 +774,7 @@ TypeCheckExpr::visit (HIR::RangeFromExpr &expr) // substitute it in std::vector<TyTy::SubstitutionArg> subst_mappings; const TyTy::SubstitutionParamMapping *param_ref = &adt->get_substs ().at (0); - subst_mappings.push_back (TyTy::SubstitutionArg (param_ref, from_ty)); + subst_mappings.emplace_back (param_ref, from_ty); TyTy::SubstitutionArgumentMappings subst ( subst_mappings, {}, adt->get_substitution_arguments ().get_regions (), @@ -818,7 +818,7 @@ TypeCheckExpr::visit (HIR::RangeToExpr &expr) // substitute it in std::vector<TyTy::SubstitutionArg> subst_mappings; const TyTy::SubstitutionParamMapping *param_ref = &adt->get_substs ().at (0); - subst_mappings.push_back (TyTy::SubstitutionArg (param_ref, from_ty)); + subst_mappings.emplace_back (param_ref, from_ty); TyTy::SubstitutionArgumentMappings subst ( subst_mappings, {}, adt->get_substitution_arguments ().get_regions (), @@ -991,7 +991,7 @@ TypeCheckExpr::visit (HIR::RangeFromToInclExpr &expr) // substitute it in std::vector<TyTy::SubstitutionArg> subst_mappings; const TyTy::SubstitutionParamMapping *param_ref = &adt->get_substs ().at (0); - subst_mappings.push_back (TyTy::SubstitutionArg (param_ref, unified)); + subst_mappings.emplace_back (param_ref, unified); TyTy::SubstitutionArgumentMappings subst ( subst_mappings, {}, adt->get_substitution_arguments ().get_regions (), @@ -1888,7 +1888,7 @@ TypeCheckExpr::visit (HIR::ClosureExpr &expr) // auto resolve because the hir id's match , expr.get_locus ()); - args.get_type_args ().push_back (std::unique_ptr<HIR::Type> (implicit_tuple)); + args.get_type_args ().emplace_back (implicit_tuple); // apply the arguments predicate.apply_generic_arguments (&args, false, false); @@ -1974,13 +1974,13 @@ TypeCheckExpr::resolve_operator_overload ( std::vector<TyTy::SubstitutionArg> mappings; auto &self_param_mapping = trait_subst[0]; - mappings.push_back (TyTy::SubstitutionArg (&self_param_mapping, lhs)); + mappings.emplace_back (&self_param_mapping, lhs); if (rhs != nullptr) { rust_assert (trait_subst.size () == 2); auto &rhs_param_mapping = trait_subst[1]; - mappings.push_back (TyTy::SubstitutionArg (&rhs_param_mapping, lhs)); + mappings.emplace_back (&rhs_param_mapping, lhs); } std::map<std::string, TyTy::BaseType *> binding_args; @@ -2312,7 +2312,7 @@ TypeCheckExpr::resolve_fn_trait_call (HIR::CallExpr &expr, for (auto &arg : expr.get_arguments ()) { TyTy::BaseType *a = TypeCheckExpr::Resolve (*arg); - call_args.push_back (TyTy::TyVar (a->get_ref ())); + call_args.emplace_back (a->get_ref ()); } // crate implicit tuple @@ -2325,9 +2325,8 @@ TypeCheckExpr::resolve_fn_trait_call (HIR::CallExpr &expr, context->insert_implicit_type (implicit_arg_id, tuple); std::vector<TyTy::Argument> args; - TyTy::Argument a (mapping, tuple, - expr.get_locus () /*FIXME is there a better location*/); - args.push_back (std::move (a)); + args.emplace_back (mapping, tuple, + expr.get_locus () /*FIXME is there a better location*/); TyTy::BaseType *function_ret_tyty = TyTy::TypeCheckMethodCallExpr::go (fn, expr.get_mappings (), args, diff --git a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc index c8544a1a9c27..077a228e52b1 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc @@ -121,7 +121,7 @@ TypeCheckTopLevelExternItem::visit (HIR::ExternalFunctionItem &function) UNDEF_LOCATION, false, Mutability::Imm, std::unique_ptr<HIR::Pattern> (nullptr))); - params.push_back (TyTy::FnParam (std::move (param_pattern), param_tyty)); + params.emplace_back (std::move (param_pattern), param_tyty); context->insert_type (param.get_mappings (), param_tyty); @@ -324,7 +324,7 @@ TypeCheckImplItem::visit (HIR::Function &function) } context->insert_type (self_param.get_mappings (), self_type); - params.push_back (TyTy::FnParam (std::move (self_pattern), self_type)); + params.emplace_back (std::move (self_pattern), self_type); } for (auto ¶m : function.get_function_params ()) @@ -335,8 +335,8 @@ TypeCheckImplItem::visit (HIR::Function &function) context->insert_type (param.get_mappings (), param_tyty); TypeCheckPattern::Resolve (param.get_param_name (), param_tyty); - params.push_back ( - TyTy::FnParam (param.get_param_name ().clone_pattern (), param_tyty)); + params.emplace_back (param.get_param_name ().clone_pattern (), + param_tyty); } auto &nr_ctx diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.cc b/gcc/rust/typecheck/rust-hir-type-check-item.cc index 3ba607bc5ab4..4987c88ab706 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-item.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-item.cc @@ -115,13 +115,13 @@ TypeCheckItem::ResolveImplBlockSelfWithInference ( if (p.needs_substitution ()) { TyTy::TyVar infer_var = TyTy::TyVar::get_implicit_infer_var (locus); - args.push_back (TyTy::SubstitutionArg (&p, infer_var.get_tyty ())); + args.emplace_back (&p, infer_var.get_tyty ()); } else { auto param = p.get_param_ty (); auto resolved = param->destructure (); - args.push_back (TyTy::SubstitutionArg (&p, resolved)); + args.emplace_back (&p, resolved); } } @@ -554,8 +554,8 @@ TypeCheckItem::visit (HIR::Function &function) auto param_tyty = TypeCheckType::Resolve (param.get_type ()); context->insert_type (param.get_mappings (), param_tyty); TypeCheckPattern::Resolve (param.get_param_name (), param_tyty); - params.push_back ( - TyTy::FnParam (param.get_param_name ().clone_pattern (), param_tyty)); + params.emplace_back (param.get_param_name ().clone_pattern (), + param_tyty); } auto &nr_ctx diff --git a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc index aba760ae7b65..0930c2233e63 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc @@ -486,7 +486,7 @@ TypeCheckPattern::visit (HIR::TuplePattern &pattern) TyTy::BaseType *par_type = par.get_field (i); TyTy::BaseType *elem = TypeCheckPattern::Resolve (*p, par_type); - pattern_elems.push_back (TyTy::TyVar (elem->get_ref ())); + pattern_elems.emplace_back (elem->get_ref ()); } infered = new TyTy::TupleType (pattern.get_mappings ().get_hirid (), pattern.get_locus (), pattern_elems); @@ -519,7 +519,7 @@ TypeCheckPattern::visit (HIR::TuplePattern &pattern) TyTy::BaseType *par_type = par.get_field (i); TyTy::BaseType *elem = TypeCheckPattern::Resolve (*p, par_type); - pattern_elems.push_back (TyTy::TyVar (elem->get_ref ())); + pattern_elems.emplace_back (elem->get_ref ()); } // Pad pattern_elems until needing to resolve upper patterns @@ -527,7 +527,7 @@ TypeCheckPattern::visit (HIR::TuplePattern &pattern) for (size_t i = lower.size (); i < rest_end; i++) { TyTy::BaseType *par_type = par.get_field (i); - pattern_elems.push_back (TyTy::TyVar (par_type->get_ref ())); + pattern_elems.emplace_back (par_type->get_ref ()); } // Resolve upper patterns @@ -537,7 +537,7 @@ TypeCheckPattern::visit (HIR::TuplePattern &pattern) TyTy::BaseType *par_type = par.get_field (rest_end + i); TyTy::BaseType *elem = TypeCheckPattern::Resolve (*p, par_type); - pattern_elems.push_back (TyTy::TyVar (elem->get_ref ())); + pattern_elems.emplace_back (elem->get_ref ()); } infered = new TyTy::TupleType (pattern.get_mappings ().get_hirid (), diff --git a/gcc/rust/typecheck/rust-hir-type-check-struct.cc b/gcc/rust/typecheck/rust-hir-type-check-struct.cc index 4ef83482a536..eb442791d36a 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-struct.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-struct.cc @@ -214,8 +214,7 @@ TypeCheckStructExpr::resolve (HIR::StructExprStructFields &struct_expr) rust_assert (ok); adtFieldIndexToField[field_index] = implicit_field; - struct_expr.get_fields ().push_back ( - std::unique_ptr<HIR::StructExprField> (implicit_field)); + struct_expr.get_fields ().emplace_back (implicit_field); } } } @@ -245,11 +244,11 @@ TypeCheckStructExpr::resolve (HIR::StructExprStructFields &struct_expr) field.release (); std::vector<std::unique_ptr<HIR::StructExprField> > ordered_fields; + ordered_fields.reserve (adtFieldIndexToField.size ()); + for (size_t i = 0; i < adtFieldIndexToField.size (); i++) - { - ordered_fields.push_back ( - std::unique_ptr<HIR::StructExprField> (adtFieldIndexToField[i])); - } + ordered_fields.emplace_back (adtFieldIndexToField[i]); + struct_expr.set_fields_as_owner (std::move (ordered_fields)); } diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc b/gcc/rust/typecheck/rust-hir-type-check-type.cc index 78037bdbd183..4a6c703d764b 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-type.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc @@ -97,10 +97,12 @@ TypeCheckType::visit (HIR::BareFunctionType &fntype) } std::vector<TyTy::TyVar> params; + params.reserve (fntype.get_function_params ().size ()); + for (auto ¶m : fntype.get_function_params ()) { TyTy::BaseType *ptype = TypeCheckType::Resolve (param.get_type ()); - params.push_back (TyTy::TyVar (ptype->get_ref ())); + params.emplace_back (ptype->get_ref ()); } translated = new TyTy::FnPtr (fntype.get_mappings ().get_hirid (), @@ -118,10 +120,12 @@ TypeCheckType::visit (HIR::TupleType &tuple) } std::vector<TyTy::TyVar> fields; + fields.reserve (tuple.get_elems ().size ()); + for (auto &elem : tuple.get_elems ()) { auto field_ty = TypeCheckType::Resolve (*elem); - fields.push_back (TyTy::TyVar (field_ty->get_ref ())); + fields.emplace_back (field_ty->get_ref ()); } translated = new TyTy::TupleType (tuple.get_mappings ().get_hirid (), diff --git a/gcc/rust/typecheck/rust-hir-type-check.cc b/gcc/rust/typecheck/rust-hir-type-check.cc index aba4ab55c6c4..64f4314e3151 100644 --- a/gcc/rust/typecheck/rust-hir-type-check.cc +++ b/gcc/rust/typecheck/rust-hir-type-check.cc @@ -260,7 +260,7 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const } context->insert_type (self_param.get_mappings (), self_type); - params.push_back (TyTy::FnParam (std::move (self_pattern), self_type)); + params.emplace_back (std::move (self_pattern), self_type); } for (auto ¶m : function.get_function_params ()) @@ -270,8 +270,8 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const context->insert_type (param.get_mappings (), param_tyty); TypeCheckPattern::Resolve (param.get_param_name (), param_tyty); // FIXME: Should we take the name ? Use a shared pointer instead ? - params.push_back ( - TyTy::FnParam (param.get_param_name ().clone_pattern (), param_tyty)); + params.emplace_back (param.get_param_name ().clone_pattern (), + param_tyty); } auto &nr_ctx diff --git a/gcc/rust/typecheck/rust-typecheck-context.cc b/gcc/rust/typecheck/rust-typecheck-context.cc index c74a92075d59..3419ef643b1b 100644 --- a/gcc/rust/typecheck/rust-typecheck-context.cc +++ b/gcc/rust/typecheck/rust-typecheck-context.cc @@ -154,7 +154,7 @@ void TypeCheckContext::push_return_type (TypeCheckContextItem item, TyTy::BaseType *return_type) { - return_type_stack.push_back ({std::move (item), return_type}); + return_type_stack.emplace_back (std::move (item), return_type); } void @@ -310,7 +310,7 @@ TypeCheckContext::insert_associated_impl_mapping (HirId trait_id, associated_traits_to_impls[trait_id] = {}; } - associated_traits_to_impls[trait_id].push_back ({impl_type, impl_id}); + associated_traits_to_impls[trait_id].emplace_back (impl_type, impl_id); } bool diff --git a/gcc/rust/typecheck/rust-tyty-bounds.cc b/gcc/rust/typecheck/rust-tyty-bounds.cc index 6cf9b04b9c66..91de35976b66 100644 --- a/gcc/rust/typecheck/rust-tyty-bounds.cc +++ b/gcc/rust/typecheck/rust-tyty-bounds.cc @@ -90,7 +90,7 @@ TypeBoundsProbe::process_impl_block ( return true; } - possible_trait_paths.push_back ({&impl->get_trait_ref (), impl}); + possible_trait_paths.emplace_back (&impl->get_trait_ref (), impl); return true; } @@ -110,7 +110,7 @@ TypeBoundsProbe::scan () TraitReference *trait_ref = TraitResolver::Resolve (*trait_path); if (!trait_ref->is_error ()) - trait_references.push_back ({trait_ref, path.second}); + trait_references.emplace_back (trait_ref, path.second); } // marker traits... @@ -185,7 +185,7 @@ TypeBoundsProbe::add_trait_bound (HIR::Trait *trait) { auto trait_ref = TraitResolver::Resolve (*trait); - trait_references.push_back ({trait_ref, mappings.lookup_builtin_marker ()}); + trait_references.emplace_back (trait_ref, mappings.lookup_builtin_marker ()); } void @@ -306,11 +306,9 @@ TypeCheckBase::get_predicate_from_bound ( std::vector<HIR::GenericArgsBinding> bindings; location_t output_locus = fn.get_return_type ().get_locus (); - HIR::GenericArgsBinding binding (Identifier ( - trait_item->trait_identifier ()), - fn.get_return_type ().clone_type (), - output_locus); - bindings.push_back (std::move (binding)); + bindings.emplace_back (Identifier (trait_item->trait_identifier ()), + fn.get_return_type ().clone_type (), + output_locus); args = HIR::GenericArgs ({} /* lifetimes */, std::move (inputs) /* type_args*/, @@ -468,8 +466,8 @@ TypeBoundPredicate::operator= (const TypeBoundPredicate &other) { TyTy::BaseType *argument = m.get_tyty () == nullptr ? nullptr : m.get_tyty ()->clone (); - SubstitutionArg c (&substitutions.at (i++), argument); - copied_arg_mappings.push_back (std::move (c)); + + copied_arg_mappings.emplace_back (&substitutions.at (i++), argument); } used_arguments @@ -691,8 +689,7 @@ TypeBoundPredicateItem::get_tyty_for_receiver (const TyTy::BaseType *receiver) TyTy::BaseType *argument = is_implicit_self ? receiver->clone () : mapping.get_tyty (); - SubstitutionArg arg (mapping.get_param_mapping (), argument); - adjusted_mappings.push_back (std::move (arg)); + adjusted_mappings.emplace_back (mapping.get_param_mapping (), argument); } SubstitutionArgumentMappings adjusted (adjusted_mappings, {}, @@ -827,10 +824,7 @@ TypeBoundPredicate::get_associated_type_items () = trait_item.get_trait_item_type () == Resolver::TraitItemReference::TraitItemType::TYPE; if (is_associated_type) - { - TypeBoundPredicateItem item (*this, &trait_item); - items.push_back (std::move (item)); - } + items.emplace_back (*this, &trait_item); } return items; } diff --git a/gcc/rust/typecheck/rust-tyty-call.cc b/gcc/rust/typecheck/rust-tyty-call.cc index 63bb1ff91f80..7f0b5eb6bfc8 100644 --- a/gcc/rust/typecheck/rust-tyty-call.cc +++ b/gcc/rust/typecheck/rust-tyty-call.cc @@ -327,8 +327,8 @@ TypeCheckMethodCallExpr::go (FnType *ref, HIR::MethodCallExpr &call, return new ErrorType (ref->get_ref ()); } - Argument a (arg->get_mappings (), argument_expr_tyty, arg->get_locus ()); - args.push_back (std::move (a)); + args.emplace_back (arg->get_mappings (), argument_expr_tyty, + arg->get_locus ()); } TypeCheckMethodCallExpr checker (call.get_mappings (), args, diff --git a/gcc/rust/typecheck/rust-tyty-subst.cc b/gcc/rust/typecheck/rust-tyty-subst.cc index 817910b8e762..872d24318ed2 100644 --- a/gcc/rust/typecheck/rust-tyty-subst.cc +++ b/gcc/rust/typecheck/rust-tyty-subst.cc @@ -554,6 +554,7 @@ std::vector<SubstitutionParamMapping> SubstitutionRef::clone_substs () const { std::vector<SubstitutionParamMapping> clone; + clone.reserve (substitutions.size ()); for (auto &sub : substitutions) clone.push_back (sub.clone ()); @@ -777,9 +778,8 @@ SubstitutionRef::get_mappings_from_generic_args ( } } - SubstitutionArg subst_arg (¶m_mapping, resolved); + mappings.emplace_back (¶m_mapping, resolved); offs++; - mappings.push_back (std::move (subst_arg)); } for (auto &arg : args.get_const_args ()) @@ -833,9 +833,8 @@ SubstitutionRef::get_mappings_from_generic_args ( expr.get_mappings ().get_hirid (), expr.get_mappings ().get_hirid (), {}); - SubstitutionArg subst_arg (¶m_mapping, const_value); + mappings.emplace_back (¶m_mapping, const_value); offs++; - mappings.push_back (std::move (subst_arg)); } // we must need to fill out defaults @@ -865,8 +864,7 @@ SubstitutionRef::get_mappings_from_generic_args ( return SubstitutionArgumentMappings::error (); } - SubstitutionArg subst_arg (¶m, resolved); - mappings.push_back (std::move (subst_arg)); + mappings.emplace_back (¶m, resolved); } } @@ -892,12 +890,12 @@ SubstitutionRef::infer_substitions (location_t locus) if (have_mapping) { - args.push_back (SubstitutionArg (&p, it->second)); + args.emplace_back (&p, it->second); } else if (generic.get_kind () == HIR::GenericParam::GenericKind::TYPE) { TyVar infer_var = TyVar::get_implicit_infer_var (locus); - args.push_back (SubstitutionArg (&p, infer_var.get_tyty ())); + args.emplace_back (&p, infer_var.get_tyty ()); argument_mappings[symbol] = infer_var.get_tyty (); } else if (generic.get_kind () == HIR::GenericParam::GenericKind::CONST) @@ -909,13 +907,13 @@ SubstitutionRef::infer_substitions (location_t locus) TyVar infer_var = TyVar::get_implicit_const_infer_var (const_type, locus); - args.push_back (SubstitutionArg (&p, infer_var.get_tyty ())); + args.emplace_back (&p, infer_var.get_tyty ()); argument_mappings[symbol] = infer_var.get_tyty (); } } else { - args.push_back (SubstitutionArg (&p, p.get_param_ty ()->resolve ())); + args.emplace_back (&p, p.get_param_ty ()->resolve ()); } } @@ -961,10 +959,7 @@ SubstitutionRef::adjust_mappings_for_this ( bool ok = !arg.is_error (); if (ok || (trait_mode && i == 0)) - { - SubstitutionArg adjusted (&subst, arg.get_tyty ()); - resolved_mappings.push_back (std::move (adjusted)); - } + resolved_mappings.emplace_back (&subst, arg.get_tyty ()); } if (resolved_mappings.empty ()) @@ -1008,10 +1003,7 @@ SubstitutionRef::are_mappings_bound (SubstitutionArgumentMappings &mappings) bool ok = !arg.is_error (); if (ok) - { - SubstitutionArg adjusted (&subst, arg.get_tyty ()); - resolved_mappings.push_back (std::move (adjusted)); - } + resolved_mappings.emplace_back (&subst, arg.get_tyty ()); } return !resolved_mappings.empty (); @@ -1032,10 +1024,7 @@ SubstitutionRef::solve_mappings_from_receiver_for_self ( SubstitutionArg &arg = mappings.get_mappings ().at (i); if (param_mapping.needs_substitution ()) - { - SubstitutionArg adjusted (¶m_mapping, arg.get_tyty ()); - resolved_mappings.push_back (std::move (adjusted)); - } + resolved_mappings.emplace_back (¶m_mapping, arg.get_tyty ()); } return SubstitutionArgumentMappings (resolved_mappings, diff --git a/gcc/rust/typecheck/rust-tyty-variance-analysis-private.h b/gcc/rust/typecheck/rust-tyty-variance-analysis-private.h index deb76a7246d1..f1833c5874c2 100644 --- a/gcc/rust/typecheck/rust-tyty-variance-analysis-private.h +++ b/gcc/rust/typecheck/rust-tyty-variance-analysis-private.h @@ -47,6 +47,10 @@ struct Term /** Variance constraint of a type parameter. */ struct Constraint { + Constraint (SolutionIndex target_index, Term *term) + : target_index (target_index), term (term) + {} + SolutionIndex target_index; Term *term; }; diff --git a/gcc/rust/typecheck/rust-tyty-variance-analysis.cc b/gcc/rust/typecheck/rust-tyty-variance-analysis.cc index 7971ccfd02e9..d640d55d8c08 100644 --- a/gcc/rust/typecheck/rust-tyty-variance-analysis.cc +++ b/gcc/rust/typecheck/rust-tyty-variance-analysis.cc @@ -321,6 +321,8 @@ GenericTyPerCrateCtx::query_generic_variance (const ADTType &type) auto num_types = type.get_num_type_params (); std::vector<Variance> result; + result.reserve (num_lifetimes + num_types); + for (size_t i = 0; i < num_lifetimes + num_types; ++i) { result.push_back (solutions[solution_index.value () + i]); @@ -410,7 +412,7 @@ GenericTyVisitorCtx::add_constraint (SolutionIndex index, Term term) } else { - ctx.constraints.push_back ({index, new Term (term)}); + ctx.constraints.emplace_back (index, new Term (term)); } } diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index db967737b709..af5f9777fd41 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -2368,8 +2368,10 @@ BaseType * FnPtr::clone () const { std::vector<TyVar> cloned_params; + cloned_params.reserve (params.size ()); + for (auto &p : params) - cloned_params.push_back (TyVar (p.get_ref ())); + cloned_params.emplace_back (p.get_ref ()); return new FnPtr (get_ref (), get_ty_ref (), ident.locus, std::move (cloned_params), result_type, @@ -4287,7 +4289,7 @@ DynamicObjectType::get_object_items () const if (item->get_trait_item_type () == Resolver::TraitItemReference::TraitItemType::FN && item->is_object_safe ()) - items.push_back ({item, &bound}); + items.emplace_back (item, &bound); } } return items; diff --git a/gcc/rust/typecheck/rust-unify.cc b/gcc/rust/typecheck/rust-unify.cc index 30ead5b4e0e9..66ced65ad780 100644 --- a/gcc/rust/typecheck/rust-unify.cc +++ b/gcc/rust/typecheck/rust-unify.cc @@ -44,7 +44,7 @@ UnifyRules::Resolve (TyTy::TyWithLocation lhs, TyTy::TyWithLocation rhs, infers); TyTy::BaseType *result = r.go (); - commits.push_back ({lhs.get_ty (), rhs.get_ty (), result}); + commits.emplace_back (lhs.get_ty (), rhs.get_ty (), result); if (r.commit_flag) UnifyRules::commit (lhs.get_ty (), rhs.get_ty (), result); @@ -222,7 +222,7 @@ UnifyRules::go () rust_assert (iv.get_tyty ()->get_kind () == TyTy::TypeKind::INFER); TyTy::InferType *i = static_cast<TyTy::InferType *> (iv.get_tyty ()); - infers.push_back ({p->get_ref (), p->get_ty_ref (), p, i}); + infers.emplace_back (p->get_ref (), p->get_ty_ref (), p, i); // FIXME // this is hacky to set the implicit param lets make this a function @@ -239,7 +239,7 @@ UnifyRules::go () rust_assert (iv.get_tyty ()->get_kind () == TyTy::TypeKind::INFER); TyTy::InferType *i = static_cast<TyTy::InferType *> (iv.get_tyty ()); - infers.push_back ({p->get_ref (), p->get_ty_ref (), p, i}); + infers.emplace_back (p->get_ref (), p->get_ty_ref (), p, i); // FIXME // this is hacky to set the implicit param lets make this a function @@ -1223,7 +1223,7 @@ UnifyRules::expect_tuple (TyTy::TupleType *ltype, TyTy::BaseType *rtype) if (unified_ty->get_kind () == TyTy::TypeKind::ERROR) return new TyTy::ErrorType (0); - fields.push_back (TyTy::TyVar (unified_ty->get_ref ())); + fields.emplace_back (unified_ty->get_ref ()); } return new TyTy::TupleType (type.get_ref (), type.get_ty_ref (), diff --git a/gcc/rust/typecheck/rust-unify.h b/gcc/rust/typecheck/rust-unify.h index b8c9cbcfcd67..fc7e8666ab06 100644 --- a/gcc/rust/typecheck/rust-unify.h +++ b/gcc/rust/typecheck/rust-unify.h @@ -30,6 +30,11 @@ class UnifyRules public: struct InferenceSite { + InferenceSite (HirId pref, HirId ptyref, TyTy::ParamType *param, + TyTy::InferType *infer) + : pref (pref), ptyref (ptyref), param (param), infer (infer) + {} + HirId pref; HirId ptyref; TyTy::ParamType *param; @@ -37,6 +42,11 @@ public: }; struct CommitSite { + CommitSite (TyTy::BaseType *lhs, TyTy::BaseType *rhs, + TyTy::BaseType *resolved) + : lhs (lhs), rhs (rhs), resolved (resolved) + {} + TyTy::BaseType *lhs; TyTy::BaseType *rhs; TyTy::BaseType *resolved; diff --git a/gcc/rust/util/rust-canonical-path.h b/gcc/rust/util/rust-canonical-path.h index 079ae76eadba..4c4d9dfdd9cb 100644 --- a/gcc/rust/util/rust-canonical-path.h +++ b/gcc/rust/util/rust-canonical-path.h @@ -114,6 +114,8 @@ public: return CanonicalPath (other.segs, crate_num); std::vector<std::pair<NodeId, std::string>> copy (segs); + copy.reserve (other.segs.size ()); + for (auto &s : other.segs) copy.push_back (s); diff --git a/gcc/rust/util/rust-token-converter.cc b/gcc/rust/util/rust-token-converter.cc index 52172f40cfa6..0865bf92c687 100644 --- a/gcc/rust/util/rust-token-converter.cc +++ b/gcc/rust/util/rust-token-converter.cc @@ -102,7 +102,9 @@ ProcMacro::TokenStream convert (const std::vector<const_TokenPtr> &tokens) { std::vector<ProcMacro::TokenStream> trees; - trees.push_back (ProcMacro::TokenStream::make_tokenstream ()); + trees.reserve (tokens.size ()); + + trees.emplace_back (ProcMacro::TokenStream::make_tokenstream ()); for (auto &token : tokens) { auto loc = convert (token->get_locus ());
