Skip to content

Fix/parser improvements #1919

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

Merged
merged 6 commits into from
Mar 4, 2025
Merged
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
16 changes: 14 additions & 2 deletions src/CppParser/Bootstrap/StmtCodeGenerators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,20 @@ public override bool VisitProperty(Property property)
WriteLine($"_S->{fieldName} = static_cast<AST::{typeName}>(" +
$"WalkStatement(S->{methodName}()));");
else if (typeName.Contains("Expr"))
WriteLine($"_S->{fieldName} = static_cast<AST::{typeName}>(" +
$"WalkExpression(S->{methodName}()));");
{
var expr = $"_S->{fieldName} = static_cast<AST::{typeName}>(WalkExpression(S->{methodName}()));";

if (fieldName == "base" && typeName is "CXXDependentScopeMemberExpr")
{
// Clang asserts that 'getBase()' is not called when 'isImplicitAccess()' returns true
WriteLine("if (!S->isImplicitAccess())");
WriteLineIndent(expr);
}
else
{
WriteLine(expr);
}
}
else if (fieldName == "guidDecl")
WriteLine($"_S->{fieldName} = S->getGuidDecl()->getNameAsString();");
else if (typeName.Contains("Decl") || typeName.Contains("Method") ||
Expand Down
3 changes: 2 additions & 1 deletion src/CppParser/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2426,7 +2426,8 @@ AST::Expr* Parser::WalkExpression(const clang::Expr* Expr)
_S->refersToMatrixElement = S->refersToMatrixElement();
_S->hasPlaceholderType = S->hasPlaceholderType();
_S->isImplicitAccess = S->isImplicitAccess();
_S->base = static_cast<AST::Expr*>(WalkExpression(S->getBase()));
if (!S->isImplicitAccess())
_S->base = static_cast<AST::Expr*>(WalkExpression(S->getBase()));
_S->baseType = GetQualifiedType(S->getBaseType());
_S->isArrow = S->isArrow();
_S->firstQualifierFoundInScope = static_cast<AST::Declaration*>(WalkDeclaration(S->getFirstQualifierFoundInScope()));
Expand Down
31 changes: 16 additions & 15 deletions src/CppParser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ Parser::Parser(CppParserOptions* Opts)
{
supportedStdTypes.insert("allocator");
supportedStdTypes.insert("basic_string");

walkedNamespaces.reserve(8192);
walkedTypeTemplateParameters.reserve(8192);
walkedTemplateTemplateParameters.reserve(32);
walkedNonTypeTemplateParameters.reserve(1024);
walkedParameters.reserve(65536);
}

LayoutField Parser::WalkVTablePointer(Class* Class,
Expand Down Expand Up @@ -424,27 +430,23 @@ void Parser::Setup(bool Compile)
if (opts->verbose)
HSOpts.Verbose = true;

for (unsigned I = 0, E = opts->IncludeDirs.size(); I != E; ++I)
for (const auto& s : opts->IncludeDirs)
{
const auto& s = opts->IncludeDirs[I];
HSOpts.AddPath(s, frontend::Angled, false, false);
}

for (unsigned I = 0, E = opts->SystemIncludeDirs.size(); I != E; ++I)
for (const auto& s : opts->SystemIncludeDirs)
{
const auto& s = opts->SystemIncludeDirs[I];
HSOpts.AddPath(s, frontend::System, false, false);
}

for (unsigned I = 0, E = opts->Defines.size(); I != E; ++I)
for (const auto& define : opts->Defines)
{
const auto& define = opts->Defines[I];
PPOpts.addMacroDef(define);
}

for (unsigned I = 0, E = opts->Undefines.size(); I != E; ++I)
for (const auto& undefine : opts->Undefines)
{
const auto& undefine = opts->Undefines[I];
PPOpts.addMacroUndef(undefine);
}

Expand Down Expand Up @@ -480,8 +482,7 @@ void Parser::Setup(bool Compile)
}
}

