From b24716aee61e919572980b98f697c931325667e2 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Sun, 12 Jul 2026 16:08:49 -0700 Subject: [PATCH] Avoid allocating per-cursor LazyList factory delegates using function pointers `LazyList` previously stored a `Func` (or `Func`) factory per instance, which allocated a closure delegate for every cursor that exposed a lazy child list -- even when the list was empty (the common case for `Decl._attrs`). Mirroring the `ValueLazy` conversion, the factory is now a `delegate*` function pointer plus a stored `object _self`, so the closure allocation is gone. All owners (`Cursor`, `Type`, `TemplateArgument`) are reference types, so passing `this` as `object self` never boxes. Each former lambda becomes a `static` factory method that casts `self` back to the owning type. Measured on the TerraFX d3d12 generation workload (gc-verbose, `DOTNET_TieredCompilation=0`): `Func` (~2.0 MB) and `Func` (~1.9 MB) are eliminated entirely, dropping sampled allocations from ~142.6 MB to ~134.2 MB. No behavioral change; all 3708 golden tests pass. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- sources/ClangSharp/Cursors/Decls/BlockDecl.cs | 16 +++++- .../Cursors/Decls/CXXConstructorDecl.cs | 8 ++- .../ClangSharp/Cursors/Decls/CXXMethodDecl.cs | 8 ++- .../ClangSharp/Cursors/Decls/CXXRecordDecl.cs | 40 +++++++++++-- .../ClangSharp/Cursors/Decls/CapturedDecl.cs | 8 ++- .../Cursors/Decls/ClassTemplateDecl.cs | 8 ++- .../ClassTemplatePartialSpecializationDecl.cs | 16 +++++- .../Decls/ClassTemplateSpecializationDecl.cs | 10 +++- sources/ClangSharp/Cursors/Decls/Decl.cs | 57 +++++++++++++++---- .../Cursors/Decls/DeclaratorDecl.cs | 5 +- .../Cursors/Decls/DecompositionDecl.cs | 10 +++- sources/ClangSharp/Cursors/Decls/EnumDecl.cs | 8 ++- .../ClangSharp/Cursors/Decls/FriendDecl.cs | 5 +- .../Cursors/Decls/FriendTemplateDecl.cs | 5 +- .../ClangSharp/Cursors/Decls/FunctionDecl.cs | 16 +++++- .../Cursors/Decls/FunctionTemplateDecl.cs | 18 +++++- .../ImplicitConceptSpecializationDecl.cs | 10 +++- .../Cursors/Decls/NonTypeTemplateParmDecl.cs | 16 +++++- .../Cursors/Decls/ObjCCategoryDecl.cs | 16 +++++- .../Cursors/Decls/ObjCImplementationDecl.cs | 8 ++- .../Cursors/Decls/ObjCInterfaceDecl.cs | 16 +++++- .../Cursors/Decls/ObjCMethodDecl.cs | 8 ++- .../Cursors/Decls/ObjCProtocolDecl.cs | 8 ++- .../ClangSharp/Cursors/Decls/RecordDecl.cs | 8 ++- sources/ClangSharp/Cursors/Decls/TagDecl.cs | 5 +- .../ClangSharp/Cursors/Decls/TemplateDecl.cs | 16 +++++- .../Cursors/Decls/TemplateTypeParmDecl.cs | 8 ++- .../VarTemplatePartialSpecializationDecl.cs | 16 +++++- .../Decls/VarTemplateSpecializationDecl.cs | 8 ++- .../Exprs/CXXDependentScopeMemberExpr.cs | 8 ++- .../Cursors/Exprs/CXXParenListInitExpr.cs | 16 +++++- sources/ClangSharp/Cursors/Exprs/CastExpr.cs | 8 ++- .../ClangSharp/Cursors/Exprs/DeclRefExpr.cs | 8 ++- .../Exprs/DependentScopeDeclRefExpr.cs | 10 +++- .../Cursors/Exprs/ExprWithCleanups.cs | 10 +++- .../Cursors/Exprs/FunctionParmPackExpr.cs | 8 ++- .../ClangSharp/Cursors/Exprs/MemberExpr.cs | 8 ++- .../Cursors/Exprs/ObjCMessageExpr.cs | 8 ++- .../ClangSharp/Cursors/Exprs/OverloadExpr.cs | 16 +++++- .../Cursors/Exprs/SizeOfPackExpr.cs | 8 ++- .../Cursors/Stmts/AttributedStmt.cs | 10 +++- .../ClangSharp/Cursors/Stmts/CapturedStmt.cs | 8 ++- sources/ClangSharp/Cursors/Stmts/DeclStmt.cs | 10 +++- sources/ClangSharp/Cursors/Stmts/Stmt.cs | 12 ++-- sources/ClangSharp/LazyList.cs | 9 ++- sources/ClangSharp/LazyList`1.cs | 23 +++++--- sources/ClangSharp/LazyList`2.cs | 14 +++-- sources/ClangSharp/TemplateArgument.cs | 8 ++- sources/ClangSharp/Types/AutoType.cs | 10 +++- .../DependentTemplateSpecializationType.cs | 10 +++- sources/ClangSharp/Types/FunctionProtoType.cs | 10 +++- sources/ClangSharp/Types/ObjCObjectType.cs | 16 +++++- .../Types/TemplateSpecializationType.cs | 8 ++- 53 files changed, 515 insertions(+), 122 deletions(-) diff --git a/sources/ClangSharp/Cursors/Decls/BlockDecl.cs b/sources/ClangSharp/Cursors/Decls/BlockDecl.cs index 3f2f4202..da1c0936 100644 --- a/sources/ClangSharp/Cursors/Decls/BlockDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/BlockDecl.cs @@ -16,8 +16,8 @@ public sealed partial class BlockDecl : Decl, IDeclContext internal unsafe BlockDecl(CXCursor handle) : base(handle, CXCursor_UnexposedDecl, CX_DeclKind_Block) { _blockManglingContextDecl = new ValueLazy(&BlockManglingContextDeclFactory); - _captures = LazyList.Create(Handle.NumCaptures, (i) => new Capture(this, unchecked((uint)i))); - _parameters = LazyList.Create(Handle.NumArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetArgument(unchecked((uint)i)))); + _captures = LazyList.Create(this, Handle.NumCaptures, &CapturesFactory); + _parameters = LazyList.Create(this, Handle.NumArguments, &ParametersFactory); } public Decl BlockManglingContextDecl => _blockManglingContextDecl.GetValue(this); @@ -55,4 +55,16 @@ internal unsafe BlockDecl(CXCursor handle) : base(handle, CXCursor_UnexposedDecl public bool CapturesVariable(VarDecl var) => Handle.CapturesVariable((var is not null) ? var.Handle : CXCursor.Null); private static unsafe Decl BlockManglingContextDeclFactory(BlockDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.BlockManglingContextDecl); + + private static unsafe Capture CapturesFactory(object self, int i) + { + var @this = (BlockDecl)self; + return new Capture(@this, unchecked((uint)i)); + } + + private static unsafe ParmVarDecl ParametersFactory(object self, int i) + { + var @this = (BlockDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetArgument(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/CXXConstructorDecl.cs b/sources/ClangSharp/Cursors/Decls/CXXConstructorDecl.cs index 2663d5a8..44e54af6 100644 --- a/sources/ClangSharp/Cursors/Decls/CXXConstructorDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/CXXConstructorDecl.cs @@ -15,7 +15,7 @@ public sealed class CXXConstructorDecl : CXXMethodDecl internal unsafe CXXConstructorDecl(CXCursor handle) : base(handle, CXCursor_Constructor, CX_DeclKind_CXXConstructor) { _inheritedConstructor = new ValueLazy(&InheritedConstructorFactory); - _initExprs = LazyList.Create(Handle.NumExprs , (i) => TranslationUnit.GetOrCreate(Handle.GetExpr(unchecked((uint)i)))); + _initExprs = LazyList.Create(this, Handle.NumExprs, &InitExprsFactory); } public new CXXConstructorDecl CanonicalDecl => (CXXConstructorDecl)base.CanonicalDecl; @@ -58,4 +58,10 @@ public CXXConstructorDecl? TargetConstructor } private static unsafe CXXConstructorDecl InheritedConstructorFactory(CXXConstructorDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.InheritedConstructor); + + private static unsafe Expr InitExprsFactory(object self, int i) + { + var @this = (CXXConstructorDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetExpr(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/CXXMethodDecl.cs b/sources/ClangSharp/Cursors/Decls/CXXMethodDecl.cs index c2848483..0049e9ed 100644 --- a/sources/ClangSharp/Cursors/Decls/CXXMethodDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/CXXMethodDecl.cs @@ -25,7 +25,7 @@ private protected unsafe CXXMethodDecl(CXCursor handle, CXCursorKind expectedCur throw new ArgumentOutOfRangeException(nameof(handle)); } - _overriddenMethods = LazyList.Create(Handle.NumMethods, (i) => TranslationUnit.GetOrCreate(Handle.GetMethod(unchecked((uint)i)))); + _overriddenMethods = LazyList.Create(this, Handle.NumMethods, &OverriddenMethodsFactory); _thisType = new ValueLazy(&ThisTypeFactory); _thisObjectType = new ValueLazy(&ThisObjectTypeFactory); } @@ -57,4 +57,10 @@ private protected unsafe CXXMethodDecl(CXCursor handle, CXCursorKind expectedCur private static unsafe Type ThisObjectTypeFactory(CXXMethodDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.ThisObjectType); private static unsafe Type ThisTypeFactory(CXXMethodDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.ThisType); + + private static unsafe CXXMethodDecl OverriddenMethodsFactory(object self, int i) + { + var @this = (CXXMethodDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetMethod(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/CXXRecordDecl.cs b/sources/ClangSharp/Cursors/Decls/CXXRecordDecl.cs index 81d3ce5b..be380069 100644 --- a/sources/ClangSharp/Cursors/Decls/CXXRecordDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/CXXRecordDecl.cs @@ -35,19 +35,19 @@ private protected unsafe CXXRecordDecl(CXCursor handle, CXCursorKind expectedCur throw new ArgumentOutOfRangeException(nameof(handle)); } - _bases = LazyList.Create(Handle.NumBases, (i) => TranslationUnit.GetOrCreate(Handle.GetBase(unchecked((uint)i)))); - _ctors = LazyList.Create(Handle.NumCtors, (i) => TranslationUnit.GetOrCreate(Handle.GetCtor(unchecked((uint)i)))); + _bases = LazyList.Create(this, Handle.NumBases, &BasesFactory); + _ctors = LazyList.Create(this, Handle.NumCtors, &CtorsFactory); _dependentLambdaCallOperator = new ValueLazy(&DependentLambdaCallOperatorFactory); _describedClassTemplate = new ValueLazy(&DescribedClassTemplateFactory); _destructor = new ValueLazy(&DestructorFactory); - _friends = LazyList.Create(Handle.NumFriends, (i) => TranslationUnit.GetOrCreate(Handle.GetFriend(unchecked((uint)i)))); + _friends = LazyList.Create(this, Handle.NumFriends, &FriendsFactory); _instantiatedFromMemberClass = new ValueLazy(&InstantiatedFromMemberClassFactory); _lambdaCallOperator = new ValueLazy(&LambdaCallOperatorFactory); _lambdaContextDecl = new ValueLazy(&LambdaContextDeclFactory); _lambdaStaticInvoker = new ValueLazy(&LambdaStaticInvokerFactory); - _methods = LazyList.Create(Handle.NumMethods, (i) => TranslationUnit.GetOrCreate(Handle.GetMethod(unchecked((uint)i)))); + _methods = LazyList.Create(this, Handle.NumMethods, &MethodsFactory); _templateInstantiationPattern = new ValueLazy(&TemplateInstantiationPatternFactory); - _vbases = LazyList.Create(Handle.NumVBases, (i) => TranslationUnit.GetOrCreate(Handle.GetVBase(unchecked((uint)i)))); + _vbases = LazyList.Create(this, Handle.NumVBases, &VbasesFactory); } public bool IsAbstract => Handle.CXXRecord_IsAbstract; @@ -146,4 +146,34 @@ public CXXRecordDecl MostRecentNonInjectedDecl private static unsafe ClassTemplateDecl DescribedClassTemplateFactory(CXXRecordDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.DescribedCursorTemplate); private static unsafe FunctionTemplateDecl DependentLambdaCallOperatorFactory(CXXRecordDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.DependentLambdaCallOperator); + + private static unsafe CXXBaseSpecifier BasesFactory(object self, int i) + { + var @this = (CXXRecordDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetBase(unchecked((uint)i))); + } + + private static unsafe CXXConstructorDecl CtorsFactory(object self, int i) + { + var @this = (CXXRecordDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetCtor(unchecked((uint)i))); + } + + private static unsafe FriendDecl FriendsFactory(object self, int i) + { + var @this = (CXXRecordDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetFriend(unchecked((uint)i))); + } + + private static unsafe CXXMethodDecl MethodsFactory(object self, int i) + { + var @this = (CXXRecordDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetMethod(unchecked((uint)i))); + } + + private static unsafe CXXBaseSpecifier VbasesFactory(object self, int i) + { + var @this = (CXXRecordDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetVBase(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/CapturedDecl.cs b/sources/ClangSharp/Cursors/Decls/CapturedDecl.cs index 943e28c5..3d34737a 100644 --- a/sources/ClangSharp/Cursors/Decls/CapturedDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/CapturedDecl.cs @@ -15,7 +15,7 @@ public sealed class CapturedDecl : Decl, IDeclContext internal unsafe CapturedDecl(CXCursor handle) : base(handle, CXCursor_UnexposedDecl, CX_DeclKind_Captured) { _contextParam = new ValueLazy(&ContextParamFactory); - _parameters = LazyList.Create(Handle.NumArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetArgument(unchecked((uint)i)))); + _parameters = LazyList.Create(this, Handle.NumArguments, &ParametersFactory); } public ImplicitParamDecl ContextParam => _contextParam.GetValue(this); @@ -29,4 +29,10 @@ internal unsafe CapturedDecl(CXCursor handle) : base(handle, CXCursor_UnexposedD public IReadOnlyList Parameters => _parameters; private static unsafe ImplicitParamDecl ContextParamFactory(CapturedDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.ContextParam); + + private static unsafe ImplicitParamDecl ParametersFactory(object self, int i) + { + var @this = (CapturedDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetArgument(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/ClassTemplateDecl.cs b/sources/ClangSharp/Cursors/Decls/ClassTemplateDecl.cs index 6deddc9b..abd4e180 100644 --- a/sources/ClangSharp/Cursors/Decls/ClassTemplateDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/ClassTemplateDecl.cs @@ -15,7 +15,7 @@ public sealed class ClassTemplateDecl : RedeclarableTemplateDecl internal unsafe ClassTemplateDecl(CXCursor handle) : base(handle, CXCursor_ClassTemplate, CX_DeclKind_ClassTemplate) { _injectedClassNameSpecialization = new ValueLazy(&InjectedClassNameSpecializationFactory); - _specializations = LazyList.Create(Handle.NumSpecializations, (i) => TranslationUnit.GetOrCreate(Handle.GetSpecialization(unchecked((uint)i)))); + _specializations = LazyList.Create(this, Handle.NumSpecializations, &SpecializationsFactory); } public new ClassTemplateDecl CanonicalDecl => (ClassTemplateDecl)base.CanonicalDecl; @@ -35,4 +35,10 @@ internal unsafe ClassTemplateDecl(CXCursor handle) : base(handle, CXCursor_Class public new CXXRecordDecl TemplatedDecl => (CXXRecordDecl)base.TemplatedDecl; private static unsafe Type InjectedClassNameSpecializationFactory(ClassTemplateDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.InjectedSpecializationType); + + private static unsafe ClassTemplateSpecializationDecl SpecializationsFactory(object self, int i) + { + var @this = (ClassTemplateDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetSpecialization(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/ClassTemplatePartialSpecializationDecl.cs b/sources/ClangSharp/Cursors/Decls/ClassTemplatePartialSpecializationDecl.cs index ec8f834e..7550cd00 100644 --- a/sources/ClangSharp/Cursors/Decls/ClassTemplatePartialSpecializationDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/ClassTemplatePartialSpecializationDecl.cs @@ -16,10 +16,10 @@ public sealed class ClassTemplatePartialSpecializationDecl : ClassTemplateSpecia internal unsafe ClassTemplatePartialSpecializationDecl(CXCursor handle) : base(handle, CXCursor_ClassTemplatePartialSpecialization, CX_DeclKind_ClassTemplatePartialSpecialization) { - _associatedConstraints = LazyList.Create(Handle.NumAssociatedConstraints, (i) => TranslationUnit.GetOrCreate(Handle.GetAssociatedConstraint(unchecked((uint)i)))); + _associatedConstraints = LazyList.Create(this, Handle.NumAssociatedConstraints, &AssociatedConstraintsFactory); _injectedSpecializationType = new ValueLazy(&InjectedSpecializationTypeFactory); _instantiatedFromMember = new ValueLazy(&InstantiatedFromMemberFactory); - _templateParameters = LazyList.Create(Handle.GetNumTemplateParameters(0), (i) => TranslationUnit.GetOrCreate(Handle.GetTemplateParameter(0, unchecked((uint)i)))); + _templateParameters = LazyList.Create(this, Handle.GetNumTemplateParameters(0), &TemplateParametersFactory); } public IReadOnlyList AssociatedConstraints => _associatedConstraints; @@ -41,4 +41,16 @@ internal unsafe ClassTemplatePartialSpecializationDecl(CXCursor handle) : base(h private static unsafe ClassTemplatePartialSpecializationDecl InstantiatedFromMemberFactory(ClassTemplatePartialSpecializationDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.InstantiatedFromMember); private static unsafe Type InjectedSpecializationTypeFactory(ClassTemplatePartialSpecializationDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.InjectedSpecializationType); + + private static unsafe Expr AssociatedConstraintsFactory(object self, int i) + { + var @this = (ClassTemplatePartialSpecializationDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetAssociatedConstraint(unchecked((uint)i))); + } + + private static unsafe NamedDecl TemplateParametersFactory(object self, int i) + { + var @this = (ClassTemplatePartialSpecializationDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetTemplateParameter(0, unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/ClassTemplateSpecializationDecl.cs b/sources/ClangSharp/Cursors/Decls/ClassTemplateSpecializationDecl.cs index 86471ba6..902406f4 100644 --- a/sources/ClangSharp/Cursors/Decls/ClassTemplateSpecializationDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/ClassTemplateSpecializationDecl.cs @@ -17,7 +17,7 @@ public class ClassTemplateSpecializationDecl : CXXRecordDecl internal unsafe ClassTemplateSpecializationDecl(CXCursor handle) : this(handle, handle.Kind, CX_DeclKind_ClassTemplateSpecialization) { _specializedTemplate = new ValueLazy(&SpecializedTemplateFactory); - _templateArgs = LazyList.Create(Handle.NumTemplateArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetTemplateArgument(unchecked((uint)i)))); + _templateArgs = LazyList.Create(this, Handle.NumTemplateArguments, &TemplateArgsFactory); } private protected unsafe ClassTemplateSpecializationDecl(CXCursor handle, CXCursorKind expectedCursorKind, CX_DeclKind expectedDeclKind) : base(handle, expectedCursorKind, expectedDeclKind) @@ -28,7 +28,7 @@ private protected unsafe ClassTemplateSpecializationDecl(CXCursor handle, CXCurs } _specializedTemplate = new ValueLazy(&SpecializedTemplateFactory); - _templateArgs = LazyList.Create(Handle.NumTemplateArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetTemplateArgument(unchecked((uint)i)))); + _templateArgs = LazyList.Create(this, Handle.NumTemplateArguments, &TemplateArgsFactory); } public bool IsClassScopeExplicitSpecialization => IsExplicitSpecialization && (LexicalDeclContext is CXXRecordDecl); @@ -69,4 +69,10 @@ public bool IsExplicitInstantiationOrSpecialization public IReadOnlyList TemplateArgs => _templateArgs; private static unsafe ClassTemplateDecl SpecializedTemplateFactory(ClassTemplateSpecializationDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.SpecializedCursorTemplate); + + private static unsafe TemplateArgument TemplateArgsFactory(object self, int i) + { + var @this = (ClassTemplateSpecializationDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetTemplateArgument(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/Decl.cs b/sources/ClangSharp/Cursors/Decls/Decl.cs index ddc98eb0..8af01533 100644 --- a/sources/ClangSharp/Cursors/Decls/Decl.cs +++ b/sources/ClangSharp/Cursors/Decls/Decl.cs @@ -37,19 +37,10 @@ private protected unsafe Decl(CXCursor handle, CXCursorKind expectedCursorKind, } _asFunction = new ValueLazy(&AsFunctionFactory); - _attrs = LazyList.Create(Handle.NumAttrs, (i) => TranslationUnit.GetOrCreate(Handle.GetAttr(unchecked((uint)i)))); + _attrs = LazyList.Create(this, Handle.NumAttrs, &AttrsFactory); _body = new ValueLazy(&BodyFactory); _canonicalDecl = new ValueLazy(&CanonicalDeclFactory); - _decls = LazyList.Create(Handle.NumDecls, (i, previousDecl) => { - if (previousDecl is null) - { - return TranslationUnit.GetOrCreate(Handle.GetDecl(unchecked((uint)i))); - } - else - { - return previousDecl.NextDeclInContext; - } - }); + _decls = LazyList.Create(this, Handle.NumDecls, &DeclsFactory); _describedTemplate = new ValueLazy(&DescribedTemplateFactory); _mostRecentDecl = new ValueLazy(&MostRecentDeclFactory); _nextDeclInContext = new ValueLazy(&NextDeclInContextFactory); @@ -276,4 +267,48 @@ public bool IsStdNamespace private static unsafe Stmt? BodyFactory(Decl self) => !self.Handle.Body.IsNull ? self.TranslationUnit.GetOrCreate(self.Handle.Body) : null; private static unsafe FunctionDecl AsFunctionFactory(Decl self) => self.TranslationUnit.GetOrCreate(self.Handle.AsFunction); + + private static unsafe Attr AttrsFactory(object self, int i) + { + var @this = (Decl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetAttr(unchecked((uint)i))); + } + + private static unsafe Decl DeclsFactory(object self, int i, Decl? previousDecl) + { + var @this = (Decl)self; + + if (previousDecl is null) + { + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetDecl(unchecked((uint)i))); + } + else + { + return previousDecl.NextDeclInContext; + } + } + + private protected static unsafe LazyList> CreateTemplateParameterLists(Decl self) + { + return LazyList.Create>(self, self.Handle.NumTemplateParameterLists, &TemplateParameterListFactory); + } + + private static unsafe LazyList TemplateParameterListFactory(object self, int listIndex) + { + var @this = (Decl)self; + var numTemplateParameters = @this.Handle.GetNumTemplateParameters(unchecked((uint)listIndex)); + return LazyList.Create(new TemplateParameterListContext(@this, unchecked((uint)listIndex)), numTemplateParameters, &TemplateParameterFactory); + } + + private static unsafe NamedDecl TemplateParameterFactory(object self, int parameterIndex) + { + var context = (TemplateParameterListContext)self; + return context.Owner.TranslationUnit.GetOrCreate(context.Owner.Handle.GetTemplateParameter(context.ListIndex, unchecked((uint)parameterIndex))); + } + + private sealed class TemplateParameterListContext(Decl owner, uint listIndex) + { + public readonly Decl Owner = owner; + public readonly uint ListIndex = listIndex; + } } diff --git a/sources/ClangSharp/Cursors/Decls/DeclaratorDecl.cs b/sources/ClangSharp/Cursors/Decls/DeclaratorDecl.cs index dcc586c2..d266d55f 100644 --- a/sources/ClangSharp/Cursors/Decls/DeclaratorDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/DeclaratorDecl.cs @@ -19,10 +19,7 @@ private protected unsafe DeclaratorDecl(CXCursor handle, CXCursorKind expectedCu throw new ArgumentOutOfRangeException(nameof(handle)); } - _templateParameterLists = LazyList.Create>(Handle.NumTemplateParameterLists, (listIndex) => { - var numTemplateParameters = Handle.GetNumTemplateParameters(unchecked((uint)listIndex)); - return LazyList.Create(numTemplateParameters, (parameterIndex) => TranslationUnit.GetOrCreate(Handle.GetTemplateParameter(unchecked((uint)listIndex), unchecked((uint)parameterIndex)))); - }); + _templateParameterLists = CreateTemplateParameterLists(this); _trailingRequiresClause = new ValueLazy(&TrailingRequiresClauseFactory); } diff --git a/sources/ClangSharp/Cursors/Decls/DecompositionDecl.cs b/sources/ClangSharp/Cursors/Decls/DecompositionDecl.cs index 7259760f..bc12addf 100644 --- a/sources/ClangSharp/Cursors/Decls/DecompositionDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/DecompositionDecl.cs @@ -11,10 +11,16 @@ public sealed class DecompositionDecl : VarDecl { private readonly LazyList _bindings; - internal DecompositionDecl(CXCursor handle) : base(handle, CXCursor_UnexposedDecl, CX_DeclKind_Decomposition) + internal unsafe DecompositionDecl(CXCursor handle) : base(handle, CXCursor_UnexposedDecl, CX_DeclKind_Decomposition) { - _bindings = LazyList.Create(Handle.NumBindings, (i) => TranslationUnit.GetOrCreate(Handle.GetBindingDecl(unchecked((uint)i)))); + _bindings = LazyList.Create(this, Handle.NumBindings, &BindingsFactory); } public IReadOnlyList Bindings => _bindings; + + private static unsafe BindingDecl BindingsFactory(object self, int i) + { + var @this = (DecompositionDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetBindingDecl(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/EnumDecl.cs b/sources/ClangSharp/Cursors/Decls/EnumDecl.cs index ecb21bb6..451e4499 100644 --- a/sources/ClangSharp/Cursors/Decls/EnumDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/EnumDecl.cs @@ -17,7 +17,7 @@ public sealed class EnumDecl : TagDecl internal unsafe EnumDecl(CXCursor handle) : base(handle, CXCursor_EnumDecl, CX_DeclKind_Enum) { - _enumerators = LazyList.Create(Handle.NumEnumerators, (i) => TranslationUnit.GetOrCreate(Handle.GetEnumerator(unchecked((uint)i)))); + _enumerators = LazyList.Create(this, Handle.NumEnumerators, &EnumeratorsFactory); _instantiatedFromMemberEnum = new ValueLazy(&InstantiatedFromMemberEnumFactory); _integerType = new ValueLazy(&IntegerTypeFactory); _promotionType = new ValueLazy(&PromotionTypeFactory); @@ -55,4 +55,10 @@ internal unsafe EnumDecl(CXCursor handle) : base(handle, CXCursor_EnumDecl, CX_D private static unsafe Type IntegerTypeFactory(EnumDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.EnumDecl_IntegerType); private static unsafe EnumDecl InstantiatedFromMemberEnumFactory(EnumDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.InstantiatedFromMember); + + private static unsafe EnumConstantDecl EnumeratorsFactory(object self, int i) + { + var @this = (EnumDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetEnumerator(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/FriendDecl.cs b/sources/ClangSharp/Cursors/Decls/FriendDecl.cs index 0c5c72d7..adb3ee35 100644 --- a/sources/ClangSharp/Cursors/Decls/FriendDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/FriendDecl.cs @@ -15,10 +15,7 @@ public sealed class FriendDecl : Decl internal unsafe FriendDecl(CXCursor handle) : base(handle, CXCursor_FriendDecl, CX_DeclKind_Friend) { _friendNamedDecl = new ValueLazy(&FriendNamedDeclFactory); - _friendTypeTemplateParameterLists = LazyList.Create>(Handle.NumTemplateParameterLists, (listIndex) => { - var numTemplateParameters = Handle.GetNumTemplateParameters(unchecked((uint)listIndex)); - return LazyList.Create(numTemplateParameters, (parameterIndex) => TranslationUnit.GetOrCreate(Handle.GetTemplateParameter(unchecked((uint)listIndex), unchecked((uint)parameterIndex)))); - }); + _friendTypeTemplateParameterLists = CreateTemplateParameterLists(this); } public NamedDecl FriendNamedDecl => _friendNamedDecl.GetValue(this); diff --git a/sources/ClangSharp/Cursors/Decls/FriendTemplateDecl.cs b/sources/ClangSharp/Cursors/Decls/FriendTemplateDecl.cs index d598e7c6..ffdb92fa 100644 --- a/sources/ClangSharp/Cursors/Decls/FriendTemplateDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/FriendTemplateDecl.cs @@ -17,10 +17,7 @@ internal unsafe FriendTemplateDecl(CXCursor handle) : base(handle, CXCursor_Unex { _friendDecl = new ValueLazy(&FriendDeclFactory); _friendType = new ValueLazy(&FriendTypeFactory); - _templateParameterLists = LazyList.Create>(Handle.NumTemplateParameterLists, (listIndex) => { - var numTemplateParameters = Handle.GetNumTemplateParameters(unchecked((uint)listIndex)); - return LazyList.Create(numTemplateParameters, (parameterIndex) => TranslationUnit.GetOrCreate(Handle.GetTemplateParameter(unchecked((uint)listIndex), unchecked((uint)parameterIndex)))); - }); + _templateParameterLists = CreateTemplateParameterLists(this); } public NamedDecl FriendDecl => _friendDecl.GetValue(this); diff --git a/sources/ClangSharp/Cursors/Decls/FunctionDecl.cs b/sources/ClangSharp/Cursors/Decls/FunctionDecl.cs index cef262a7..0aa62131 100644 --- a/sources/ClangSharp/Cursors/Decls/FunctionDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/FunctionDecl.cs @@ -37,11 +37,11 @@ private protected unsafe FunctionDecl(CXCursor handle, CXCursorKind expectedCurs _definition = new ValueLazy(&DefinitionFactory); _describedFunctionDecl = new ValueLazy(&DescribedFunctionDeclFactory); _instantiatedFromMemberFunction = new ValueLazy(&InstantiatedFromMemberFunctionFactory); - _parameters = LazyList.Create(Handle.NumArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetArgument(unchecked((uint)i)))); + _parameters = LazyList.Create(this, Handle.NumArguments, &ParametersFactory); _primaryTemplate = new ValueLazy(&PrimaryTemplateFactory); _returnType = new ValueLazy(&ReturnTypeFactory); _templateInstantiationPattern = new ValueLazy(&TemplateInstantiationPatternFactory); - _templateSpecializationArgs = LazyList.Create(Handle.NumTemplateArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetTemplateArgument(unchecked((uint)i)))); + _templateSpecializationArgs = LazyList.Create(this, Handle.NumTemplateArguments, &TemplateSpecializationArgsFactory); } public Type CallResultType => _callResultType.GetValue(this); @@ -127,4 +127,16 @@ private protected unsafe FunctionDecl(CXCursor handle, CXCursorKind expectedCurs private static unsafe Type DeclaredReturnTypeFactory(FunctionDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.DeclaredReturnType); private static unsafe Type CallResultTypeFactory(FunctionDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.CallResultType); + + private static unsafe ParmVarDecl ParametersFactory(object self, int i) + { + var @this = (FunctionDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetArgument(unchecked((uint)i))); + } + + private static unsafe TemplateArgument TemplateSpecializationArgsFactory(object self, int i) + { + var @this = (FunctionDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetTemplateArgument(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/FunctionTemplateDecl.cs b/sources/ClangSharp/Cursors/Decls/FunctionTemplateDecl.cs index 3bcac8d5..9a0325e4 100644 --- a/sources/ClangSharp/Cursors/Decls/FunctionTemplateDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/FunctionTemplateDecl.cs @@ -12,10 +12,10 @@ public sealed class FunctionTemplateDecl : RedeclarableTemplateDecl private readonly LazyList _injectedTemplateArgs; private readonly LazyList _specializations; - internal FunctionTemplateDecl(CXCursor handle) : base(handle, CXCursor_FunctionTemplate, CX_DeclKind_FunctionTemplate) + internal unsafe FunctionTemplateDecl(CXCursor handle) : base(handle, CXCursor_FunctionTemplate, CX_DeclKind_FunctionTemplate) { - _injectedTemplateArgs = LazyList.Create(Handle.NumTemplateArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetTemplateArgument(unchecked((uint)i)))); - _specializations = LazyList.Create(Handle.NumSpecializations, (i) => TranslationUnit.GetOrCreate(Handle.GetSpecialization(unchecked((uint)i)))); + _injectedTemplateArgs = LazyList.Create(this, Handle.NumTemplateArguments, &InjectedTemplateArgsFactory); + _specializations = LazyList.Create(this, Handle.NumSpecializations, &SpecializationsFactory); } public new FunctionTemplateDecl CanonicalDecl => (FunctionTemplateDecl)base.CanonicalDecl; @@ -33,4 +33,16 @@ internal FunctionTemplateDecl(CXCursor handle) : base(handle, CXCursor_FunctionT public IReadOnlyList Specializations => _specializations; public new FunctionDecl TemplatedDecl => (FunctionDecl)base.TemplatedDecl; + + private static unsafe TemplateArgument InjectedTemplateArgsFactory(object self, int i) + { + var @this = (FunctionTemplateDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetTemplateArgument(unchecked((uint)i))); + } + + private static unsafe FunctionDecl SpecializationsFactory(object self, int i) + { + var @this = (FunctionTemplateDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetSpecialization(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/ImplicitConceptSpecializationDecl.cs b/sources/ClangSharp/Cursors/Decls/ImplicitConceptSpecializationDecl.cs index 9d6fe8b7..709cff3e 100644 --- a/sources/ClangSharp/Cursors/Decls/ImplicitConceptSpecializationDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/ImplicitConceptSpecializationDecl.cs @@ -11,12 +11,18 @@ public sealed class ImplicitConceptSpecializationDecl : Decl { private readonly LazyList _templateArgs; - internal ImplicitConceptSpecializationDecl(CXCursor handle) : base(handle, CXCursor_UnexposedDecl, CX_DeclKind_ImplicitConceptSpecialization) + internal unsafe ImplicitConceptSpecializationDecl(CXCursor handle) : base(handle, CXCursor_UnexposedDecl, CX_DeclKind_ImplicitConceptSpecialization) { - _templateArgs = LazyList.Create(Handle.NumTemplateArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetTemplateArgumentLoc(unchecked((uint)i)))); + _templateArgs = LazyList.Create(this, Handle.NumTemplateArguments, &TemplateArgsFactory); } public uint NumTemplateArgs => unchecked((uint)Handle.NumTemplateArguments); public IReadOnlyList TemplateArgs => _templateArgs; + + private static unsafe TemplateArgumentLoc TemplateArgsFactory(object self, int i) + { + var @this = (ImplicitConceptSpecializationDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetTemplateArgumentLoc(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/NonTypeTemplateParmDecl.cs b/sources/ClangSharp/Cursors/Decls/NonTypeTemplateParmDecl.cs index 70375d86..9f0aefb8 100644 --- a/sources/ClangSharp/Cursors/Decls/NonTypeTemplateParmDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/NonTypeTemplateParmDecl.cs @@ -16,9 +16,9 @@ public sealed class NonTypeTemplateParmDecl : DeclaratorDecl, ITemplateParmPosit internal unsafe NonTypeTemplateParmDecl(CXCursor handle) : base(handle, CXCursor_NonTypeTemplateParameter, CX_DeclKind_NonTypeTemplateParm) { - _associatedConstraints = LazyList.Create(Handle.NumAssociatedConstraints, (i) => TranslationUnit.GetOrCreate(Handle.GetAssociatedConstraint(unchecked((uint)i)))); + _associatedConstraints = LazyList.Create(this, Handle.NumAssociatedConstraints, &AssociatedConstraintsFactory); _defaultArgument = new ValueLazy(&DefaultArgumentFactory); - _expansionTypes = LazyList.Create(Handle.NumExpansionTypes, (i) => TranslationUnit.GetOrCreate(Handle.GetExpansionType(unchecked((uint)i)))); + _expansionTypes = LazyList.Create(this, Handle.NumExpansionTypes, &ExpansionTypesFactory); _placeholderTypeConstraint = new ValueLazy(&PlaceholderTypeConstraintFactory); } @@ -49,4 +49,16 @@ internal unsafe NonTypeTemplateParmDecl(CXCursor handle) : base(handle, CXCursor private static unsafe Expr PlaceholderTypeConstraintFactory(NonTypeTemplateParmDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.PlaceholderTypeConstraint); private static unsafe Expr DefaultArgumentFactory(NonTypeTemplateParmDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.DefaultArg); + + private static unsafe Expr AssociatedConstraintsFactory(object self, int i) + { + var @this = (NonTypeTemplateParmDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetAssociatedConstraint(unchecked((uint)i))); + } + + private static unsafe Type ExpansionTypesFactory(object self, int i) + { + var @this = (NonTypeTemplateParmDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetExpansionType(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/ObjCCategoryDecl.cs b/sources/ClangSharp/Cursors/Decls/ObjCCategoryDecl.cs index f3c9aa5f..b68a7f11 100644 --- a/sources/ClangSharp/Cursors/Decls/ObjCCategoryDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/ObjCCategoryDecl.cs @@ -25,8 +25,8 @@ internal unsafe ObjCCategoryDecl(CXCursor handle) : base(handle, CXCursor_ObjCCa _ivars = new ValueLazy>(&IvarsFactory); _nextClassCategory = new ValueLazy(&NextClassCategoryFactory); _nextClassCategoryRaw = new ValueLazy(&NextClassCategoryRawFactory); - _protocols = LazyList.Create(Handle.NumProtocols, (i) => TranslationUnit.GetOrCreate(Handle.GetProtocol(unchecked((uint)i)))); - _typeParamList = LazyList.Create(Handle.NumTypeParams, (i) => TranslationUnit.GetOrCreate(Handle.GetTypeParam(unchecked((uint)i)))); + _protocols = LazyList.Create(this, Handle.NumProtocols, &ProtocolsFactory); + _typeParamList = LazyList.Create(this, Handle.NumTypeParams, &TypeParamListFactory); } public ObjCInterfaceDecl ClassInterface => _classInterface.GetValue(this); @@ -56,4 +56,16 @@ internal unsafe ObjCCategoryDecl(CXCursor handle) : base(handle, CXCursor_ObjCCa private static unsafe ObjCCategoryImplDecl ImplementationFactory(ObjCCategoryDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.GetSubDecl(1)); private static unsafe ObjCInterfaceDecl ClassInterfaceFactory(ObjCCategoryDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.GetSubDecl(0)); + + private static unsafe ObjCProtocolDecl ProtocolsFactory(object self, int i) + { + var @this = (ObjCCategoryDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetProtocol(unchecked((uint)i))); + } + + private static unsafe ObjCTypeParamDecl TypeParamListFactory(object self, int i) + { + var @this = (ObjCCategoryDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetTypeParam(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/ObjCImplementationDecl.cs b/sources/ClangSharp/Cursors/Decls/ObjCImplementationDecl.cs index f52301fa..493231f9 100644 --- a/sources/ClangSharp/Cursors/Decls/ObjCImplementationDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/ObjCImplementationDecl.cs @@ -16,7 +16,7 @@ public sealed class ObjCImplementationDecl : ObjCImplDecl internal unsafe ObjCImplementationDecl(CXCursor handle) : base(handle, CXCursor_ObjCImplementationDecl, CX_DeclKind_ObjCImplementation) { - _initExprs = LazyList.Create(Handle.NumExprs, (i) => TranslationUnit.GetOrCreate(Handle.GetExpr(unchecked((uint)i)))); + _initExprs = LazyList.Create(this, Handle.NumExprs, &InitExprsFactory); _ivars = new ValueLazy>(&IvarsFactory); _superClass = new ValueLazy(&SuperClassFactory); } @@ -32,4 +32,10 @@ internal unsafe ObjCImplementationDecl(CXCursor handle) : base(handle, CXCursor_ private static unsafe ObjCInterfaceDecl SuperClassFactory(ObjCImplementationDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.GetSubDecl(1)); private static unsafe List IvarsFactory(ObjCImplementationDecl self) => [.. self.Decls.OfType()]; + + private static unsafe Expr InitExprsFactory(object self, int i) + { + var @this = (ObjCImplementationDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetExpr(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/ObjCInterfaceDecl.cs b/sources/ClangSharp/Cursors/Decls/ObjCInterfaceDecl.cs index d73eb7e2..96ff4a80 100644 --- a/sources/ClangSharp/Cursors/Decls/ObjCInterfaceDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/ObjCInterfaceDecl.cs @@ -31,11 +31,11 @@ internal unsafe ObjCInterfaceDecl(CXCursor handle) : base(handle, CXCursor_ObjCI _implementation = new ValueLazy(&ImplementationFactory); _ivars = new ValueLazy>(&IvarsFactory); _knownExtensions = new ValueLazy>(&KnownExtensionsFactory); - _protocols = LazyList.Create(Handle.NumProtocols, (i) => TranslationUnit.GetOrCreate(Handle.GetProtocol(unchecked((uint)i)))); + _protocols = LazyList.Create(this, Handle.NumProtocols, &ProtocolsFactory); _superClass = new ValueLazy(&SuperClassFactory); _superClassType = new ValueLazy(&SuperClassTypeFactory); _typeForDecl = new ValueLazy(&TypeForDeclFactory); - _typeParamList = LazyList.Create(Handle.NumTypeParams, (i) => TranslationUnit.GetOrCreate(Handle.GetTypeParam(unchecked((uint)i)))); + _typeParamList = LazyList.Create(this, Handle.NumTypeParams, &TypeParamListFactory); _visibleCategories = new ValueLazy>(&VisibleCategoriesFactory); _visibleExtensions = new ValueLazy>(&VisibleExtensionsFactory); } @@ -103,4 +103,16 @@ private static unsafe List CategoryListFactory(ObjCInterfaceDe return categories; } + + private static unsafe ObjCProtocolDecl ProtocolsFactory(object self, int i) + { + var @this = (ObjCInterfaceDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetProtocol(unchecked((uint)i))); + } + + private static unsafe ObjCTypeParamDecl TypeParamListFactory(object self, int i) + { + var @this = (ObjCInterfaceDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetTypeParam(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/ObjCMethodDecl.cs b/sources/ClangSharp/Cursors/Decls/ObjCMethodDecl.cs index 13959314..0bf71334 100644 --- a/sources/ClangSharp/Cursors/Decls/ObjCMethodDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/ObjCMethodDecl.cs @@ -26,7 +26,7 @@ internal unsafe ObjCMethodDecl(CXCursor handle) : base(handle, handle.Kind, CX_D _classInterface = new ValueLazy(&ClassInterfaceFactory); _cmdDecl = new ValueLazy(&CmdDeclFactory); - _parameters = LazyList.Create(Handle.NumArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetArgument(unchecked((uint)i)))); + _parameters = LazyList.Create(this, Handle.NumArguments, &ParametersFactory); _selfDecl = new ValueLazy(&SelfDeclFactory); _returnType = new ValueLazy(&ReturnTypeFactory); _sendResultType = new ValueLazy(&SendResultTypeFactory); @@ -69,4 +69,10 @@ internal unsafe ObjCMethodDecl(CXCursor handle) : base(handle, handle.Kind, CX_D private static unsafe ImplicitParamDecl CmdDeclFactory(ObjCMethodDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.GetSubDecl(1)); private static unsafe ObjCInterfaceDecl ClassInterfaceFactory(ObjCMethodDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.GetSubDecl(0)); + + private static unsafe ParmVarDecl ParametersFactory(object self, int i) + { + var @this = (ObjCMethodDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetArgument(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/ObjCProtocolDecl.cs b/sources/ClangSharp/Cursors/Decls/ObjCProtocolDecl.cs index a3d140f2..240394e2 100644 --- a/sources/ClangSharp/Cursors/Decls/ObjCProtocolDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/ObjCProtocolDecl.cs @@ -15,7 +15,7 @@ public sealed class ObjCProtocolDecl : ObjCContainerDecl, IRedeclarable(&DefinitionFactory); - _protocols = LazyList.Create(Handle.NumProtocols, (i) => TranslationUnit.GetOrCreate(Handle.GetProtocol(unchecked((uint)i)))); + _protocols = LazyList.Create(this, Handle.NumProtocols, &ProtocolsFactory); } public new ObjCProtocolDecl CanonicalDecl => (ObjCProtocolDecl)base.CanonicalDecl; @@ -33,4 +33,10 @@ internal unsafe ObjCProtocolDecl(CXCursor handle) : base(handle, CXCursor_ObjCPr public IReadOnlyList ReferencedProtocols => Protocols; private static unsafe ObjCProtocolDecl DefinitionFactory(ObjCProtocolDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.Definition); + + private static unsafe ObjCProtocolDecl ProtocolsFactory(object self, int i) + { + var @this = (ObjCProtocolDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetProtocol(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/RecordDecl.cs b/sources/ClangSharp/Cursors/Decls/RecordDecl.cs index 264a06d0..a6086a9d 100644 --- a/sources/ClangSharp/Cursors/Decls/RecordDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/RecordDecl.cs @@ -34,7 +34,7 @@ private protected unsafe RecordDecl(CXCursor handle, CXCursorKind expectedCursor throw new ArgumentOutOfRangeException(nameof(handle)); } - _fields = LazyList.Create(Handle.NumFields, (i) => TranslationUnit.GetOrCreate(Handle.GetField(unchecked((uint)i)))); + _fields = LazyList.Create(this, Handle.NumFields, &FieldsFactory); _anonymousFields = new ValueLazy>(&AnonymousFieldsFactory); _anonymousRecords = new ValueLazy>(&AnonymousRecordsFactory); _indirectFields = new ValueLazy>(&IndirectFieldsFactory); @@ -68,4 +68,10 @@ private protected unsafe RecordDecl(CXCursor handle, CXCursorKind expectedCursor private static unsafe List AnonymousRecordsFactory(RecordDecl self) => [.. self.Decls.OfType().Where(decl => decl.IsAnonymousStructOrUnion && !decl.IsInjectedClassName)]; private static unsafe List AnonymousFieldsFactory(RecordDecl self) => [.. self.Decls.OfType().Where(decl => decl.IsAnonymousField)]; + + private static unsafe FieldDecl FieldsFactory(object self, int i) + { + var @this = (RecordDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetField(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/TagDecl.cs b/sources/ClangSharp/Cursors/Decls/TagDecl.cs index 582f447a..b0911a0d 100644 --- a/sources/ClangSharp/Cursors/Decls/TagDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/TagDecl.cs @@ -22,10 +22,7 @@ private protected TagDecl(CXCursor handle, CXCursorKind expectedCursorKind, CX_D } _definition = new ValueLazy(&DefinitionFactory); - _templateParameterLists = LazyList.Create>(Handle.NumTemplateParameterLists, (listIndex) => { - var numTemplateParameters = Handle.GetNumTemplateParameters(unchecked((uint)listIndex)); - return LazyList.Create(numTemplateParameters, (parameterIndex) => TranslationUnit.GetOrCreate(Handle.GetTemplateParameter(unchecked((uint)listIndex), unchecked((uint)parameterIndex)))); - }); + _templateParameterLists = CreateTemplateParameterLists(this); _typedefNameForAnonDecl = new ValueLazy(&TypedefNameForAnonDeclFactory); } diff --git a/sources/ClangSharp/Cursors/Decls/TemplateDecl.cs b/sources/ClangSharp/Cursors/Decls/TemplateDecl.cs index 71b3f05a..e9e85eeb 100644 --- a/sources/ClangSharp/Cursors/Decls/TemplateDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/TemplateDecl.cs @@ -20,9 +20,9 @@ private protected unsafe TemplateDecl(CXCursor handle, CXCursorKind expectedCurs throw new ArgumentOutOfRangeException(nameof(handle)); } - _associatedConstraints = LazyList.Create(Handle.NumAssociatedConstraints, (i) => TranslationUnit.GetOrCreate(Handle.GetAssociatedConstraint(unchecked((uint)i)))); + _associatedConstraints = LazyList.Create(this, Handle.NumAssociatedConstraints, &AssociatedConstraintsFactory); _templatedDecl = new ValueLazy(&TemplatedDeclFactory); - _templateParameters = LazyList.Create(Handle.GetNumTemplateParameters(0), (i) => TranslationUnit.GetOrCreate(Handle.GetTemplateParameter(0, unchecked((uint)i)))); + _templateParameters = LazyList.Create(this, Handle.GetNumTemplateParameters(0), &TemplateParametersFactory); } public IReadOnlyList AssociatedConstraints => _associatedConstraints; @@ -34,4 +34,16 @@ private protected unsafe TemplateDecl(CXCursor handle, CXCursorKind expectedCurs public IReadOnlyList TemplateParameters => _templateParameters; private static unsafe NamedDecl TemplatedDeclFactory(TemplateDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.TemplatedDecl); + + private static unsafe Expr AssociatedConstraintsFactory(object self, int i) + { + var @this = (TemplateDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetAssociatedConstraint(unchecked((uint)i))); + } + + private static unsafe NamedDecl TemplateParametersFactory(object self, int i) + { + var @this = (TemplateDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetTemplateParameter(0, unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/TemplateTypeParmDecl.cs b/sources/ClangSharp/Cursors/Decls/TemplateTypeParmDecl.cs index f89ac4f1..12b23aaa 100644 --- a/sources/ClangSharp/Cursors/Decls/TemplateTypeParmDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/TemplateTypeParmDecl.cs @@ -15,7 +15,7 @@ public sealed class TemplateTypeParmDecl : TypeDecl internal unsafe TemplateTypeParmDecl(CXCursor handle) : base(handle, CXCursor_TemplateTypeParameter, CX_DeclKind_TemplateTypeParm) { - _associatedConstraints = LazyList.Create(Handle.NumAssociatedConstraints, (i) => TranslationUnit.GetOrCreate(Handle.GetAssociatedConstraint(unchecked((uint)i)))); + _associatedConstraints = LazyList.Create(this, Handle.NumAssociatedConstraints, &AssociatedConstraintsFactory); _defaultArgument = new ValueLazy(&DefaultArgumentFactory); } @@ -41,4 +41,10 @@ internal unsafe TemplateTypeParmDecl(CXCursor handle) : base(handle, CXCursor_Te var defaultArgType = self.Handle.DefaultArgType; return defaultArgType.kind == CXType_Invalid ? null : self.TranslationUnit.GetOrCreate(defaultArgType); } + + private static unsafe Expr AssociatedConstraintsFactory(object self, int i) + { + var @this = (TemplateTypeParmDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetAssociatedConstraint(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/VarTemplatePartialSpecializationDecl.cs b/sources/ClangSharp/Cursors/Decls/VarTemplatePartialSpecializationDecl.cs index cc651eb0..79dfd9f2 100644 --- a/sources/ClangSharp/Cursors/Decls/VarTemplatePartialSpecializationDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/VarTemplatePartialSpecializationDecl.cs @@ -15,9 +15,9 @@ public class VarTemplatePartialSpecializationDecl : VarDecl internal unsafe VarTemplatePartialSpecializationDecl(CXCursor handle) : base(handle, CXCursor_UnexposedDecl, CX_DeclKind_VarTemplatePartialSpecialization) { - _associatedConstraints = LazyList.Create(Handle.NumAssociatedConstraints, (i) => TranslationUnit.GetOrCreate(Handle.GetAssociatedConstraint(unchecked((uint)i)))); + _associatedConstraints = LazyList.Create(this, Handle.NumAssociatedConstraints, &AssociatedConstraintsFactory); _instantiatedFromMember = new ValueLazy(&InstantiatedFromMemberFactory); - _templateParameters = LazyList.Create(Handle.GetNumTemplateParameters(0), (i) => TranslationUnit.GetOrCreate(Handle.GetTemplateParameter(0, unchecked((uint)i)))); + _templateParameters = LazyList.Create(this, Handle.GetNumTemplateParameters(0), &TemplateParametersFactory); } public IReadOnlyList AssociatedConstraints => _associatedConstraints; @@ -29,4 +29,16 @@ internal unsafe VarTemplatePartialSpecializationDecl(CXCursor handle) : base(han public IReadOnlyList TemplateParameters => _templateParameters; private static unsafe VarTemplatePartialSpecializationDecl InstantiatedFromMemberFactory(VarTemplatePartialSpecializationDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.InstantiatedFromMember); + + private static unsafe Expr AssociatedConstraintsFactory(object self, int i) + { + var @this = (VarTemplatePartialSpecializationDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetAssociatedConstraint(unchecked((uint)i))); + } + + private static unsafe NamedDecl TemplateParametersFactory(object self, int i) + { + var @this = (VarTemplatePartialSpecializationDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetTemplateParameter(0, unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Decls/VarTemplateSpecializationDecl.cs b/sources/ClangSharp/Cursors/Decls/VarTemplateSpecializationDecl.cs index 7baa43b8..fc651da5 100644 --- a/sources/ClangSharp/Cursors/Decls/VarTemplateSpecializationDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/VarTemplateSpecializationDecl.cs @@ -25,7 +25,7 @@ private protected unsafe VarTemplateSpecializationDecl(CXCursor handle, CXCursor } _specializedTemplate = new ValueLazy(&SpecializedTemplateFactory); - _templateArgs = LazyList.Create(Handle.NumTemplateArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetTemplateArgument(unchecked((uint)i)))); + _templateArgs = LazyList.Create(this, Handle.NumTemplateArguments, &TemplateArgsFactory); } public new VarTemplateSpecializationDecl MostRecentDecl => (VarTemplateSpecializationDecl)base.MostRecentDecl; @@ -35,4 +35,10 @@ private protected unsafe VarTemplateSpecializationDecl(CXCursor handle, CXCursor public IReadOnlyList TemplateArgs => _templateArgs; private static unsafe VarTemplateDecl SpecializedTemplateFactory(VarTemplateSpecializationDecl self) => self.TranslationUnit.GetOrCreate(self.Handle.SpecializedCursorTemplate); + + private static unsafe TemplateArgument TemplateArgsFactory(object self, int i) + { + var @this = (VarTemplateSpecializationDecl)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetTemplateArgument(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Exprs/CXXDependentScopeMemberExpr.cs b/sources/ClangSharp/Cursors/Exprs/CXXDependentScopeMemberExpr.cs index d7bd3be9..9bdcbc62 100644 --- a/sources/ClangSharp/Cursors/Exprs/CXXDependentScopeMemberExpr.cs +++ b/sources/ClangSharp/Cursors/Exprs/CXXDependentScopeMemberExpr.cs @@ -21,7 +21,7 @@ internal unsafe CXXDependentScopeMemberExpr(CXCursor handle) : base(handle, CXCu _baseType = new ValueLazy(&BaseTypeFactory); _firstQualifierFoundInScope = new ValueLazy(&FirstQualifierFoundInScopeFactory); - _templateArgs = LazyList.Create(Handle.NumTemplateArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetTemplateArgumentLoc(unchecked((uint)i)))); + _templateArgs = LazyList.Create(this, Handle.NumTemplateArguments, &TemplateArgsFactory); } public Expr? Base => (Expr?)Children.SingleOrDefault(); @@ -47,4 +47,10 @@ internal unsafe CXXDependentScopeMemberExpr(CXCursor handle) : base(handle, CXCu private static unsafe NamedDecl FirstQualifierFoundInScopeFactory(CXXDependentScopeMemberExpr self) => self.TranslationUnit.GetOrCreate(self.Handle.Referenced); private static unsafe Type BaseTypeFactory(CXXDependentScopeMemberExpr self) => self.TranslationUnit.GetOrCreate(self.Handle.TypeOperand); + + private static unsafe TemplateArgumentLoc TemplateArgsFactory(object self, int i) + { + var @this = (CXXDependentScopeMemberExpr)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetTemplateArgumentLoc(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Exprs/CXXParenListInitExpr.cs b/sources/ClangSharp/Cursors/Exprs/CXXParenListInitExpr.cs index 9a4a5f46..0ad9d62a 100644 --- a/sources/ClangSharp/Cursors/Exprs/CXXParenListInitExpr.cs +++ b/sources/ClangSharp/Cursors/Exprs/CXXParenListInitExpr.cs @@ -16,8 +16,8 @@ public sealed class CXXParenListInitExpr : Expr internal unsafe CXXParenListInitExpr(CXCursor handle) : base(handle, CXCursor_CXXParenListInitExpr, CX_StmtClass_CXXParenListInitExpr) { _arrayFillerOrUnionFieldInit = new ValueLazy(&ArrayFillerOrUnionFieldInitFactory); - _initExprs = LazyList.Create(Handle.NumExprs, (i) => TranslationUnit.GetOrCreate(Handle.GetExpr(unchecked((uint)i)))); - _userSpecifiedInitExprs = LazyList.Create(Handle.NumExprsOther, (i) => TranslationUnit.GetOrCreate(Handle.GetExpr(unchecked((uint)i)))); + _initExprs = LazyList.Create(this, Handle.NumExprs, &InitExprsFactory); + _userSpecifiedInitExprs = LazyList.Create(this, Handle.NumExprsOther, &UserSpecifiedInitExprsFactory); } public Expr? ArrayFiller => _arrayFillerOrUnionFieldInit.GetValue(this) as Expr; @@ -29,4 +29,16 @@ internal unsafe CXXParenListInitExpr(CXCursor handle) : base(handle, CXCursor_CX public IReadOnlyList UserSpecifiedInitExprs => _userSpecifiedInitExprs; private static unsafe Cursor ArrayFillerOrUnionFieldInitFactory(CXXParenListInitExpr self) => self.TranslationUnit.GetOrCreate(self.Handle.SubExpr); + + private static unsafe Expr InitExprsFactory(object self, int i) + { + var @this = (CXXParenListInitExpr)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetExpr(unchecked((uint)i))); + } + + private static unsafe Expr UserSpecifiedInitExprsFactory(object self, int i) + { + var @this = (CXXParenListInitExpr)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetExpr(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Exprs/CastExpr.cs b/sources/ClangSharp/Cursors/Exprs/CastExpr.cs index b4709db3..b4be80bf 100644 --- a/sources/ClangSharp/Cursors/Exprs/CastExpr.cs +++ b/sources/ClangSharp/Cursors/Exprs/CastExpr.cs @@ -23,7 +23,7 @@ private protected unsafe CastExpr(CXCursor handle, CXCursorKind expectedCursorKi Debug.Assert(NumChildren is 1); - _path = LazyList.Create(Handle.NumArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetArgument(unchecked((uint)i)))); + _path = LazyList.Create(this, Handle.NumArguments, &PathFactory); _targetUnionField = new ValueLazy(&TargetUnionFieldFactory); } @@ -106,4 +106,10 @@ public Expr SubExprAsWritten public FieldDecl TargetUnionField => _targetUnionField.GetValue(this); private static unsafe FieldDecl TargetUnionFieldFactory(CastExpr self) => self.TranslationUnit.GetOrCreate(self.Handle.TargetUnionField); + + private static unsafe CXXBaseSpecifier PathFactory(object self, int i) + { + var @this = (CastExpr)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetArgument(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Exprs/DeclRefExpr.cs b/sources/ClangSharp/Cursors/Exprs/DeclRefExpr.cs index b6330f32..196378c0 100644 --- a/sources/ClangSharp/Cursors/Exprs/DeclRefExpr.cs +++ b/sources/ClangSharp/Cursors/Exprs/DeclRefExpr.cs @@ -26,7 +26,7 @@ internal unsafe DeclRefExpr(CXCursor handle) : base(handle, handle.Kind, CX_Stmt _decl = new ValueLazy(&DeclFactory); _foundDecl = new ValueLazy(&FoundDeclFactory); - _templateArgs = LazyList.Create(Handle.NumTemplateArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetTemplateArgumentLoc(unchecked((uint)i)))); + _templateArgs = LazyList.Create(this, Handle.NumTemplateArguments, &TemplateArgsFactory); } public ValueDecl Decl => _decl.GetValue(this); @@ -48,4 +48,10 @@ internal unsafe DeclRefExpr(CXCursor handle) : base(handle, handle.Kind, CX_Stmt private static unsafe NamedDecl FoundDeclFactory(DeclRefExpr self) => self.TranslationUnit.GetOrCreate(self.Handle.FoundDecl); private static unsafe ValueDecl DeclFactory(DeclRefExpr self) => self.TranslationUnit.GetOrCreate(self.Handle.Referenced); + + private static unsafe TemplateArgumentLoc TemplateArgsFactory(object self, int i) + { + var @this = (DeclRefExpr)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetTemplateArgumentLoc(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Exprs/DependentScopeDeclRefExpr.cs b/sources/ClangSharp/Cursors/Exprs/DependentScopeDeclRefExpr.cs index 793df4cf..582ed794 100644 --- a/sources/ClangSharp/Cursors/Exprs/DependentScopeDeclRefExpr.cs +++ b/sources/ClangSharp/Cursors/Exprs/DependentScopeDeclRefExpr.cs @@ -12,10 +12,10 @@ public sealed class DependentScopeDeclRefExpr : Expr { private readonly LazyList _templateArgs; - internal DependentScopeDeclRefExpr(CXCursor handle) : base(handle, CXCursor_DeclRefExpr, CX_StmtClass_DependentScopeDeclRefExpr) + internal unsafe DependentScopeDeclRefExpr(CXCursor handle) : base(handle, CXCursor_DeclRefExpr, CX_StmtClass_DependentScopeDeclRefExpr) { Debug.Assert(NumChildren is 0); - _templateArgs = LazyList.Create(Handle.NumTemplateArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetTemplateArgumentLoc(unchecked((uint)i)))); + _templateArgs = LazyList.Create(this, Handle.NumTemplateArguments, &TemplateArgsFactory); } public string DeclName => Handle.Name.CString; @@ -25,4 +25,10 @@ internal DependentScopeDeclRefExpr(CXCursor handle) : base(handle, CXCursor_Decl public bool HasTemplateKeyword => Handle.HasTemplateKeyword; public IReadOnlyList TemplateArgs => _templateArgs; + + private static unsafe TemplateArgumentLoc TemplateArgsFactory(object self, int i) + { + var @this = (DependentScopeDeclRefExpr)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetTemplateArgumentLoc(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Exprs/ExprWithCleanups.cs b/sources/ClangSharp/Cursors/Exprs/ExprWithCleanups.cs index 648cd5a4..4d63f1d7 100644 --- a/sources/ClangSharp/Cursors/Exprs/ExprWithCleanups.cs +++ b/sources/ClangSharp/Cursors/Exprs/ExprWithCleanups.cs @@ -11,12 +11,18 @@ public sealed class ExprWithCleanups : FullExpr { private readonly LazyList _objects; - internal ExprWithCleanups(CXCursor handle) : base(handle, CXCursor_UnexposedExpr, CX_StmtClass_ExprWithCleanups) + internal unsafe ExprWithCleanups(CXCursor handle) : base(handle, CXCursor_UnexposedExpr, CX_StmtClass_ExprWithCleanups) { - _objects = LazyList.Create(Handle.NumArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetArgument(unchecked((uint)i)))); + _objects = LazyList.Create(this, Handle.NumArguments, &ObjectsFactory); } public uint NumObjects => unchecked((uint)Handle.NumArguments); public IReadOnlyList Objects => _objects; + + private static unsafe Cursor ObjectsFactory(object self, int i) + { + var @this = (ExprWithCleanups)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetArgument(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Exprs/FunctionParmPackExpr.cs b/sources/ClangSharp/Cursors/Exprs/FunctionParmPackExpr.cs index 16f77c96..58cbe3c6 100644 --- a/sources/ClangSharp/Cursors/Exprs/FunctionParmPackExpr.cs +++ b/sources/ClangSharp/Cursors/Exprs/FunctionParmPackExpr.cs @@ -17,7 +17,7 @@ internal unsafe FunctionParmPackExpr(CXCursor handle) : base(handle, CXCursor_De { Debug.Assert(NumChildren is 0); - _expansions = LazyList.Create(Handle.NumDecls, (i) => TranslationUnit.GetOrCreate(Handle.GetDecl(unchecked((uint)i)))); + _expansions = LazyList.Create(this, Handle.NumDecls, &ExpansionsFactory); _parameterPack = new ValueLazy(&ParameterPackFactory); } @@ -28,4 +28,10 @@ internal unsafe FunctionParmPackExpr(CXCursor handle) : base(handle, CXCursor_De public VarDecl ParameterPack => _parameterPack.GetValue(this); private static unsafe VarDecl ParameterPackFactory(FunctionParmPackExpr self) => self.TranslationUnit.GetOrCreate(self.Handle.Referenced); + + private static unsafe VarDecl ExpansionsFactory(object self, int i) + { + var @this = (FunctionParmPackExpr)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetDecl(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Exprs/MemberExpr.cs b/sources/ClangSharp/Cursors/Exprs/MemberExpr.cs index 0bd9604e..37fcb255 100644 --- a/sources/ClangSharp/Cursors/Exprs/MemberExpr.cs +++ b/sources/ClangSharp/Cursors/Exprs/MemberExpr.cs @@ -18,7 +18,7 @@ internal unsafe MemberExpr(CXCursor handle) : base(handle, CXCursor_MemberRefExp Debug.Assert(NumChildren is 1); _memberDecl = new ValueLazy(&MemberDeclFactory); - _templateArgs = LazyList.Create(Handle.NumTemplateArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetTemplateArgumentLoc(unchecked((uint)i)))); + _templateArgs = LazyList.Create(this, Handle.NumTemplateArguments, &TemplateArgsFactory); } public Expr Base => (Expr)Children[0]; @@ -42,4 +42,10 @@ internal unsafe MemberExpr(CXCursor handle) : base(handle, CXCursor_MemberRefExp public IReadOnlyList TemplateArgs => _templateArgs; private static unsafe ValueDecl MemberDeclFactory(MemberExpr self) => self.TranslationUnit.GetOrCreate(self.Handle.Referenced); + + private static unsafe TemplateArgumentLoc TemplateArgsFactory(object self, int i) + { + var @this = (MemberExpr)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetTemplateArgumentLoc(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Exprs/ObjCMessageExpr.cs b/sources/ClangSharp/Cursors/Exprs/ObjCMessageExpr.cs index 833cf27f..2c0eef6b 100644 --- a/sources/ClangSharp/Cursors/Exprs/ObjCMessageExpr.cs +++ b/sources/ClangSharp/Cursors/Exprs/ObjCMessageExpr.cs @@ -18,7 +18,7 @@ public sealed class ObjCMessageExpr : Expr internal unsafe ObjCMessageExpr(CXCursor handle) : base(handle, CXCursor_ObjCMessageExpr, CX_StmtClass_ObjCMessageExpr) { - _args = LazyList.Create(Handle.NumArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetArgument(unchecked((uint)i)))); + _args = LazyList.Create(this, Handle.NumArguments, &ArgsFactory); _classReceiver = new ValueLazy(&ClassReceiverFactory); _instanceReceiver = new ValueLazy(&InstanceReceiverFactory); _methodDecl = new ValueLazy(&MethodDeclFactory); @@ -51,4 +51,10 @@ internal unsafe ObjCMessageExpr(CXCursor handle) : base(handle, CXCursor_ObjCMes private static unsafe Expr InstanceReceiverFactory(ObjCMessageExpr self) => self.TranslationUnit.GetOrCreate(self.Handle.GetExpr(0)); private static unsafe Type ClassReceiverFactory(ObjCMessageExpr self) => self.TranslationUnit.GetOrCreate(self.Handle.TypeOperand); + + private static unsafe Expr ArgsFactory(object self, int i) + { + var @this = (ObjCMessageExpr)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetArgument(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Exprs/OverloadExpr.cs b/sources/ClangSharp/Cursors/Exprs/OverloadExpr.cs index c11545fe..9f617ba6 100644 --- a/sources/ClangSharp/Cursors/Exprs/OverloadExpr.cs +++ b/sources/ClangSharp/Cursors/Exprs/OverloadExpr.cs @@ -20,9 +20,9 @@ private protected unsafe OverloadExpr(CXCursor handle, CXCursorKind expectedCurs throw new ArgumentOutOfRangeException(nameof(handle)); } - _decls = LazyList.Create(Handle.NumDecls, (i) => TranslationUnit.GetOrCreate(Handle.GetDecl(unchecked((uint)i)))); + _decls = LazyList.Create(this, Handle.NumDecls, &DeclsFactory); _namingClass = new ValueLazy(&NamingClassFactory); - _templateArgs = LazyList.Create(Handle.NumTemplateArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetTemplateArgumentLoc(unchecked((uint)i)))); + _templateArgs = LazyList.Create(this, Handle.NumTemplateArguments, &TemplateArgsFactory); } public IReadOnlyList Decls => _decls; @@ -42,4 +42,16 @@ private protected unsafe OverloadExpr(CXCursor handle, CXCursorKind expectedCurs public IReadOnlyList TemplateArgs => _templateArgs; private static unsafe CXXRecordDecl NamingClassFactory(OverloadExpr self) => self.TranslationUnit.GetOrCreate(self.Handle.Referenced); + + private static unsafe Decl DeclsFactory(object self, int i) + { + var @this = (OverloadExpr)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetDecl(unchecked((uint)i))); + } + + private static unsafe TemplateArgumentLoc TemplateArgsFactory(object self, int i) + { + var @this = (OverloadExpr)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetTemplateArgumentLoc(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Exprs/SizeOfPackExpr.cs b/sources/ClangSharp/Cursors/Exprs/SizeOfPackExpr.cs index d1a45d7b..e91f952e 100644 --- a/sources/ClangSharp/Cursors/Exprs/SizeOfPackExpr.cs +++ b/sources/ClangSharp/Cursors/Exprs/SizeOfPackExpr.cs @@ -15,7 +15,7 @@ public sealed class SizeOfPackExpr : Expr internal unsafe SizeOfPackExpr(CXCursor handle) : base(handle, CXCursor_SizeOfPackExpr, CX_StmtClass_SizeOfPackExpr) { _pack = new ValueLazy(&PackFactory); - _partialArguments = LazyList.Create(Handle.NumTemplateArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetTemplateArgument(unchecked((uint)i)))); + _partialArguments = LazyList.Create(this, Handle.NumTemplateArguments, &PartialArgumentsFactory); } public NamedDecl Pack => _pack.GetValue(this); @@ -27,4 +27,10 @@ internal unsafe SizeOfPackExpr(CXCursor handle) : base(handle, CXCursor_SizeOfPa public IReadOnlyList PartialArguments => _partialArguments; private static unsafe NamedDecl PackFactory(SizeOfPackExpr self) => self.TranslationUnit.GetOrCreate(self.Handle.Referenced); + + private static unsafe TemplateArgument PartialArgumentsFactory(object self, int i) + { + var @this = (SizeOfPackExpr)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetTemplateArgument(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Stmts/AttributedStmt.cs b/sources/ClangSharp/Cursors/Stmts/AttributedStmt.cs index 7ecb9250..c13d52ee 100644 --- a/sources/ClangSharp/Cursors/Stmts/AttributedStmt.cs +++ b/sources/ClangSharp/Cursors/Stmts/AttributedStmt.cs @@ -12,14 +12,20 @@ public sealed class AttributedStmt : ValueStmt { private readonly LazyList _attrs; - internal AttributedStmt(CXCursor handle) : base(handle, CXCursor_UnexposedStmt, CX_StmtClass_AttributedStmt) + internal unsafe AttributedStmt(CXCursor handle) : base(handle, CXCursor_UnexposedStmt, CX_StmtClass_AttributedStmt) { Debug.Assert(NumChildren == 1); - _attrs = LazyList.Create(Handle.NumAttrs, (i) => TranslationUnit.GetOrCreate(Handle.GetAttr(unchecked((uint)i)))); + _attrs = LazyList.Create(this, Handle.NumAttrs, &AttrsFactory); } public IReadOnlyList Attrs => _attrs; public Stmt SubStmt => Children[0]; + + private static unsafe Attr AttrsFactory(object self, int i) + { + var @this = (AttributedStmt)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetAttr(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Stmts/CapturedStmt.cs b/sources/ClangSharp/Cursors/Stmts/CapturedStmt.cs index d296f7b1..6692a86f 100644 --- a/sources/ClangSharp/Cursors/Stmts/CapturedStmt.cs +++ b/sources/ClangSharp/Cursors/Stmts/CapturedStmt.cs @@ -20,7 +20,7 @@ internal unsafe CapturedStmt(CXCursor handle) : base(handle, CXCursor_UnexposedS _capturedDecl = new ValueLazy(&CapturedDeclFactory); _capturedRecordDecl = new ValueLazy(&CapturedRecordDeclFactory); _captureStmt = new ValueLazy(&CaptureStmtFactory); - _captures = LazyList.Create(Handle.NumCaptures, (i) => new Capture(this, unchecked((uint)i))); + _captures = LazyList.Create(this, Handle.NumCaptures, &CapturesFactory); _captureInits = LazyList.Create(_children); } @@ -62,4 +62,10 @@ public bool CapturesVariable(VarDecl var) private static unsafe RecordDecl CapturedRecordDeclFactory(CapturedStmt self) => self.TranslationUnit.GetOrCreate(self.Handle.CapturedRecordDecl); private static unsafe CapturedDecl CapturedDeclFactory(CapturedStmt self) => self.TranslationUnit.GetOrCreate(self.Handle.CapturedDecl); + + private static unsafe Capture CapturesFactory(object self, int i) + { + var @this = (CapturedStmt)self; + return new Capture(@this, unchecked((uint)i)); + } } diff --git a/sources/ClangSharp/Cursors/Stmts/DeclStmt.cs b/sources/ClangSharp/Cursors/Stmts/DeclStmt.cs index c0d4f3c0..9bf2e412 100644 --- a/sources/ClangSharp/Cursors/Stmts/DeclStmt.cs +++ b/sources/ClangSharp/Cursors/Stmts/DeclStmt.cs @@ -12,9 +12,9 @@ public sealed class DeclStmt : Stmt { private readonly LazyList _decls; - internal DeclStmt(CXCursor handle) : base(handle, CXCursor_DeclStmt, CX_StmtClass_DeclStmt) + internal unsafe DeclStmt(CXCursor handle) : base(handle, CXCursor_DeclStmt, CX_StmtClass_DeclStmt) { - _decls = LazyList.Create(Handle.NumDecls, (i) => TranslationUnit.GetOrCreate(Handle.GetDecl(unchecked((uint)i)))); + _decls = LazyList.Create(this, Handle.NumDecls, &DeclsFactory); } public IReadOnlyList Decls => _decls; @@ -22,4 +22,10 @@ internal DeclStmt(CXCursor handle) : base(handle, CXCursor_DeclStmt, CX_StmtClas public bool IsSingleDecl => Decls.Count == 1; public Decl? SingleDecl => Decls.SingleOrDefault(); + + private static unsafe Decl DeclsFactory(object self, int i) + { + var @this = (DeclStmt)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetDecl(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Cursors/Stmts/Stmt.cs b/sources/ClangSharp/Cursors/Stmts/Stmt.cs index 4feb0343..b37b942a 100644 --- a/sources/ClangSharp/Cursors/Stmts/Stmt.cs +++ b/sources/ClangSharp/Cursors/Stmts/Stmt.cs @@ -20,10 +20,7 @@ private protected unsafe Stmt(CXCursor handle, CXCursorKind expectedCursorKind, throw new ArgumentOutOfRangeException(nameof(handle)); } - _children = LazyList.Create(Handle.NumChildren, (i) => { - var childHandle = Handle.GetChild(unchecked((uint)i)); - return !childHandle.IsNull ? TranslationUnit.GetOrCreate(childHandle) : null!; - }); + _children = LazyList.Create(this, Handle.NumChildren, &ChildrenFactory); _declContext = new ValueLazy(&DeclContextFactory); } @@ -374,4 +371,11 @@ private static unsafe IDeclContext DeclContextFactory(Stmt self) { Debug.Assert(semanticParent is not null); return (IDeclContext)semanticParent!; } + + private static unsafe Stmt ChildrenFactory(object self, int i) + { + var @this = (Stmt)self; + var childHandle = @this.Handle.GetChild(unchecked((uint)i)); + return !childHandle.IsNull ? @this.TranslationUnit.GetOrCreate(childHandle) : null!; + } } diff --git a/sources/ClangSharp/LazyList.cs b/sources/ClangSharp/LazyList.cs index e4df8831..22322b4f 100644 --- a/sources/ClangSharp/LazyList.cs +++ b/sources/ClangSharp/LazyList.cs @@ -1,30 +1,29 @@ // Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information. -using System; using System.Diagnostics.CodeAnalysis; namespace ClangSharp; internal static class LazyList { - public static LazyList Create<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] T>(int count, Func valueFactory) + public static unsafe LazyList Create<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] T>(object self, int count, delegate* valueFactory) where T : class { if (count <= 0) { return LazyList.Empty; } - return new LazyList(count, valueFactory); + return new LazyList(self, count, valueFactory); } - public static LazyList Create<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] T>(int count, Func valueFactory) + public static unsafe LazyList Create<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] T>(object self, int count, delegate* valueFactory) where T : class { if (count <= 0) { return LazyList.Empty; } - return new LazyList(count, valueFactory); + return new LazyList(self, count, valueFactory); } public static LazyList Create(LazyList list, int skip = -1, int take = -1) diff --git a/sources/ClangSharp/LazyList`1.cs b/sources/ClangSharp/LazyList`1.cs index 5a987526..749227fb 100644 --- a/sources/ClangSharp/LazyList`1.cs +++ b/sources/ClangSharp/LazyList`1.cs @@ -7,29 +7,34 @@ namespace ClangSharp; -internal sealed class LazyList<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] T> : IList, IReadOnlyList +internal sealed unsafe class LazyList<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] T> : IList, IReadOnlyList where T : class { internal readonly T[] _items; - internal readonly Func? _valueFactory; - internal readonly Func? _valueFactoryWithPreviousValue; + internal readonly object? _self; + internal readonly delegate* _valueFactory; + internal readonly delegate* _valueFactoryWithPreviousValue; - public static readonly LazyList Empty = new LazyList(0, _ => null!); + public static readonly LazyList Empty = new LazyList(null!, 0, &EmptyFactory); - public LazyList(int count, Func valueFactory) + public LazyList(object self, int count, delegate* valueFactory) { _items = (count <= 0) ? [] : new T[count]; + _self = self; _valueFactory = valueFactory; _valueFactoryWithPreviousValue = null; } - public LazyList(int count, Func valueFactoryWithPreviousValue) + public LazyList(object self, int count, delegate* valueFactoryWithPreviousValue) { _items = (count <= 0) ? [] : new T[count]; + _self = self; _valueFactory = null; _valueFactoryWithPreviousValue = valueFactoryWithPreviousValue; } + private static T EmptyFactory(object self, int index) => null!; + public T this[int index] { get @@ -39,13 +44,13 @@ public T this[int index] if (item is null) { - if (_valueFactoryWithPreviousValue is not null) + if (_valueFactoryWithPreviousValue != null) { - item = _valueFactoryWithPreviousValue(index, index == 0 ? null : _items[index - 1]); + item = _valueFactoryWithPreviousValue(_self!, index, index == 0 ? null : _items[index - 1]); } else { - item = _valueFactory!.Invoke(index); + item = _valueFactory(_self!, index); } items[index] = item; } diff --git a/sources/ClangSharp/LazyList`2.cs b/sources/ClangSharp/LazyList`2.cs index 94351d73..cfd9ad9d 100644 --- a/sources/ClangSharp/LazyList`2.cs +++ b/sources/ClangSharp/LazyList`2.cs @@ -7,13 +7,14 @@ namespace ClangSharp; -internal sealed class LazyList : IList, IReadOnlyList +internal sealed unsafe class LazyList : IList, IReadOnlyList where T : class, TBase where TBase : class { internal readonly TBase[] _items; - internal readonly Func? _valueFactory; - internal readonly Func? _valueFactoryWithPreviousValue; + internal readonly object? _self; + internal readonly delegate* _valueFactory; + internal readonly delegate* _valueFactoryWithPreviousValue; private readonly int _start; private readonly int _count; @@ -24,6 +25,7 @@ public LazyList(LazyList list, int skip = -1, int take = -1) take = (take < 0) ? (list.Count - skip) : take; _items = list._items; + _self = list._self; _valueFactory = list._valueFactory; _valueFactoryWithPreviousValue = list._valueFactoryWithPreviousValue; @@ -41,13 +43,13 @@ public T this[int index] if (item is null) { var idx = index + _start; - if (_valueFactoryWithPreviousValue is not null) + if (_valueFactoryWithPreviousValue != null) { - item = _valueFactoryWithPreviousValue(idx, idx == 0 ? null : _items[idx - 1]); + item = _valueFactoryWithPreviousValue(_self!, idx, idx == 0 ? null : _items[idx - 1]); } else { - item = _valueFactory!.Invoke(idx); + item = _valueFactory(_self!, idx); } items[index] = item; } diff --git a/sources/ClangSharp/TemplateArgument.cs b/sources/ClangSharp/TemplateArgument.cs index 5887a835..a9ba14ea 100644 --- a/sources/ClangSharp/TemplateArgument.cs +++ b/sources/ClangSharp/TemplateArgument.cs @@ -39,7 +39,7 @@ internal TemplateArgument(CX_TemplateArgument handle) _nonTypeTemplateArgumentType = new ValueLazy(&NonTypeTemplateArgumentTypeFactory); _nullPtrType = new ValueLazy(&NullPtrTypeFactory); _packExpansionPattern = new ValueLazy(&PackExpansionPatternFactory); - _packElements = LazyList.Create(Handle.NumPackElements, (i) => TranslationUnit.GetOrCreate(Handle.GetPackElement(unchecked((uint)i)))); + _packElements = LazyList.Create(this, Handle.NumPackElements, &PackElementsFactory); _paramTypeForDecl = new ValueLazy(&ParamTypeForDeclFactory); } @@ -151,4 +151,10 @@ public void Dispose() private static unsafe ValueDecl AsDeclFactory(TemplateArgument self) => self.TranslationUnit.GetOrCreate(self.Handle.AsDecl); private static unsafe TranslationUnit TranslationUnitFactory(TemplateArgument self) => TranslationUnit.GetOrCreate(self.Handle.tu); + + private static unsafe TemplateArgument PackElementsFactory(object self, int i) + { + var @this = (TemplateArgument)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetPackElement(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Types/AutoType.cs b/sources/ClangSharp/Types/AutoType.cs index 9e1a30ea..c36255a1 100644 --- a/sources/ClangSharp/Types/AutoType.cs +++ b/sources/ClangSharp/Types/AutoType.cs @@ -11,10 +11,16 @@ public sealed class AutoType : DeducedType { private readonly LazyList _templateArgs; - internal AutoType(CXType handle) : base(handle, CXType_Auto, CX_TypeClass_Auto) + internal unsafe AutoType(CXType handle) : base(handle, CXType_Auto, CX_TypeClass_Auto) { - _templateArgs = LazyList.Create(Handle.NumTemplateArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetTemplateArgument(unchecked((uint)i)))); + _templateArgs = LazyList.Create(this, Handle.NumTemplateArguments, &TemplateArgsFactory); } public IReadOnlyList Args => _templateArgs; + + private static unsafe TemplateArgument TemplateArgsFactory(object self, int i) + { + var @this = (AutoType)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetTemplateArgument(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Types/DependentTemplateSpecializationType.cs b/sources/ClangSharp/Types/DependentTemplateSpecializationType.cs index 05957f8f..e483a2d9 100644 --- a/sources/ClangSharp/Types/DependentTemplateSpecializationType.cs +++ b/sources/ClangSharp/Types/DependentTemplateSpecializationType.cs @@ -11,10 +11,16 @@ public sealed class DependentTemplateSpecializationType : TypeWithKeyword { private readonly LazyList _templateArgs; - internal DependentTemplateSpecializationType(CXType handle) : base(handle, CXType_Unexposed, CX_TypeClass_DependentTemplateSpecialization) + internal unsafe DependentTemplateSpecializationType(CXType handle) : base(handle, CXType_Unexposed, CX_TypeClass_DependentTemplateSpecialization) { - _templateArgs = LazyList.Create(Handle.NumTemplateArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetTemplateArgument(unchecked((uint)i)))); + _templateArgs = LazyList.Create(this, Handle.NumTemplateArguments, &TemplateArgsFactory); } public IReadOnlyList Args => _templateArgs; + + private static unsafe TemplateArgument TemplateArgsFactory(object self, int i) + { + var @this = (DependentTemplateSpecializationType)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetTemplateArgument(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Types/FunctionProtoType.cs b/sources/ClangSharp/Types/FunctionProtoType.cs index 771fd8b8..8fa7000e 100644 --- a/sources/ClangSharp/Types/FunctionProtoType.cs +++ b/sources/ClangSharp/Types/FunctionProtoType.cs @@ -11,9 +11,9 @@ public sealed class FunctionProtoType : FunctionType { private readonly LazyList _paramTypes; - internal FunctionProtoType(CXType handle) : base(handle, CXType_FunctionProto, CX_TypeClass_FunctionProto) + internal unsafe FunctionProtoType(CXType handle) : base(handle, CXType_FunctionProto, CX_TypeClass_FunctionProto) { - _paramTypes = LazyList.Create(Handle.NumArgTypes, (i) => TranslationUnit.GetOrCreate(Handle.GetArgType(unchecked((uint)i)))); + _paramTypes = LazyList.Create(this, Handle.NumArgTypes, &ParamTypesFactory); } public CXCursor_ExceptionSpecificationKind ExceptionSpecType => Handle.ExceptionSpecificationType; @@ -25,4 +25,10 @@ internal FunctionProtoType(CXType handle) : base(handle, CXType_FunctionProto, C public IReadOnlyList ParamTypes => _paramTypes; public CXRefQualifierKind RefQualifier => Handle.CXXRefQualifier; + + private static unsafe Type ParamTypesFactory(object self, int i) + { + var @this = (FunctionProtoType)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetArgType(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Types/ObjCObjectType.cs b/sources/ClangSharp/Types/ObjCObjectType.cs index 7f37b74a..1e601574 100644 --- a/sources/ClangSharp/Types/ObjCObjectType.cs +++ b/sources/ClangSharp/Types/ObjCObjectType.cs @@ -23,9 +23,9 @@ private protected unsafe ObjCObjectType(CXType handle, CXTypeKind expectedTypeKi { _baseType = new ValueLazy(&BaseTypeFactory); _interface = new ValueLazy(&InterfaceFactory); - _protocols = LazyList.Create(unchecked((int)Handle.NumObjCProtocolRefs), (i) => TranslationUnit.GetOrCreate(Handle.GetObjCProtocolDecl(unchecked((uint)i)))); + _protocols = LazyList.Create(this, unchecked((int)Handle.NumObjCProtocolRefs), &ProtocolsFactory); _superClassType = new ValueLazy(&SuperClassTypeFactory); - _typeArgs = LazyList.Create(unchecked((int)Handle.NumObjCTypeArgs), (i) => TranslationUnit.GetOrCreate(Handle.GetObjCTypeArg(unchecked((uint)i)))); + _typeArgs = LazyList.Create(this, unchecked((int)Handle.NumObjCTypeArgs), &TypeArgsFactory); } public Type BaseType => _baseType.GetValue(this); @@ -43,4 +43,16 @@ private protected unsafe ObjCObjectType(CXType handle, CXTypeKind expectedTypeKi private static unsafe ObjCInterfaceDecl InterfaceFactory(ObjCObjectType self) => self.TranslationUnit.GetOrCreate(self.Handle.Declaration); private static unsafe Type BaseTypeFactory(ObjCObjectType self) => self.TranslationUnit.GetOrCreate(self.Handle.ObjCObjectBaseType); + + private static unsafe ObjCProtocolDecl ProtocolsFactory(object self, int i) + { + var @this = (ObjCObjectType)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetObjCProtocolDecl(unchecked((uint)i))); + } + + private static unsafe Type TypeArgsFactory(object self, int i) + { + var @this = (ObjCObjectType)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetObjCTypeArg(unchecked((uint)i))); + } } diff --git a/sources/ClangSharp/Types/TemplateSpecializationType.cs b/sources/ClangSharp/Types/TemplateSpecializationType.cs index 60ea81e6..e1fc5617 100644 --- a/sources/ClangSharp/Types/TemplateSpecializationType.cs +++ b/sources/ClangSharp/Types/TemplateSpecializationType.cs @@ -15,7 +15,7 @@ public sealed class TemplateSpecializationType : Type internal unsafe TemplateSpecializationType(CXType handle) : base(handle, CXType_Unexposed, CX_TypeClass_TemplateSpecialization) { - _templateArgs = LazyList.Create(Handle.NumTemplateArguments, (i) => TranslationUnit.GetOrCreate(Handle.GetTemplateArgument(unchecked((uint)i)))); + _templateArgs = LazyList.Create(this, Handle.NumTemplateArguments, &TemplateArgsFactory); _templateName = new ValueLazy(&TemplateNameFactory); } @@ -29,4 +29,10 @@ internal unsafe TemplateSpecializationType(CXType handle) : base(handle, CXType_ public TemplateName TemplateName => _templateName.GetValue(this); private static unsafe TemplateName TemplateNameFactory(TemplateSpecializationType self) => self.TranslationUnit.GetOrCreate(self.Handle.TemplateName); + + private static unsafe TemplateArgument TemplateArgsFactory(object self, int i) + { + var @this = (TemplateSpecializationType)self; + return @this.TranslationUnit.GetOrCreate(@this.Handle.GetTemplateArgument(unchecked((uint)i))); + } }