Skip to content

Commit e10fc01

Browse files
committed
Changed type maps to only return types - no strings.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 76d8182 commit e10fc01

File tree

13 files changed

+122
-151
lines changed

13 files changed

+122
-151
lines changed

src/AST/Type.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,13 @@ public override object Clone()
11191119
}
11201120
}
11211121

1122+
public class CustomType : UnsupportedType
1123+
{
1124+
public CustomType(string description) : base(description)
1125+
{
1126+
}
1127+
}
1128+
11221129
#region Primitives
11231130

11241131
/// <summary>

src/Generator.Tests/ASTTestFixture.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ protected void ParseLibrary(params string[] files)
3434
if (!Driver.ParseCode())
3535
throw new Exception("Error parsing the code");
3636

37+
Driver.SetupTypeMaps();
3738
AstContext = Driver.Context.ASTContext;
3839
new CleanUnitPass { Context = Driver.Context }.VisitASTContext(AstContext);
3940
new ResolveIncompleteDeclsPass { Context = Driver.Context }.VisitASTContext(AstContext);

src/Generator/BindingContext.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,24 @@ namespace CppSharp.Generators
77
{
88
public class BindingContext
99
{
10-
public DriverOptions Options { get; private set; }
10+
public DriverOptions Options { get; }
1111
public ParserOptions ParserOptions { get; set; }
1212

1313
public ASTContext ASTContext { get; set; }
1414
public ParserTargetInfo TargetInfo { get; set; }
1515

16-
public SymbolContext Symbols { get; private set; }
17-
public TypeMapDatabase TypeMaps { get; private set; }
16+
public SymbolContext Symbols { get; }
17+
public TypeMapDatabase TypeMaps { get; set; }
1818

19-
public PassBuilder<TranslationUnitPass> TranslationUnitPasses { get; private set; }
20-
public PassBuilder<GeneratorOutputPass> GeneratorOutputPasses { get; private set; }
19+
public PassBuilder<TranslationUnitPass> TranslationUnitPasses { get; }
20+
public PassBuilder<GeneratorOutputPass> GeneratorOutputPasses { get; }
2121

2222
public BindingContext(DriverOptions options, ParserOptions parserOptions = null)
2323
{
2424
Options = options;
2525
ParserOptions = parserOptions;
2626

2727
Symbols = new SymbolContext();
28-
TypeMaps = new TypeMapDatabase();
2928

3029
TranslationUnitPasses = new PassBuilder<TranslationUnitPass>(this);
3130
GeneratorOutputPasses = new PassBuilder<GeneratorOutputPass>(this);

src/Generator/Driver.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using CppSharp.Passes;
1414
using CppSharp.Utils;
1515
using Microsoft.CSharp;
16+
using CppSharp.Types;
1617

1718
namespace CppSharp
1819
{
@@ -68,10 +69,12 @@ public void Setup()
6869
ValidateOptions();
6970
ParserOptions.Setup();
7071
Context = new BindingContext(Options, ParserOptions);
71-
Context.TypeMaps.SetupTypeMaps(Options.GeneratorKind);
7272
Generator = CreateGeneratorFromKind(Options.GeneratorKind);
7373
}
7474

75+
public void SetupTypeMaps() =>
76+
Context.TypeMaps = new TypeMapDatabase(Context.ASTContext, Options.GeneratorKind);
77+
7578
void OnSourceFileParsed(IEnumerable<string> files, ParserResult result)
7679
{
7780
OnFileParsed(files, result);
@@ -434,6 +437,7 @@ public static void Run(ILibrary library)
434437
Diagnostics.Message("Processing code...");
435438

436439
driver.SetupPasses(library);
440+
driver.SetupTypeMaps();
437441

438442
library.Preprocess(driver, driver.Context.ASTContext);
439443

src/Generator/Generators/CLI/CLITypePrinter.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override TypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals
2727
{
2828
typeMap.Type = tag;
2929
var typePrinterContext = new TypePrinterContext { Type = tag };
30-
return typeMap.CLISignature(typePrinterContext);
30+
return typeMap.CLISignatureType(typePrinterContext).ToString();
3131
}
3232

3333
Declaration decl = tag.Declaration;
@@ -202,11 +202,11 @@ public override TypePrinterResult VisitTypedefType(TypedefType typedef,
202202
{
203203
typeMap.Type = typedef;
204204
var typePrinterContext = new TypePrinterContext { Type = typedef };
205-
return typeMap.CLISignature(typePrinterContext);
205+
return typeMap.CLISignatureType(typePrinterContext).ToString();
206206
}
207207

208208
FunctionType func;
209-
if (decl.Type.IsPointerTo<FunctionType>(out func))
209+
if (decl.Type.IsPointerTo(out func))
210210
{
211211
// TODO: Use SafeIdentifier()
212212
return string.Format("{0}^", VisitDeclaration(decl));
@@ -228,7 +228,7 @@ public override TypePrinterResult VisitTemplateSpecializationType(
228228
typeMap.Declaration = decl;
229229
typeMap.Type = template;
230230
var typePrinterContext = new TypePrinterContext { Type = template };
231-
return typeMap.CLISignature(typePrinterContext);
231+
return typeMap.CLISignatureType(typePrinterContext).ToString();
232232
}
233233

234234
return decl.Name;
@@ -289,6 +289,11 @@ public override TypePrinterResult VisitCILType(CILType type, TypeQualifiers qual
289289
return result;
290290
}
291291

292+
public override TypePrinterResult VisitUnsupportedType(UnsupportedType type, TypeQualifiers quals)
293+
{
294+
return type.Description;
295+
}
296+
292297
public override TypePrinterResult VisitDeclaration(Declaration decl)
293298
{
294299
var names = new List<string>();

src/Generator/Generators/CSharp/CSharpSources.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2757,7 +2757,7 @@ public void GenerateFunctionCall(string functionName, List<Parameter> parameters
27572757
Type = indirectRetType.Type.Desugar()
27582758
};
27592759

2760-
WriteLine("{0} {1};", typeMap.CSharpSignature(typePrinterContext),
2760+
WriteLine("{0} {1};", typeMap.CSharpSignatureType(typePrinterContext),
27612761
Helpers.ReturnIdentifier);
27622762
}
27632763
else

src/Generator/Generators/CSharp/CSharpTypePrinter.cs

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,7 @@ public override TypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals
4242
Type = tag
4343
};
4444

45-
string type = typeMap.CSharpSignature(typePrinterContext);
46-
if (!string.IsNullOrEmpty(type))
47-
{
48-
return new TypePrinterResult
49-
{
50-
Type = type,
51-
TypeMap = typeMap
52-
};
53-
}
45+
return typeMap.CSharpSignatureType(typePrinterContext).ToString();
5446
}
5547

5648
return base.VisitTagType(tag, quals);
@@ -290,15 +282,7 @@ public override TypePrinterResult VisitTypedefType(TypedefType typedef,
290282
Type = typedef
291283
};
292284

293-
string type = typeMap.CSharpSignature(typePrinterContext);
294-
if (!string.IsNullOrEmpty(type))
295-
{
296-
return new TypePrinterResult
297-
{
298-
Type = type,
299-
TypeMap = typeMap
300-
};
301-
}
285+
return typeMap.CSharpSignatureType(typePrinterContext).ToString();
302286
}
303287

304288
FunctionType func = decl.Type as FunctionType;
@@ -350,17 +334,7 @@ public override TypePrinterResult VisitTemplateSpecializationType(
350334
MarshalKind = MarshalKind
351335
};
352336

353-
var type = typeMap.CSharpSignature(typePrinterContext);
354-
if (!string.IsNullOrEmpty(type))
355-
{
356-
return new TypePrinterResult
357-
{
358-
Type = type,
359-
TypeMap = typeMap
360-
};
361-
}
362-
363-
return decl.Visit(this);
337+
return typeMap.CSharpSignatureType(typePrinterContext).ToString();
364338
}
365339

366340
public override TypePrinterResult VisitDependentTemplateSpecializationType(
@@ -408,7 +382,38 @@ public override TypePrinterResult VisitPackExpansionType(PackExpansionType type,
408382

409383
public override TypePrinterResult VisitCILType(CILType type, TypeQualifiers quals)
410384
{
411-
return type.Type.FullName;
385+
switch (System.Type.GetTypeCode(type.Type))
386+
{
387+
case TypeCode.Boolean:
388+
return "bool";
389+
case TypeCode.Char:
390+
return "char";
391+
case TypeCode.SByte:
392+
return "sbyte";
393+
case TypeCode.Byte:
394+
return "byte";
395+
case TypeCode.Int16:
396+
return "short";
397+
case TypeCode.UInt16:
398+
return "ushort";
399+
case TypeCode.Int32:
400+
return "int";
401+
case TypeCode.UInt32:
402+
return "uint";
403+
case TypeCode.Int64:
404+
return "long";
405+
case TypeCode.UInt64:
406+
return "ulong";
407+
case TypeCode.Single:
408+
return "float";
409+
case TypeCode.Double:
410+
return "double";
411+
case TypeCode.Decimal:
412+
return "decimal";
413+
case TypeCode.String:
414+
return "string";
415+
}
416+
return $"global::{type.Type.FullName}";
412417
}
413418

414419
public static void GetPrimitiveTypeWidth(PrimitiveType primitive,
@@ -707,6 +712,11 @@ public override TypePrinterResult VisitVectorType(VectorType vectorType,
707712
return vectorType.ElementType.Visit(this);
708713
}
709714

715+
public override TypePrinterResult VisitUnsupportedType(UnsupportedType type, TypeQualifiers quals)
716+
{
717+
return type.Description;
718+
}
719+
710720
private static string GetParameterUsage(ParameterUsage usage)
711721
{
712722
switch (usage)

src/Generator/Generators/TypePrinter.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
using CppSharp.AST;
2-
using CppSharp.AST.Extensions;
3-
using CppSharp.Types;
42
using System;
53
using System.Collections.Generic;
6-
using System.Linq;
74

85
namespace CppSharp.Generators
96
{
107
public class TypePrinterResult
118
{
129
public string Type;
13-
public TypeMap TypeMap;
1410
public string NameSuffix;
1511

1612
public static implicit operator TypePrinterResult(string type)

0 commit comments

Comments
 (0)