[clang] [RFC] Initial implementation of P2719 (PR #113510)

via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 6 01:38:33 PST 2024


================
@@ -2709,14 +2831,63 @@ static bool resolveAllocationOverload(
   llvm_unreachable("Unreachable, bad result from BestViableFunction");
 }
 
-bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range,
-                                   AllocationFunctionScope NewScope,
-                                   AllocationFunctionScope DeleteScope,
-                                   QualType AllocType, bool IsArray,
-                                   bool &PassAlignment, MultiExprArg PlaceArgs,
-                                   FunctionDecl *&OperatorNew,
-                                   FunctionDecl *&OperatorDelete,
-                                   bool Diagnose) {
+enum class DeallocLookupMode { Untyped, OptionallyTyped, RequireTyped };
+
+static void LookupGlobalDeallocationFunctions(Sema &S, SourceLocation Loc,
+                                              LookupResult &FoundDelete,
+                                              DeallocLookupMode Mode,
+                                              DeclarationName Name,
+                                              QualType DeallocType) {
+  S.LookupQualifiedName(FoundDelete, S.Context.getTranslationUnitDecl());
+  if (Mode == DeallocLookupMode::OptionallyTyped) {
+    bool RemoveTypedDecl = Mode == DeallocLookupMode::Untyped;
+    LookupResult::Filter Filter = FoundDelete.makeFilter();
+    while (Filter.hasNext()) {
+      NamedDecl *Decl = Filter.next()->getUnderlyingDecl();
+      bool DeclIsTypeAware = S.isTypeAwareOperatorNewOrDelete(Decl);
+      if (DeclIsTypeAware && RemoveTypedDecl)
+        Filter.erase();
+    }
+    Filter.done();
+  }
+}
+
+static bool resolveAllocationOverload(
+    Sema &S, LookupResult &R, SourceRange Range, SmallVectorImpl<Expr *> &Args,
+    ImplicitAllocationParameters &IAP, FunctionDecl *&Operator,
+    OverloadCandidateSet *AlignedCandidates, Expr *AlignArg, bool Diagnose) {
+  Operator = nullptr;
+  if (isTypeAwareAllocation(IAP.PassTypeIdentity)) {
+    assert(Args[0]->getType()->isTypeIdentitySpecialization());
+    SmallVector<Expr *> UntypedParameters;
+    UntypedParameters.reserve(Args.size() - 1);
+    UntypedParameters.append(Args.begin() + 1, Args.end());
+    AlignedAllocationMode InitialAlignmentMode = IAP.PassAlignment;
+    if (resolveAllocationOverloadInterior(
+            S, R, Range, ResolveMode::Typed, Args, IAP.PassAlignment, Operator,
+            AlignedCandidates, AlignArg, Diagnose))
+      return true;
+    if (Operator)
+      return false;
+    // There's no type aware allocator
+    IAP.PassTypeIdentity = TypeAwareAllocationMode::No;
+    // Restore alignment requirements
+    IAP.PassAlignment = InitialAlignmentMode;
+    // Finally prepare the type free parameter list
+    Args = UntypedParameters;
----------------
cor3ntin wrote:

Can you explain why we need to remove the type?
(also, I think `Args.erase(Args.begin())` would work as well as saving a copy, right?)

https://github.com/llvm/llvm-project/pull/113510


More information about the cfe-commits mailing list