if (TC)
delete TC;
delete TC;

// Enable preprocessing record.
PPOpts.DetailedRecord = true;
Expand Down Expand Up @@ -2537,7 +2538,7 @@ Type* Parser::WalkType(clang::QualType QualType, const clang::TypeLoc* TL, bool
EnumDecl* ED = ET->getDecl();

auto TT = new AST::TagType();
TT->declaration = TT->declaration = WalkDeclaration(ED);
TT->declaration = WalkDeclaration(ED);

Ty = TT;
break;
Expand Down Expand Up @@ -4430,8 +4431,8 @@ Declaration* Parser::WalkDeclaration(const clang::Decl* D)
{
auto MD = cast<CXXMethodDecl>(D);
Decl = WalkMethodCXX(MD);
if (Decl == nullptr)
return Decl;
if (!Decl)
return nullptr;

auto NS = GetNamespace(MD);
Decl->_namespace = NS;
Expand Down Expand Up @@ -4609,7 +4610,7 @@ void Parser::SetupLLVMCodegen()
c->getHeaderSearchOpts(), c->getPreprocessorOpts(),
c->getCodeGenOpts(), *LLVMModule, c->getDiagnostics()));

codeGenTypes.reset(new clang::CodeGen::CodeGenTypes(*CGM.get()));
codeGenTypes.reset(new clang::CodeGen::CodeGenTypes(*CGM));
}

bool Parser::SetupSourceFiles(const std::vector<std::string>& SourceFiles,
Expand Down Expand Up @@ -4710,7 +4711,7 @@ ParserResult* Parser::Parse(const std::vector<std::string>& SourceFiles)

DiagClient->BeginSourceFile(c->getLangOpts(), &c->getPreprocessor());

ParseAST(c->getSema());
ParseAST(c->getSema(), opts->verbose, opts->skipFunctionBodies);

DiagClient->EndSourceFile();

Expand Down
12 changes: 4 additions & 8 deletions src/Generator/Passes/SymbolsCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ private void WrapConstructor(Method method, string wrapper, string @params)
bool isAbstract = ((Class)method.Namespace).IsAbstract;
if (method.Access == AccessSpecifier.Protected || isAbstract)
{
Write($@"{{ ::new ({Helpers.InstanceField}) {
wrapper}{method.Namespace.Name}({@params}); }}");
Write($@"{{ ::new ({Helpers.InstanceField}) {wrapper}{method.Namespace.Name}({@params}); }}");
WriteLine(!isAbstract ? " };" : string.Empty);
}
else
Expand Down Expand Up @@ -210,12 +209,10 @@ private void WrapDestructor(Method method, string wrapper)
{
string @class = wrapper + method.Namespace.Name;
WriteLine($"() {{ this->~{@class}(); }} }};");
Write($@"extern ""C"" {GetExporting()}void {wrapper}({
@class}* {instance}) {{ {instance}->{wrapper}Protected");
Write($@"extern ""C"" {GetExporting()}void {wrapper}({@class}* {instance}) {{ {instance}->{wrapper}Protected");
}
else
Write($@"({$"{@namespace}*{instance}"}) {{ {
instance}->~{method.Namespace.Name}");
Write($@"({$"{@namespace}*{instance}"}) {{ {instance}->~{method.Namespace.Name}");
WriteLine("(); }");
}

Expand All @@ -238,8 +235,7 @@ private void TakeFunctionAddress(Function function, string wrapper)

var method = function as Method;
if (function.Namespace.Access == AccessSpecifier.Protected)
Write($@"class {wrapper}{function.Namespace.Name} : public {
function.Namespace.Namespace.Visit(cppTypePrinter)} {{ ");
Write($@"class {wrapper}{function.Namespace.Name} : public {function.Namespace.Namespace.Visit(cppTypePrinter)} {{ ");

string variable = $@"({(method?.IsStatic == false ?
(@namespace + "::") : string.Empty)}*{wrapper}){signature}";
Expand Down
Loading