Browse Source

Code cleanups.

pull/1713/head
Joao Matos 4 years ago
parent
commit
4449c14f58
  1. 93
      src/AST/Property.cs
  2. 2
      src/CLI/Generator.cs
  3. 8
      src/Generator/Driver.cs
  4. 2
      src/Generator/Generators/Emscripten/EmscriptenHeaders.cs
  5. 3
      src/Generator/Passes/FieldToPropertyPass.cs
  6. 57
      src/Generator/Passes/GetterSetterToPropertyPass.cs
  7. 2
      src/Generator/Types/Std/Stdlib.CSharp.cs
  8. 1
      src/Generator/Utils/Options.cs
  9. 35
      src/Parser/ParserOptions.cs

93
src/AST/Property.cs

@ -22,109 +22,62 @@ namespace CppSharp.AST @@ -22,109 +22,62 @@ namespace CppSharp.AST
parameters.AddRange(property.Parameters);
}
public Type Type
{
get { return QualifiedType.Type; }
}
public Type Type => QualifiedType.Type;
public QualifiedType QualifiedType { get; set; }
public bool IsStatic
{
get
{
return (GetMethod != null && GetMethod.IsStatic) ||
(SetMethod != null && SetMethod.IsStatic);
}
}
public bool IsStatic =>
GetMethod is {IsStatic: true} ||
SetMethod is {IsStatic: true};
public bool IsPure
{
get
{
return (GetMethod != null && GetMethod.IsPure) ||
(SetMethod != null && SetMethod.IsPure);
}
}
public bool IsPure =>
GetMethod is {IsPure: true} ||
SetMethod is {IsPure: true};
public bool IsVirtual
{
get
{
return (GetMethod != null && GetMethod.IsVirtual) ||
(SetMethod != null && SetMethod.IsVirtual);
}
}
public bool IsVirtual =>
GetMethod is {IsVirtual: true} ||
SetMethod is {IsVirtual: true};
public bool IsOverride
{
get
{
return (GetMethod != null && GetMethod.IsOverride) ||
(SetMethod != null && SetMethod.IsOverride);
}
}
public bool IsOverride =>
GetMethod is {IsOverride: true} ||
SetMethod is {IsOverride: true};
public Method GetMethod { get; set; }
public Method SetMethod { get; set; }
public bool HasGetter
{
get
{
return (GetMethod != null &&
public bool HasGetter =>
(GetMethod != null &&
GetMethod.GenerationKind != GenerationKind.None) ||
(Field != null &&
Field.GenerationKind != GenerationKind.None);
}
}
public bool HasSetter
{
get
{
return (SetMethod != null &&
public bool HasSetter =>
(SetMethod != null &&
SetMethod.GenerationKind != GenerationKind.None) ||
(Field != null &&
(!Field.QualifiedType.IsConst() ||
Field.Type.IsConstCharString()) &&
Field.GenerationKind != GenerationKind.None);
}
}
// The field that should be get and set by this property
public Field Field { get; set; }
public Class ExplicitInterfaceImpl { get; set; }
private readonly List<Parameter> parameters = new List<Parameter>();
private readonly List<Parameter> parameters = new();
/// <summary>
/// Only applicable to index ([]) properties.
/// </summary>
public List<Parameter> Parameters
{
get { return parameters; }
}
public List<Parameter> Parameters => parameters;
public bool IsIndexer
{
get
{
return GetMethod != null &&
GetMethod.OperatorKind == CXXOperatorKind.Subscript;
}
}
public bool IsIndexer =>
GetMethod is {OperatorKind: CXXOperatorKind.Subscript};
public bool IsSynthetized
{
get
{
return (GetMethod != null && GetMethod.IsSynthetized) ||
public bool IsSynthetized =>
(GetMethod != null && GetMethod.IsSynthetized) ||
(SetMethod != null && SetMethod.IsSynthetized);
}
}
public override T Visit<T>(IDeclVisitor<T> visitor)
{

2
src/CLI/Generator.cs

@ -138,7 +138,7 @@ namespace CppSharp @@ -138,7 +138,7 @@ namespace CppSharp
parserOptions.UnityBuild = options.UnityBuild;
parserOptions.EnableRTTI = options.EnableRTTI;
parserOptions.Setup();
parserOptions.Setup(options.Platform ?? Platform.Host);
if (triple.Contains("linux"))
SetupLinuxOptions(parserOptions);

8
src/Generator/Driver.cs

@ -85,7 +85,7 @@ namespace CppSharp @@ -85,7 +85,7 @@ namespace CppSharp
public void Setup()
{
ValidateOptions();
ParserOptions.Setup();
ParserOptions.Setup(Platform.Host);
Context = new BindingContext(Options, ParserOptions);
Context.LinkerOptions.Setup(ParserOptions.TargetTriple, ParserOptions.LanguageVersion);
Generator = CreateGeneratorFromKind(Options.GeneratorKind);
@ -367,7 +367,7 @@ namespace CppSharp @@ -367,7 +367,7 @@ namespace CppSharp
public bool CompileCode(Module module)
{
var msBuildGenerator = new MSBuildGenerator(Context, module, libraryMappings);
var msBuildGenerator = new MSBuildGenerator(Context, module, LibraryMappings);
msBuildGenerator.Process();
string csproj = Path.Combine(Options.OutputDir,
$"{module.LibraryName}.{msBuildGenerator.FileExtension}");
@ -378,7 +378,7 @@ namespace CppSharp @@ -378,7 +378,7 @@ namespace CppSharp
if (error == 0)
{
Diagnostics.Message($@"Compilation succeeded: {
libraryMappings[module] = Path.Combine(
LibraryMappings[module] = Path.Combine(
Options.OutputDir, $"{module.LibraryName}.dll")}.");
return true;
}
@ -407,7 +407,7 @@ namespace CppSharp @@ -407,7 +407,7 @@ namespace CppSharp
}
private bool hasParsingErrors;
private static readonly Dictionary<Module, string> libraryMappings = new Dictionary<Module, string>();
private static readonly Dictionary<Module, string> LibraryMappings = new();
}
public static class ConsoleDriver

2
src/Generator/Generators/Emscripten/EmscriptenHeaders.cs

@ -7,7 +7,7 @@ namespace CppSharp.Generators.Emscripten @@ -7,7 +7,7 @@ namespace CppSharp.Generators.Emscripten
/// Generates Emscripten Embind C/C++ header files.
/// Embind documentation: https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html
/// </summary>
public class EmscriptenHeaders : EmscriptenCodeGenerator
public sealed class EmscriptenHeaders : EmscriptenCodeGenerator
{
public EmscriptenHeaders(BindingContext context, IEnumerable<TranslationUnit> units)
: base(context, units)

3
src/Generator/Passes/FieldToPropertyPass.cs

@ -33,8 +33,7 @@ namespace CppSharp.Passes @@ -33,8 +33,7 @@ namespace CppSharp.Passes
return false;
}
var @class = field.Namespace as Class;
if (@class == null)
if (field.Namespace is not Class @class)
return false;
// Check if we already have a synthetized property.

57
src/Generator/Passes/GetterSetterToPropertyPass.cs

@ -21,19 +21,16 @@ namespace CppSharp.Passes @@ -21,19 +21,16 @@ namespace CppSharp.Passes
private static void LoadVerbs()
{
var assembly = Assembly.GetAssembly(typeof(GetterSetterToPropertyPass));
using (var resourceStream = GetResourceStream(assembly))
{
using (var streamReader = new StreamReader(resourceStream))
using var resourceStream = GetResourceStream(assembly);
using var streamReader = new StreamReader(resourceStream);
while (!streamReader.EndOfStream)
verbs.Add(streamReader.ReadLine());
}
Verbs.Add(streamReader.ReadLine());
}
private static Stream GetResourceStream(Assembly assembly)
{
var resources = assembly.GetManifestResourceNames();
if (resources.Count() == 0)
if (!resources.Any())
throw new Exception("Cannot find embedded verbs data resource.");
// We are relying on this fact that there is only one resource embedded.
@ -54,13 +51,12 @@ namespace CppSharp.Passes @@ -54,13 +51,12 @@ namespace CppSharp.Passes
return false;
}
protected virtual List<Property> GetProperties(Class @class) =>
new List<Property>();
protected virtual List<Property> GetProperties() => new();
protected IEnumerable<Property> GenerateProperties(Class @class)
{
List<Property> properties = GetProperties(@class);
foreach (Method method in @class.Methods.Where(
var properties = GetProperties();
foreach (var method in @class.Methods.Where(
m => !m.IsConstructor && !m.IsDestructor && !m.IsOperator && m.IsGenerated &&
(properties.All(p => p.GetMethod != m && p.SetMethod != m) ||
m.OriginalFunction != null) &&
@ -72,15 +68,16 @@ namespace CppSharp.Passes @@ -72,15 +68,16 @@ namespace CppSharp.Passes
if (IsGetter(method))
{
string name = GetPropertyName(method.Name);
GetProperty(properties, method, name, method.OriginalReturnType);
CreateOrUpdateProperty(properties, method, name, method.OriginalReturnType);
continue;
}
if (IsSetter(method))
{
string name = GetPropertyNameFromSetter(method.Name);
QualifiedType type = method.Parameters.First(
p => p.Kind == ParameterKind.Regular).QualifiedType;
GetProperty(properties, method, name, type, true);
CreateOrUpdateProperty(properties, method, name, type, true);
}
}
@ -104,7 +101,7 @@ namespace CppSharp.Passes @@ -104,7 +101,7 @@ namespace CppSharp.Passes
continue;
if (Match(firstWord, new[] { "to", "new", "on" }) ||
verbs.Contains(firstWord))
Verbs.Contains(firstWord))
{
property.GetMethod.GenerationKind = GenerationKind.Generate;
@class.Properties.Remove(property);
@ -115,12 +112,10 @@ namespace CppSharp.Passes @@ -115,12 +112,10 @@ namespace CppSharp.Passes
return properties;
}
private static void GetProperty(List<Property> properties, Method method,
private static void CreateOrUpdateProperty(List<Property> properties, Method method,
string name, QualifiedType type, bool isSetter = false)
{
Type underlyingType = GetUnderlyingType(type);
Class @class = (Class)method.Namespace;
Property property = properties.Find(
p => p.Field == null &&
((!isSetter && p.SetMethod?.IsStatic == method.IsStatic) ||
@ -255,14 +250,13 @@ namespace CppSharp.Passes @@ -255,14 +250,13 @@ namespace CppSharp.Passes
private static Type GetUnderlyingType(QualifiedType type)
{
TagType tagType = type.Type as TagType;
if (tagType != null)
if (type.Type is TagType)
return type.Type;
// TODO: we should normally check pointer types for const;
// however, there's some bug, probably in the parser, that returns IsConst = false for "const Type& arg"
// so skip the check for the time being
PointerType pointerType = type.Type as PointerType;
return pointerType != null ? pointerType.Pointee : type.Type;
return type.Type is PointerType pointerType ? pointerType.Pointee : type.Type;
}
private static void CombineComments(Property property)
@ -277,6 +271,7 @@ namespace CppSharp.Passes @@ -277,6 +271,7 @@ namespace CppSharp.Passes
BriefText = getter.Comment.BriefText,
Text = getter.Comment.Text
};
if (getter.Comment.FullComment != null)
{
comment.FullComment = new FullComment();
@ -295,20 +290,18 @@ namespace CppSharp.Passes @@ -295,20 +290,18 @@ namespace CppSharp.Passes
private static string GetPropertyName(string name)
{
var firstWord = GetFirstWord(name);
if (Match(firstWord, new[] { "get" }) &&
(string.Compare(name, firstWord, StringComparison.InvariantCultureIgnoreCase) != 0) &&
!char.IsNumber(name[3]))
{
if (!Match(firstWord, new[] {"get"}) ||
(string.Compare(name, firstWord, StringComparison.InvariantCultureIgnoreCase) == 0) ||
char.IsNumber(name[3])) return name;
if (name.Length == 4)
{
return char.ToLowerInvariant(
name[3]).ToString(CultureInfo.InvariantCulture);
}
return char.ToLowerInvariant(
name[3]).ToString(CultureInfo.InvariantCulture) +
name.Substring(4);
}
return name;
return string.Concat(char.ToLowerInvariant(
name[3]).ToString(CultureInfo.InvariantCulture), name.AsSpan(4));
}
private static string GetPropertyNameFromSetter(string name)
@ -328,7 +321,7 @@ namespace CppSharp.Passes @@ -328,7 +321,7 @@ namespace CppSharp.Passes
private bool IsGetter(Method method) =>
!method.IsDestructor &&
!method.OriginalReturnType.Type.IsPrimitiveType(PrimitiveType.Void) &&
!method.Parameters.Any(p => p.Kind != ParameterKind.IndirectReturnType);
method.Parameters.All(p => p.Kind == ParameterKind.IndirectReturnType);
private static bool IsSetter(Method method)
{
@ -365,6 +358,6 @@ namespace CppSharp.Passes @@ -365,6 +358,6 @@ namespace CppSharp.Passes
return new string(firstWord.ToArray());
}
private static readonly HashSet<string> verbs = new HashSet<string>();
private static readonly HashSet<string> Verbs = new();
}
}

2
src/Generator/Types/Std/Stdlib.CSharp.cs

@ -227,8 +227,6 @@ namespace CppSharp.Types.Std @@ -227,8 +227,6 @@ namespace CppSharp.Types.Std
return (Context.Options.Encoding, nameof(Encoding.ASCII));
if (Context.Options.Encoding == Encoding.UTF8)
return (Context.Options.Encoding, nameof(Encoding.UTF8));
if (Context.Options.Encoding == Encoding.UTF7)
return (Context.Options.Encoding, nameof(Encoding.UTF7));
if (Context.Options.Encoding == Encoding.BigEndianUnicode)
return (Context.Options.Encoding, nameof(Encoding.BigEndianUnicode));
if (Context.Options.Encoding == Encoding.Unicode)

1
src/Generator/Utils/Options.cs

@ -710,7 +710,6 @@ namespace Mono.Options @@ -710,7 +710,6 @@ namespace Mono.Options
get { return this.option; }
}
[SecurityPermission(SecurityAction.LinkDemand, SerializationFormatter = true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);

35
src/Parser/ParserOptions.cs

@ -276,15 +276,21 @@ namespace CppSharp.Parser @@ -276,15 +276,21 @@ namespace CppSharp.Parser
AddSystemIncludeDirs($"{headersPath}/usr/include/linux");
}
public void Setup()
private bool setupDone;
public void Setup(TargetPlatform targetPlatform)
{
SetupArguments();
if (setupDone) return;
SetupArguments(targetPlatform);
if (!NoBuiltinIncludes)
SetupIncludes();
SetupIncludes(targetPlatform);
setupDone = true;
}
private void SetupArguments()
private void SetupArguments(TargetPlatform targetPlatform)
{
LanguageVersion ??= CppSharp.Parser.LanguageVersion.CPP14_GNU;
@ -296,7 +302,7 @@ namespace CppSharp.Parser @@ -296,7 +302,7 @@ namespace CppSharp.Parser
// setup methods, below is generic fallback in case that logic was disabled.
if (NoBuiltinIncludes)
{
switch (Platform.Host)
switch (targetPlatform)
{
case TargetPlatform.MacOS:
case TargetPlatform.Linux:
@ -370,18 +376,21 @@ namespace CppSharp.Parser @@ -370,18 +376,21 @@ namespace CppSharp.Parser
{
get
{
var assemblyDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var assemblyDir = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
if (assemblyDir == null)
throw new InvalidOperationException();
return Path.Combine(assemblyDir, "lib", "clang", ClangVersion, "include");
}
}
private void SetupIncludes()
private void SetupIncludes(TargetPlatform targetPlatform)
{
// Check that the builtin includes folder exists.
if (!Directory.Exists(BuiltinsDir))
throw new Exception($"Clang resource folder 'lib/clang/{ClangVersion}/include' was not found.");
switch (Platform.Host)
switch (targetPlatform)
{
case TargetPlatform.Windows:
SetupMSVC();
@ -392,6 +401,16 @@ namespace CppSharp.Parser @@ -392,6 +401,16 @@ namespace CppSharp.Parser
case TargetPlatform.Linux:
SetupLinux();
break;
case TargetPlatform.Android:
throw new NotImplementedException();
case TargetPlatform.iOS:
case TargetPlatform.WatchOS:
case TargetPlatform.TVOS:
throw new NotImplementedException();
case TargetPlatform.Emscripten:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}

Loading…
Cancel
Save