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

Add support for a new --with-namespace option #288

Merged
merged 1 commit into from
Nov 11, 2021
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
25 changes: 9 additions & 16 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ private void VisitRecordDecl(RecordDecl recordDecl)
if (_testOutputBuilder != null)
{
_testOutputBuilder.AddUsingDirective("System");
_testOutputBuilder.AddUsingDirective($"static {_config.Namespace}.{_config.MethodClassName}");
_testOutputBuilder.AddUsingDirective($"static {GetNamespace(_config.MethodClassName)}.{_config.MethodClassName}");

_testOutputBuilder.WriteIndented("/// <summary>Validates that the <see cref=\"Guid\" /> of the <see cref=\"");
_testOutputBuilder.Write(escapedName);
Expand Down Expand Up @@ -1187,17 +1187,18 @@ private void VisitRecordDecl(RecordDecl recordDecl)
_ = nativeTypeNameBuilder.Append(nativeName);
_ = nativeTypeNameBuilder.Append(" : ");

var baseName = GetCursorName(cxxRecordDecl.Bases[0].Referenced);
_ = nativeTypeNameBuilder.Append(baseName);
var baseName = GetRemappedCursorName(cxxRecordDecl.Bases[0].Referenced, out var nativeBaseName, skipUsing: !_config.GenerateMarkerInterfaces);

baseTypeNamesBuilder.Add(baseName);
_ = nativeTypeNameBuilder.Append(nativeBaseName);

for (var i = 1; i < cxxRecordDecl.Bases.Count; i++)
{
_ = nativeTypeNameBuilder.Append(", ");
baseName = GetCursorName(cxxRecordDecl.Bases[i].Referenced);
baseName = GetRemappedCursorName(cxxRecordDecl.Bases[i].Referenced, out nativeBaseName, skipUsing: !_config.GenerateMarkerInterfaces);

_ = nativeTypeNameBuilder.Append(baseName);
baseTypeNamesBuilder.Add(baseName);
_ = nativeTypeNameBuilder.Append(nativeBaseName);
}

nativeNameWithExtras = nativeTypeNameBuilder.ToString();
Expand Down Expand Up @@ -1603,7 +1604,6 @@ void OutputMarkerInterface(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethodD

var currentContext = _context.AddLast((cxxMethodDecl, null));

var accessSpecifier = GetAccessSpecifier(cxxMethodDecl);
var returnType = cxxMethodDecl.ReturnType;
var returnTypeName = GetRemappedTypeName(cxxMethodDecl, cxxRecordDecl, returnType, out var nativeTypeName);

Expand All @@ -1619,7 +1619,7 @@ void OutputMarkerInterface(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethodD
}

var desc = new FunctionOrDelegateDesc<(string Name, PInvokeGenerator This)> {
AccessSpecifier = accessSpecifier,
AccessSpecifier = AccessSpecifier.Public,
EscapedName = EscapeAndStripName(name),
IsMemberFunction = true,
NativeTypeName = nativeTypeName,
Expand Down Expand Up @@ -1692,18 +1692,12 @@ void OutputVtblEntry(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethodDecl)

var cxxMethodDeclTypeName = GetRemappedTypeName(cxxMethodDecl, cxxRecordDecl, cxxMethodDecl.Type, out var nativeTypeName, skipUsing: false, ignoreTransparentStructsWhereRequired: true);

var accessSpecifier = GetAccessSpecifier(cxxMethodDecl);
var remappedName = FixupNameForMultipleHits(cxxMethodDecl);
var escapedName = EscapeAndStripName(remappedName);

if (accessSpecifier == AccessSpecifier.Private)
{
accessSpecifier = AccessSpecifier.Internal;
}

var desc = new FieldDesc
{
AccessSpecifier = accessSpecifier,
AccessSpecifier = AccessSpecifier.Public,
NativeTypeName = nativeTypeName,
EscapedName = escapedName,
Offset = null,
Expand Down Expand Up @@ -1732,7 +1726,6 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod

var currentContext = _context.AddLast((cxxMethodDecl, null));

var accessSpecifier = GetAccessSpecifier(cxxMethodDecl);
var returnType = cxxMethodDecl.ReturnType;
var returnTypeName = GetRemappedTypeName(cxxMethodDecl, cxxRecordDecl, returnType, out var nativeTypeName);

Expand All @@ -1749,7 +1742,7 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod

var desc = new FunctionOrDelegateDesc<(string Name, PInvokeGenerator This)>
{
AccessSpecifier = accessSpecifier,
AccessSpecifier = AccessSpecifier.Public,
IsAggressivelyInlined = _config.GenerateAggressiveInlining,
EscapedName = EscapeAndStripName(name),
IsMemberFunction = true,
Expand Down
58 changes: 44 additions & 14 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -582,33 +582,63 @@ private void VisitCXXUuidofExpr(CXXUuidofExpr cxxUuidofExpr)
private void VisitDeclRefExpr(DeclRefExpr declRefExpr)
{
var outputBuilder = StartCSharpCode();
if ((declRefExpr.Decl is EnumConstantDecl enumConstantDecl) && (declRefExpr.DeclContext != enumConstantDecl.DeclContext) && (enumConstantDecl.DeclContext is NamedDecl namedDecl))
{
var enumName = GetRemappedCursorName(namedDecl);

if (!_config.DontUseUsingStaticsForEnums)
var name = GetRemappedCursorName(declRefExpr.Decl, out _, skipUsing: true);
var escapedName = EscapeName(name);

if (declRefExpr.Decl is EnumConstantDecl enumConstantDecl)
{
if ((declRefExpr.DeclContext != enumConstantDecl.DeclContext) && (enumConstantDecl.DeclContext is NamedDecl namedDecl))
{
if (enumName.StartsWith("__AnonymousEnum_"))
var enumName = GetRemappedCursorName(namedDecl, out _, skipUsing: true);

if (!_config.DontUseUsingStaticsForEnums)
{
if (outputBuilder.Name != _config.MethodClassName)
if (enumName.StartsWith("__AnonymousEnum_"))
{
if (outputBuilder.Name != _config.MethodClassName)
{
outputBuilder.AddUsingDirective($"static {GetNamespace(_config.MethodClassName)}.{_config.MethodClassName}");
}
}
else
{
outputBuilder.AddUsingDirective($"static {_config.Namespace}.{_config.MethodClassName}");
outputBuilder.AddUsingDirective($"static {GetNamespace(enumName)}.{enumName}");
}
}
else
{
outputBuilder.AddUsingDirective($"static {_config.Namespace}.{enumName}");
outputBuilder.Write(enumName);
outputBuilder.Write(".");
}
}
else
}
else
{
if (TryGetNamespace(name, out var namespaceName))
{
var namespaceNameParts = namespaceName.Split(';');
namespaceName = namespaceNameParts[0];

if (namespaceNameParts.Length == 2)
{
if ($"{_currentNamespace}.{outputBuilder.Name}" != namespaceName)
{
outputBuilder.AddUsingDirective($"static {namespaceName}.{namespaceNameParts[1]}");
}
}
else if (_currentNamespace != namespaceName)
{
outputBuilder.AddUsingDirective(namespaceName);
}
}

if (declRefExpr.Decl is FunctionDecl)
{
outputBuilder.Write(enumName);
outputBuilder.Write(".");
escapedName = EscapeAndStripName(name);
}
}

var name = GetRemappedCursorName(declRefExpr.Decl);
var escapedName = (declRefExpr.Decl is FunctionDecl) ? EscapeAndStripName(name) : EscapeName(name);
outputBuilder.Write(escapedName);

StopCSharpCode();
Expand Down Expand Up @@ -1321,7 +1351,7 @@ void HandleUnmanagedConstant(InitListExpr initListExpr, Type type, string typeNa
if (_testOutputBuilder != null)
{
_testOutputBuilder.AddUsingDirective("System");
_testOutputBuilder.AddUsingDirective($"static {_config.Namespace}.{_config.MethodClassName}");
_testOutputBuilder.AddUsingDirective($"static {GetNamespace(_config.MethodClassName)}.{_config.MethodClassName}");

_testOutputBuilder.WriteIndented("/// <summary>Validates that the value of the <see cref=\"");
_testOutputBuilder.Write(escapedName);
Expand Down
Loading