Browse Source

Fix/parser improvements (#1919)

* Parser improvements

* Fix AST Converter missing conversion data

* Fix bug in test code

Can't return `T();` if `T` is a const reference type.

* Fix other test compile bug

It's kinda weird to call it pure virtual and then implement it anyway no?

* Fix test compiler error

Don't skip function bodies to force template instantiations

* Fix clang assert
pull/1920/head
Jelle 1 year ago committed by GitHub
parent
commit
88204f7fb7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 16
      src/CppParser/Bootstrap/StmtCodeGenerators.cs
  2. 1
      src/CppParser/ParseExpr.cpp
  3. 29
      src/CppParser/Parser.cpp
  4. 12
      src/Generator/Passes/SymbolsCodeGenerator.cs
  5. 3
      src/Parser/ASTConverter.cs
  6. 13
      tests/dotnet/CSharp/CSharp.Gen.cs
  7. 5
      tests/dotnet/CSharp/CSharpTemplates.h
  8. 6
      tests/dotnet/Common/Common.h

16
src/CppParser/Bootstrap/StmtCodeGenerators.cs

@ -341,8 +341,20 @@ namespace CppSharp
WriteLine($"_S->{fieldName} = static_cast<AST::{typeName}>(" + WriteLine($"_S->{fieldName} = static_cast<AST::{typeName}>(" +
$"WalkStatement(S->{methodName}()));"); $"WalkStatement(S->{methodName}()));");
else if (typeName.Contains("Expr")) 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") else if (fieldName == "guidDecl")
WriteLine($"_S->{fieldName} = S->getGuidDecl()->getNameAsString();"); WriteLine($"_S->{fieldName} = S->getGuidDecl()->getNameAsString();");
else if (typeName.Contains("Decl") || typeName.Contains("Method") || else if (typeName.Contains("Decl") || typeName.Contains("Method") ||

1
src/CppParser/ParseExpr.cpp

@ -2426,6 +2426,7 @@ AST::Expr* Parser::WalkExpression(const clang::Expr* Expr)
_S->refersToMatrixElement = S->refersToMatrixElement(); _S->refersToMatrixElement = S->refersToMatrixElement();
_S->hasPlaceholderType = S->hasPlaceholderType(); _S->hasPlaceholderType = S->hasPlaceholderType();
_S->isImplicitAccess = S->isImplicitAccess(); _S->isImplicitAccess = S->isImplicitAccess();
if (!S->isImplicitAccess())
_S->base = static_cast<AST::Expr*>(WalkExpression(S->getBase())); _S->base = static_cast<AST::Expr*>(WalkExpression(S->getBase()));
_S->baseType = GetQualifiedType(S->getBaseType()); _S->baseType = GetQualifiedType(S->getBaseType());
_S->isArrow = S->isArrow(); _S->isArrow = S->isArrow();

29
src/CppParser/Parser.cpp

@ -165,6 +165,12 @@ Parser::Parser(CppParserOptions* Opts)
{ {
supportedStdTypes.insert("allocator"); supportedStdTypes.insert("allocator");
supportedStdTypes.insert("basic_string"); supportedStdTypes.insert("basic_string");
walkedNamespaces.reserve(8192);
walkedTypeTemplateParameters.reserve(8192);
walkedTemplateTemplateParameters.reserve(32);
walkedNonTypeTemplateParameters.reserve(1024);
walkedParameters.reserve(65536);
} }
LayoutField Parser::WalkVTablePointer(Class* Class, LayoutField Parser::WalkVTablePointer(Class* Class,
@ -424,27 +430,23 @@ void Parser::Setup(bool Compile)
if (opts->verbose) if (opts->verbose)
HSOpts.Verbose = true; 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); 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); 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); 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); PPOpts.addMacroUndef(undefine);
} }
@ -480,7 +482,6 @@ void Parser::Setup(bool Compile)
} }
} }
if (TC)
delete TC; delete TC;
// Enable preprocessing record. // Enable preprocessing record.
@ -2537,7 +2538,7 @@ Type* Parser::WalkType(clang::QualType QualType, const clang::TypeLoc* TL, bool
EnumDecl* ED = ET->getDecl(); EnumDecl* ED = ET->getDecl();
auto TT = new AST::TagType(); auto TT = new AST::TagType();
TT->declaration = TT->declaration = WalkDeclaration(ED); TT->declaration = WalkDeclaration(ED);
Ty = TT; Ty = TT;
break; break;
@ -4430,8 +4431,8 @@ Declaration* Parser::WalkDeclaration(const clang::Decl* D)
{ {
auto MD = cast<CXXMethodDecl>(D); auto MD = cast<CXXMethodDecl>(D);
Decl = WalkMethodCXX(MD); Decl = WalkMethodCXX(MD);
if (Decl == nullptr) if (!Decl)
return Decl; return nullptr;
auto NS = GetNamespace(MD); auto NS = GetNamespace(MD);
Decl->_namespace = NS; Decl->_namespace = NS;
@ -4609,7 +4610,7 @@ void Parser::SetupLLVMCodegen()
c->getHeaderSearchOpts(), c->getPreprocessorOpts(), c->getHeaderSearchOpts(), c->getPreprocessorOpts(),
c->getCodeGenOpts(), *LLVMModule, c->getDiagnostics())); 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, bool Parser::SetupSourceFiles(const std::vector<std::string>& SourceFiles,
@ -4710,7 +4711,7 @@ ParserResult* Parser::Parse(const std::vector<std::string>& SourceFiles)
DiagClient->BeginSourceFile(c->getLangOpts(), &c->getPreprocessor()); DiagClient->BeginSourceFile(c->getLangOpts(), &c->getPreprocessor());
ParseAST(c->getSema()); ParseAST(c->getSema(), opts->verbose, opts->skipFunctionBodies);
DiagClient->EndSourceFile(); DiagClient->EndSourceFile();

12
src/Generator/Passes/SymbolsCodeGenerator.cs

@ -162,8 +162,7 @@ namespace CppSharp.Passes
bool isAbstract = ((Class)method.Namespace).IsAbstract; bool isAbstract = ((Class)method.Namespace).IsAbstract;
if (method.Access == AccessSpecifier.Protected || isAbstract) if (method.Access == AccessSpecifier.Protected || isAbstract)
{ {
Write($@"{{ ::new ({Helpers.InstanceField}) { Write($@"{{ ::new ({Helpers.InstanceField}) {wrapper}{method.Namespace.Name}({@params}); }}");
wrapper}{method.Namespace.Name}({@params}); }}");
WriteLine(!isAbstract ? " };" : string.Empty); WriteLine(!isAbstract ? " };" : string.Empty);
} }
else else
@ -210,12 +209,10 @@ namespace CppSharp.Passes
{ {
string @class = wrapper + method.Namespace.Name; string @class = wrapper + method.Namespace.Name;
WriteLine($"() {{ this->~{@class}(); }} }};"); WriteLine($"() {{ this->~{@class}(); }} }};");
Write($@"extern ""C"" {GetExporting()}void {wrapper}({ Write($@"extern ""C"" {GetExporting()}void {wrapper}({@class}* {instance}) {{ {instance}->{wrapper}Protected");
@class}* {instance}) {{ {instance}->{wrapper}Protected");
} }
else else
Write($@"({$"{@namespace}*{instance}"}) {{ { Write($@"({$"{@namespace}*{instance}"}) {{ {instance}->~{method.Namespace.Name}");
instance}->~{method.Namespace.Name}");
WriteLine("(); }"); WriteLine("(); }");
} }
@ -238,8 +235,7 @@ namespace CppSharp.Passes
var method = function as Method; var method = function as Method;
if (function.Namespace.Access == AccessSpecifier.Protected) if (function.Namespace.Access == AccessSpecifier.Protected)
Write($@"class {wrapper}{function.Namespace.Name} : public { Write($@"class {wrapper}{function.Namespace.Name} : public {function.Namespace.Namespace.Visit(cppTypePrinter)} {{ ");
function.Namespace.Namespace.Visit(cppTypePrinter)} {{ ");
string variable = $@"({(method?.IsStatic == false ? string variable = $@"({(method?.IsStatic == false ?
(@namespace + "::") : string.Empty)}*{wrapper}){signature}"; (@namespace + "::") : string.Empty)}*{wrapper}){signature}";

3
src/Parser/ASTConverter.cs

@ -1833,6 +1833,8 @@ namespace CppSharp
var _spec = (AST.ClassTemplateSpecialization)Visit(spec); var _spec = (AST.ClassTemplateSpecialization)Visit(spec);
_decl.Specializations.Add(_spec); _decl.Specializations.Add(_spec);
} }
_decl.TemplatedClass.TemplateParameters.AddRange(_decl.Parameters);
_decl.TemplatedClass.Specializations.AddRange(_decl.Specializations);
return _decl; return _decl;
} }
@ -1927,6 +1929,7 @@ namespace CppSharp
var _param = Visit(param); var _param = Visit(param);
_decl.Parameters.Add(_param); _decl.Parameters.Add(_param);
} }
_decl.TemplateParameters.AddRange(_decl.TemplatedDecl.Parameters);
return _decl; return _decl;
} }

