Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP, don't merge] Non-type parameters in cxxt #383

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2206,8 +2206,16 @@ JL_DLLEXPORT void *getTargIntegralTypeAtIdx(clang::TemplateArgumentList *targs,
return getTargIntegralType(&targs->get(i));
}

JL_DLLEXPORT void *getTSTTargIntegralTypeAtIdx(clang::TemplateSpecializationType *TST, size_t i)
{
return getTargIntegralType(&TST->getArg(i));
}

JL_DLLEXPORT int getTargKind(const clang::TemplateArgument *targ)
{
std::cerr << "dumping targ:" << std::endl;
targ->dump();
std::cerr << std::endl;
return targ->getKind();
}

Expand Down Expand Up @@ -2236,6 +2244,11 @@ JL_DLLEXPORT int64_t getTargAsIntegralAtIdx(clang::TemplateArgumentList *targs,
return targs->get(i).getAsIntegral().getSExtValue();
}

JL_DLLEXPORT int64_t getTSTTargAsIntegralAtIdx(clang::TemplateSpecializationType *TST, size_t i)
{
return TST->getArg(i).getAsIntegral().getSExtValue();
}

void *getTargPackBegin(clang::TemplateArgument *targ)
{
return (void*)targ->pack_begin();
Expand Down Expand Up @@ -2794,6 +2807,39 @@ JL_DLLEXPORT void *ActOnTypeParameterParserScope(C, char *Name, unsigned Positio
return ret;
}

JL_DLLEXPORT void *ActOnNonTypeParameterParserScope(C, char *Name, unsigned Position)
{
// ActOnNonTypeTemplateParameter (Scope *S, Declarator &D, unsigned Depth, unsigned Position, SourceLocation EqualLoc, Expr *DefaultArg)
// ActOnTypeParameter (Scope *S, bool Typename, SourceLocation EllipsisLoc, SourceLocation KeyLoc, IdentifierInfo *ParamName, SourceLocation ParamNameLoc, unsigned Depth, unsigned Position, SourceLocation EqualLoc, ParsedType DefaultArg)
clang::Sema &sema = Cxx->CI->getSema();
clang::Preprocessor &PP = Cxx->Parser->getPreprocessor();

// clang::DeclSpec DS(Cxx->Parser->getAttrFactory());
// unsigned DiagID = 0;
// const char* PrevSpec = "";
// DS.SetTypeSpecType(clang::DeclSpec::TST_int, clang::SourceLocation(), PrevSpec, DiagID, Cxx->CI->getASTContext().getPrintingPolicy());
// std::cout << clang::DeclSpec::getSpecifierName(DS.getTypeSpecType(),Cxx->CI->getASTContext().getPrintingPolicy()) << std::endl;
// clang::Declarator D(DS, clang::Declarator::TemplateParamContext);
// D.getName().setIdentifier(PP.getIdentifierInfo(Name),clang::SourceLocation());

// void *ret = (void*)sema.ActOnNonTypeTemplateParameter(Cxx->Parser->getCurScope(), D, 0, Position, clang::SourceLocation(), nullptr);
// return ret;

clang::QualType QT = sema.getASTContext().IntTy;

clang::NonTypeTemplateParmDecl* Param = clang::NonTypeTemplateParmDecl::Create(sema.getASTContext(),
sema.getASTContext().getTranslationUnitDecl(),
clang::SourceLocation(),
clang::SourceLocation(),
0, Position, PP.getIdentifierInfo(Name),
QT, false, sema.getASTContext().getTrivialTypeSourceInfo(QT));

Param->setAccess(clang::AS_public);
Cxx->Parser->getCurScope()->AddDecl(Param);
sema.IdResolver.AddDecl(Param);
return (void*)Param;
}

JL_DLLEXPORT void ExitParserScope(C)
{
Cxx->Parser->ExitScope();
Expand Down
12 changes: 11 additions & 1 deletion src/clangwrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,16 @@ getTargKind(targ) =
getTargAsIntegralAtIdx(targs::rcpp"clang::TemplateArgumentList", i) =
ccall((:getTargAsIntegralAtIdx,libcxxffi),Int64,(Ptr{Void},Csize_t),targs,i)

getTargIntegralTypeAtIdx(targs, i) =
getTargAsIntegralAtIdx(targs::pcpp"clang::TemplateSpecializationType", i) =
ccall((:getTSTTargAsIntegralAtIdx,libcxxffi),Int64,(Ptr{Void},Csize_t),targs,i)

getTargIntegralTypeAtIdx(targs::Union{rcpp"clang::TemplateArgumentList",
pcpp"clang::TemplateArgumentList"}, i) =
QualType(ccall((:getTargIntegralTypeAtIdx,libcxxffi),Ptr{Void},(Ptr{Void},Csize_t),targs,i))

getTargIntegralTypeAtIdx(targs::pcpp"clang::TemplateSpecializationType", i) =
QualType(ccall((:getTSTTargIntegralTypeAtIdx,libcxxffi),Ptr{Void},(Ptr{Void},Csize_t),targs,i))

getTargPackAtIdxSize(targs, i) =
ccall((:getTargPackAtIdxSize, libcxxffi), Csize_t, (Ptr{Void}, Csize_t), targs, i)

Expand Down Expand Up @@ -657,6 +664,9 @@ ExitParserScope(C) = ccall((:ExitParserScope,libcxxffi),Void,(Ref{ClangCompiler}
ActOnTypeParameterParserScope(C, Name, pos) =
pcpp"clang::Decl"(ccall((:ActOnTypeParameterParserScope,libcxxffi),Ptr{Void},(Ref{ClangCompiler},Ptr{UInt8},Cint),C,Name,pos))

ActOnNonTypeParameterParserScope(C, Name, pos) =
pcpp"clang::Decl"(ccall((:ActOnNonTypeParameterParserScope,libcxxffi),Ptr{Void},(Ref{ClangCompiler},Ptr{UInt8},Cint),C,Name,pos))

getUnderlyingTemplateDecl(TST) =
pcpp"clang::TemplateDecl"(ccall((:getUnderlyingTemplateDecl,libcxxffi),Ptr{Void},(Ptr{Void},),TST))

Expand Down
10 changes: 8 additions & 2 deletions src/cxxstr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -546,11 +546,17 @@ end
else
ctx = Cxx.to_ctx(translation_unit(C))
end
isnontypeparam(arg) = (arg <: Type && arg.parameters[1] <: Val)
for (i,arg) in enumerate(args)
if arg == TypeVar || arg <: Integer
@show arg
if arg == TypeVar || arg <: Integer || isnontypeparam(arg)
name = string(iscc ? "__juliavar" : "var",varnum)
typename = replace(typename, string(iscc ? "__juliavar" : "__julia::var",i), name)
ActOnTypeParameterParserScope(C,name,varnum)
if isnontypeparam(arg)
ActOnNonTypeParameterParserScope(C,name,varnum)
else
ActOnTypeParameterParserScope(C,name,varnum)
end
mapping[varnum] = :(args[$i])
varnum += 1
elseif arg <: Type
Expand Down
4 changes: 4 additions & 0 deletions src/typetranslation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,13 @@ function getTemplateParameters(cxxd,quoted = false,typeargs = Dict{Int64,Void}()
args = Any[]
for i = 0:(getTargsSize(targs)-1)
kind = getTargKindAtIdx(targs,i)
@show kind
if kind == KindType
T = juliatype(getTargTypeAtIdx(targs,i),quoted,typeargs; wrapvalue = false)
push!(args,T)
elseif kind == KindExpression
@show T = typeargs[i]
push!(args,T)
elseif kind == KindIntegral
val = getTargAsIntegralAtIdx(targs,i)
t = getTargIntegralTypeAtIdx(targs,i)
Expand Down