Skip to content

Commit

Permalink
minor adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
gojakuch committed Oct 1, 2024
1 parent 668ddee commit 2c88aed
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
6 changes: 4 additions & 2 deletions include/clad/Differentiator/VisitorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,15 @@ namespace clad {
/// \param[in] SS The scope specifier for the declaration.
/// \returns the DeclRefExpr for the given declaration.
clang::DeclRefExpr* BuildDeclRef(clang::DeclaratorDecl* D,
const clang::CXXScopeSpec* SS = nullptr);
const clang::CXXScopeSpec* SS = nullptr,
clang::ExprValueKind VK = clang::VK_LValue);
/// Builds a DeclRefExpr to a given Decl, adding proper nested name
/// qualifiers.
/// \param[in] D The declaration to build a DeclRefExpr for.
/// \param[in] NNS The nested name specifier to use.
clang::DeclRefExpr* BuildDeclRef(clang::DeclaratorDecl* D,
clang::NestedNameSpecifier* NNS);
clang::NestedNameSpecifier* NNS,
clang::ExprValueKind VK = clang::VK_LValue);

/// Stores the result of an expression in a temporary variable (of the same
/// type as is the result of the expression) and returns a reference to it.
Expand Down
5 changes: 3 additions & 2 deletions lib/Differentiator/ReverseModeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,7 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,
auto* ccDRE = dyn_cast<DeclRefExpr>(clonedDRE);
NestedNameSpecifier* NNS = DRE->getQualifier();
auto* referencedDecl = cast<VarDecl>(ccDRE->getDecl());
clonedDRE = BuildDeclRef(referencedDecl, NNS);
clonedDRE = BuildDeclRef(referencedDecl, NNS, DRE->getValueKind());
}
// This case happens when ref-type variables have to become function
// global. Ref-type declarations cannot be moved to the function global
Expand Down Expand Up @@ -2133,9 +2133,10 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,

if (const auto* OCE = dyn_cast<CXXOperatorCallExpr>(CE)) {
CallArgs.insert(CallArgs.begin(), Clone(OCE->getArg(0)));
// OCE->getArg(0)->dump();
call = CXXOperatorCallExpr::Create(
m_Context, OCE->getOperator(), Clone(CE->getCallee()), CallArgs,
FD->getCallResultType(), VK_LValue, Loc,
FD->getCallResultType(), OCE->getValueKind(), Loc,
CLAD_COMPAT_CLANG11_CXXOperatorCallExpr_Create_ExtraParamsOverride());
return StmtDiff(call);
}
Expand Down
18 changes: 12 additions & 6 deletions lib/Differentiator/VisitorBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,26 +236,32 @@ namespace clad {
}

DeclRefExpr* VisitorBase::BuildDeclRef(DeclaratorDecl* D,
const CXXScopeSpec* SS /*=nullptr*/) {
const CXXScopeSpec* SS /*=nullptr*/,
ExprValueKind VK /*=VK_LValue*/) {
QualType T = D->getType();
T = T.getNonReferenceType();
return cast<DeclRefExpr>(clad_compat::GetResult<Expr*>(
m_Sema.BuildDeclRefExpr(D, T, VK_LValue, D->getBeginLoc(), SS)));
m_Sema.BuildDeclRefExpr(D, T, VK, D->getBeginLoc(), SS)));
}

DeclRefExpr* VisitorBase::BuildDeclRef(DeclaratorDecl* D,
NestedNameSpecifier* NNS) {
NestedNameSpecifier* NNS,
ExprValueKind VK /*=VK_LValue*/) {
std::vector<NestedNameSpecifier*> NNChain;
CXXScopeSpec CSS;
while (NNS) {
// FIXME: proper support for dependent NNS needs to be added.
//if (!NNS->isDependent()) return BuildDeclRef(D);

NNChain.push_back(NNS);
NNS = NNS->getPrefix();
}

std::reverse(NNChain.begin(), NNChain.end());

for (auto& n : NNChain) {
NNS = n;
for (size_t i = 0; i < NNChain.size(); ++i) {
NNS = NNChain[i];
// FIXME: this needs to be extended to support more NNS kinds. An inspiration can be take from getFullyQualifiedNestedNameSpecifier in llvm-project/clang/lib/AST/QualTypeNames.cpp
if (NNS->getKind() == NestedNameSpecifier::Namespace) {
NamespaceDecl* NS = NNS->getAsNamespace();
CSS.Extend(m_Context, NS, noLoc, noLoc);
Expand All @@ -268,7 +274,7 @@ namespace clad {
}
}

return BuildDeclRef(D, &CSS);
return BuildDeclRef(D, &CSS, VK);
}

IdentifierInfo*
Expand Down
5 changes: 2 additions & 3 deletions test/Gradient/Lambdas.C
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// RUN: ./Lambdas.out | %filecheck_exec %s
// RUN: %cladclang -Xclang -plugin-arg-clad -Xclang -enable-tbr %s -I%S/../../include -oLambdas.out
// RUN: ./Lambdas.out | %filecheck_exec %s
// CHECK-NOT: {{.*error|warning|note:.*}}

#include "clad/Differentiator/Differentiator.h"

Expand All @@ -14,7 +13,7 @@ double f1(double i, double j) {
}

// CHECK: inline void operator_call_pullback(double t, double _d_y, double *_d_t) const;
// CHECK-NEXT: void f1_grad(double i, double j, double *_d_i, double *_d_j) {
// CHECK: void f1_grad(double i, double j, double *_d_i, double *_d_j) {
// CHECK-NEXT: auto _f = []{{ ?}}(double t) {
// CHECK-NEXT: return t * t + 1.;
// CHECK-NEXT: }{{;?}}
Expand All @@ -35,7 +34,7 @@ double f2(double i, double j) {
}

// CHECK: inline void operator_call_pullback(double t, double k, double _d_y, double *_d_t, double *_d_k) const;
// CHECK-NEXT: void f2_grad(double i, double j, double *_d_i, double *_d_j) {
// CHECK: void f2_grad(double i, double j, double *_d_i, double *_d_j) {
// CHECK-NEXT: auto _f = []{{ ?}}(double t, double k) {
// CHECK-NEXT: return t + k;
// CHECK-NEXT: }{{;?}}
Expand Down

0 comments on commit 2c88aed

Please sign in to comment.