@@ -892,17 +892,34 @@ namespace clad {
892892 return true ;
893893 }
894894
895+ namespace {
896+ NamespaceDecl* CachedCladNS = nullptr ;
897+ QualType CachedRestoreTrackerTy;
898+ TemplateDecl* CachedMatrixDecl = nullptr ;
899+ TemplateDecl* CachedArrayDecl = nullptr ;
900+ TemplateDecl* CachedArrayRefDecl = nullptr ;
901+ TemplateDecl* CachedTagDecl = nullptr ;
902+ }
903+
904+ void ResetCladUtilsASTCache (ASTContext&) {
905+ CachedCladNS = nullptr ;
906+ CachedRestoreTrackerTy = QualType ();
907+ CachedMatrixDecl = nullptr ;
908+ CachedArrayDecl = nullptr ;
909+ CachedArrayRefDecl = nullptr ;
910+ CachedTagDecl = nullptr ;
911+ }
912+
895913 NamespaceDecl* GetCladNamespace (Sema& S) {
896- static NamespaceDecl* Result = nullptr ;
897- if (Result)
898- return Result;
914+ if (CachedCladNS)
915+ return CachedCladNS;
899916 DeclarationName CladName = &S.getASTContext ().Idents .get (" clad" );
900917 LookupResult CladR (S, CladName, noLoc, Sema::LookupNamespaceName,
901918 CLAD_COMPAT_Sema_ForVisibleRedeclaration);
902919 S.LookupQualifiedName (CladR, S.getASTContext ().getTranslationUnitDecl ());
903920 assert (!CladR.empty () && " cannot find clad namespace" );
904- Result = cast<NamespaceDecl>(CladR.getFoundDecl ());
905- return Result ;
921+ CachedCladNS = cast<NamespaceDecl>(CladR.getFoundDecl ());
922+ return CachedCladNS ;
906923 }
907924
908925 Expr* getZeroInit (QualType T, Sema& S) {
@@ -945,9 +962,8 @@ namespace clad {
945962 }
946963
947964 clang::QualType GetRestoreTrackerType (clang::Sema& S) {
948- static QualType T;
949- if (!T.isNull ())
950- return T;
965+ if (!CachedRestoreTrackerTy.isNull ())
966+ return CachedRestoreTrackerTy;
951967 NamespaceDecl* CladNS = GetCladNamespace (S);
952968 CXXScopeSpec CSS ;
953969 CSS .Extend (S.getASTContext (), CladNS, noLoc, noLoc);
@@ -961,15 +977,16 @@ namespace clad {
961977 // This will instantiate restore_tracker<T> type and return it.
962978 auto * RD = cast<RecordDecl>(TrackerR.getFoundDecl ());
963979 ASTContext& C = S.getASTContext ();
964- T = clad_compat::getRecordType (C, RD );
980+ CachedRestoreTrackerTy = clad_compat::getRecordType (C, RD );
965981 // Get clad namespace and its identifier clad::.
966982 clad_compat::NestedNameSpecifierTy NS = CSS .getScopeRep ();
967983
968984 // Create elaborated type with namespace specifier,
969985 // i.e. class<T> -> clad::class<T>
970- T = clad_compat::getElaboratedType (
971- C, clad_compat::ElaboratedTypeKeyword_None, NS , T);
972- return T;
986+ CachedRestoreTrackerTy = clad_compat::getElaboratedType (
987+ C, clad_compat::ElaboratedTypeKeyword_None, NS ,
988+ CachedRestoreTrackerTy);
989+ return CachedRestoreTrackerTy;
973990 }
974991
975992 TemplateDecl* LookupTemplateDeclInCladNamespace (Sema& S,
@@ -1134,19 +1151,18 @@ namespace clad {
11341151 }
11351152
11361153 QualType GetCladMatrixOfType (Sema& S, clang::QualType T) {
1137- static TemplateDecl* matrixDecl = nullptr ;
1138- if (!matrixDecl)
1139- matrixDecl =
1154+ if (!CachedMatrixDecl)
1155+ CachedMatrixDecl =
11401156 utils::LookupTemplateDeclInCladNamespace (S,
11411157 /* ClassName=*/ " matrix" );
1142- return InstantiateTemplate (S, matrixDecl , {T});
1158+ return InstantiateTemplate (S, CachedMatrixDecl , {T});
11431159 }
11441160
11451161 QualType GetCladArrayOfType (Sema& S, clang::QualType T) {
1146- static TemplateDecl* arrayDecl = nullptr ;
1147- if (!arrayDecl)
1148- arrayDecl = LookupTemplateDeclInCladNamespace (S, /* ClassName=*/ " array" );
1149- return utils::InstantiateTemplate (S, arrayDecl , {T});
1162+ if (!CachedArrayDecl)
1163+ CachedArrayDecl =
1164+ LookupTemplateDeclInCladNamespace (S, /* ClassName=*/ " array" );
1165+ return utils::InstantiateTemplate (S, CachedArrayDecl , {T});
11501166 }
11511167
11521168 bool IsDifferentiableType (QualType T) {
@@ -1188,11 +1204,10 @@ namespace clad {
11881204 }
11891205
11901206 QualType GetCladArrayRefOfType (Sema& S, QualType T) {
1191- static TemplateDecl* arrayRefDecl = nullptr ;
1192- if (!arrayRefDecl)
1193- arrayRefDecl = utils::LookupTemplateDeclInCladNamespace (
1207+ if (!CachedArrayRefDecl)
1208+ CachedArrayRefDecl = utils::LookupTemplateDeclInCladNamespace (
11941209 S, /* ClassName=*/ " array_ref" );
1195- return utils::InstantiateTemplate (S, arrayRefDecl , {T});
1210+ return utils::InstantiateTemplate (S, CachedArrayRefDecl , {T});
11961211 }
11971212
11981213 QualType GetParameterDerivativeType (Sema& S, DiffMode Mode, QualType Type) {
@@ -1392,10 +1407,9 @@ namespace clad {
13921407 }
13931408
13941409 QualType GetCladTagOfType (Sema& S, QualType T) {
1395- static clang::TemplateDecl* CladTag = nullptr ;
1396- if (!CladTag)
1397- CladTag = utils::LookupTemplateDeclInCladNamespace (S, " Tag" );
1398- return utils::InstantiateTemplate (S, CladTag, {T});
1410+ if (!CachedTagDecl)
1411+ CachedTagDecl = utils::LookupTemplateDeclInCladNamespace (S, " Tag" );
1412+ return utils::InstantiateTemplate (S, CachedTagDecl, {T});
13991413 }
14001414
14011415 Expr* GetCladTagExpr (Sema& S, QualType T) {
0 commit comments