13
tests/dotnet/CSharp/CSharp.Gen.cs

@ -25,6 +25,7 @@ namespace CppSharp.Tests
base.Setup(driver); base.Setup(driver);
driver.ParserOptions.UnityBuild = true; driver.ParserOptions.UnityBuild = true;
driver.ParserOptions.SkipFunctionBodies = false;
driver.ParserOptions.AddSupportedFunctionTemplates("FunctionTemplate"); driver.ParserOptions.AddSupportedFunctionTemplates("FunctionTemplate");
driver.Options.GenerateFreeStandingFunctionsClassName = t => t.FileNameWithoutExtension + "Cool"; driver.Options.GenerateFreeStandingFunctionsClassName = t => t.FileNameWithoutExtension + "Cool";
@ -237,16 +238,12 @@ namespace CppSharp.Tests
var specialization = type.GetClassTemplateSpecialization(); var specialization = type.GetClassTemplateSpecialization();
var typePrinter = new CSharpTypePrinter(null); var typePrinter = new CSharpTypePrinter(null);
typePrinter.PushContext(TypePrinterContextKind.Native); typePrinter.PushContext(TypePrinterContextKind.Native);
return new CustomType(string.Format($@"{ return new CustomType(string.Format($@"{specialization.Visit(typePrinter)}{(Type.IsAddress() ? "*" : string.Empty)}", specialization.Visit(typePrinter),
specialization.Visit(typePrinter)}{
(Type.IsAddress() ? "*" : string.Empty)}", specialization.Visit(typePrinter),
Type.IsAddress() ? "*" : string.Empty)); Type.IsAddress() ? "*" : string.Empty));
} }
return new CustomType( return new CustomType(
$@"global::System.Collections.Generic.{ $@"global::System.Collections.Generic.{(ctx.MarshalKind == MarshalKind.DefaultExpression ? "List" : "IList")}<{ctx.GetTemplateParameterList()}>");
(ctx.MarshalKind == MarshalKind.DefaultExpression ? "List" : "IList")}<{
ctx.GetTemplateParameterList()}>");
} }
public override void MarshalToNative(MarshalContext ctx) public override void MarshalToNative(MarshalContext ctx)
@ -292,9 +289,7 @@ namespace CppSharp.Tests
{ {
if (ctx.Kind == TypePrinterContextKind.Native) if (ctx.Kind == TypePrinterContextKind.Native)
{ {
return new CustomType($@"global::CSharp.QString.{ return new CustomType($@"global::CSharp.QString.{Helpers.InternalStruct}{(ctx.Type.IsAddress() ? "*" : string.Empty)}");
Helpers.InternalStruct}{
(ctx.Type.IsAddress() ? "*" : string.Empty)}");
} }
return new CILType(typeof(string)); return new CILType(typeof(string));
} }

5
tests/dotnet/CSharp/CSharpTemplates.h

@ -163,9 +163,10 @@ T IndependentFields<T>::getDependent(const T& t)
} }
template <typename T> template <typename T>
T IndependentFields<T>::property() typename IndependentFields<T>::Type IndependentFields<T>::property()
{ {
return T(); std::remove_reference_t<std::remove_const_t<Type>> t;
return t;
} }
template <typename T> template <typename T>

6
tests/dotnet/Common/Common.h

@ -790,13 +790,13 @@ public:
virtual ~PureDtor() = 0; virtual ~PureDtor() = 0;
}; };
class PureImplementedDtor class VirtualImplementedDtor
{ {
public: public:
virtual ~PureImplementedDtor() = 0; virtual ~VirtualImplementedDtor();
}; };
PureImplementedDtor::~PureImplementedDtor() inline VirtualImplementedDtor::~VirtualImplementedDtor()
{ {
} }

Loading…
Cancel
Save