Skip to content

Commit b36481b

Browse files
committed
Temp
1 parent 658a84a commit b36481b

14 files changed

+1280
-100
lines changed

src/AST/Class.cs

+10
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,16 @@ public Variable FindVariable(string name)
274274
return Variables.FirstOrDefault(m => m.Name == name);
275275
}
276276

277+
public Property FindProperty(string name)
278+
{
279+
return Properties.FirstOrDefault(m => m.Name == name);
280+
}
281+
282+
public Field FindField(string name)
283+
{
284+
return Fields.FirstOrDefault(m => m.Name == name);
285+
}
286+
277287
public override T Visit<T>(IDeclVisitor<T> visitor)
278288
{
279289
return visitor.VisitClassDecl(this);

src/AST/Declaration.cs

+16
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,22 @@ public static IEnumerable<Declaration> GatherNamespaces(DeclarationContext @name
223223
return namespaces;
224224
}
225225

226+
public DeclarationContext GetRootNamespace()
227+
{
228+
var currentNamespace = Namespace;
229+
while (currentNamespace.Namespace != null)
230+
currentNamespace = currentNamespace.Namespace;
231+
232+
return currentNamespace;
233+
}
234+
235+
public void MoveToNamespace(DeclarationContext @namespace)
236+
{
237+
Namespace = @namespace;
238+
OriginalNamespace?.Declarations.Remove(this);
239+
Namespace?.Declarations.Add(this);
240+
}
241+
226242
public string QualifiedName
227243
{
228244
get

src/CppParser/AST.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,6 @@ namespace CppSharp { namespace CppParser { namespace AST {
11531153
kind = DeclarationKind::TranslationUnit;
11541154
}
11551155

1156-
TranslationUnit::~TranslationUnit() {}
11571156
DEF_VECTOR(TranslationUnit, MacroDefinition*, Macros)
11581157

11591158
NativeLibrary::NativeLibrary()

src/CppParser/Bootstrap/Bootstrap.cs

+322-25
Large diffs are not rendered by default.

src/CppParser/Bootstrap/CodeGeneratorHelpers.cs

+184-59
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,44 @@
99

1010
namespace CppSharp
1111
{
12+
class TemplateArgumentHandler
13+
{
14+
public static string GetTemplateArgumentTypeName(TemplateArgument arg, CppTypePrinter printer)
15+
{
16+
switch (arg.Kind)
17+
{
18+
case TemplateArgument.ArgumentKind.Type:
19+
return GetTemplateTypeArgName(arg.Type, printer);
20+
case TemplateArgument.ArgumentKind.Declaration:
21+
return GetTemplateDeclArgName(arg.Declaration, printer);
22+
case TemplateArgument.ArgumentKind.NullPtr:
23+
return "nullptr_t";
24+
case TemplateArgument.ArgumentKind.Integral:
25+
return GetTemplateIntegralArgName(arg.Integral, printer);
26+
default:
27+
throw new NotImplementedException($"Unhandled template argument kind: {arg.Kind}");
28+
}
29+
}
30+
31+
private static string GetTemplateTypeArgName(QualifiedType type, CppTypePrinter printer)
32+
{
33+
var typeStr = type.Type.Visit(printer).Type;
34+
if (type.Type.IsPointer())
35+
return $"{typeStr}*";
36+
return typeStr;
37+
}
38+
39+
private static string GetTemplateDeclArgName(Declaration decl, CppTypePrinter printer)
40+
{
41+
return decl?.Visit(printer).Type ?? "nullptr";
42+
}
43+
44+
private static string GetTemplateIntegralArgName(long value, CppTypePrinter printer)
45+
{
46+
return value.ToString();
47+
}
48+
}
49+
1250
internal static class CodeGeneratorHelpers
1351
{
1452
internal static CppTypePrinter CppTypePrinter;
@@ -31,6 +69,17 @@ @class is "Stmt"
3169
or "OverloadExpr"
3270
or "CoroutineSuspendExpr";
3371

72+
public static bool SkipClass(Class @class)
73+
{
74+
if (!@class.IsGenerated)
75+
return true;
76+
77+
if (@class.Access != AccessSpecifier.Public)
78+
return true;
79+
80+
return false;
81+
}
82+
3483
public static bool SkipProperty(Property property, bool skipBaseCheck = false)
3584
{
3685
if (!property.IsGenerated)
@@ -61,11 +110,22 @@ public static bool SkipProperty(Property property, bool skipBaseCheck = false)
61110

62111
var typeName = property.Type.Visit(CppTypePrinter).Type;
63112

113+
// General properties.
114+
if (typeName.Contains("_iterator") ||
115+
typeName.Contains("_range") ||
116+
property.Name.Contains("_begin") ||
117+
property.Name.Contains("_end") ||
118+
property.Name.Contains("_empty") ||
119+
property.Name.Contains("_size"))
120+
return true;
121+
122+
123+
return false;
64124
//
65125
// Statement properties.
66126
//
67127

68-
if (typeName.Contains("LabelDecl") ||
128+
/*if (typeName.Contains("LabelDecl") ||
69129
typeName.Contains("VarDecl") ||
70130
typeName.Contains("Token") ||
71131
typeName.Contains("CapturedDecl") ||
@@ -77,7 +137,7 @@ public static bool SkipProperty(Property property, bool skipBaseCheck = false)
77137
typeName.Contains("NestedNameSpecifierLoc") ||
78138
typeName.Contains("DeclarationNameInfo") ||
79139
typeName.Contains("DeclGroupRef"))
80-
return true;
140+
return true;*/
81141

82142
//
83143
// Expressions
@@ -209,11 +269,6 @@ public static bool SkipProperty(Property property, bool skipBaseCheck = false)
209269
if (typeName.Contains("StorageDuration"))
210270
return true;
211271

212-
// General properties.
213-
if (typeName.Contains("_iterator") ||
214-
typeName.Contains("_range"))
215-
return true;
216-
217272
if (typeName.Contains("ArrayRef"))
218273
return true;
219274

@@ -246,7 +301,7 @@ public static bool SkipProperty(Property property, bool skipBaseCheck = false)
246301

247302
public static bool SkipMethod(Method method)
248303
{
249-
if (method.Ignore)
304+
if (method.IsGenerated)
250305
return true;
251306

252307
var @class = method.Namespace as Class;
@@ -335,8 +390,14 @@ public static string GetDeclName(Declaration decl, GeneratorKind kind)
335390

336391
if (kind == GeneratorKind.CPlusPlus)
337392
{
338-
if (Generators.C.CCodeGenerator.IsReservedKeyword(name))
393+
if (name == "inline")
394+
{
395+
name = "isInline";
396+
}
397+
else if (Generators.C.CCodeGenerator.IsReservedKeyword(name))
398+
{
339399
name = $"_{name}";
400+
}
340401
}
341402
else if (kind == GeneratorKind.CSharp)
342403
{
@@ -372,7 +433,7 @@ public static AST.Type GetDeclType(AST.Type type,
372433
TypePrinter typePrinter)
373434
{
374435
var qualifiedType = new QualifiedType(type);
375-
if (qualifiedType.Type.IsPointerTo(out TagType tagType))
436+
if (qualifiedType.Type.IsPointerTo(out TagType _))
376437
qualifiedType = qualifiedType.StripConst();
377438

378439
var typeName = qualifiedType.Type.Visit(typePrinter).Type;
@@ -423,61 +484,23 @@ public static string GetDeclTypeName(AST.Type type,
423484
}
424485
}
425486

426-
string className = null;
427-
if (typeName.Contains("FieldDecl"))
428-
className = "Field";
429-
else if (typeName.Contains("NamedDecl"))
430-
className = "Declaration";
431-
else if (typeName.Contains("CXXMethodDecl"))
432-
className = "Method";
433-
else if (typeName.Contains("FunctionDecl"))
434-
className = "Function";
435-
else if (typeName.Contains("FunctionTemplateDecl"))
436-
className = "FunctionTemplate";
437-
else if (typeName is "Decl" or "Decl*")
438-
className = "Declaration";
439-
440-
if (className != null)
441-
return (typePrinter is CppTypePrinter) ? $"{className}*" : className;
442-
443487
return typeName;
444488
}
445489

446490
public static AST.Type GetIteratorType(Method method)
447491
{
448-
var retType = method.ReturnType.Type;
449-
450-
TemplateSpecializationType templateSpecType;
451-
TypedefType typedefType;
452-
TypedefNameDecl typedefNameDecl;
492+
var retType = method.ReturnType.Type.Desugar();
453493

454-
if (retType is TemplateSpecializationType)
455-
{
456-
templateSpecType = retType as TemplateSpecializationType;
457-
typedefType = templateSpecType.Arguments[0].Type.Type as TypedefType;
458-
typedefNameDecl = typedefType.Declaration as TypedefNameDecl;
459-
}
460-
else
461-
{
462-
typedefType = retType as TypedefType;
463-
typedefNameDecl = typedefType.Declaration as TypedefNameDecl;
464-
templateSpecType = typedefNameDecl.Type as TemplateSpecializationType;
465-
typedefType = templateSpecType.Arguments[0].Type.Type as TypedefType;
466-
typedefNameDecl = typedefType.Declaration as TypedefNameDecl;
467-
typedefType = typedefNameDecl.Type as TypedefType;
468-
if (typedefType != null)
469-
typedefNameDecl = typedefType.Declaration as TypedefNameDecl;
470-
}
494+
if (retType is TemplateSpecializationType templateSpecType)
495+
retType = templateSpecType.Arguments[0].Type.Type.Desugar();
471496

472-
var iteratorType = typedefNameDecl.Type;
473-
if (iteratorType.IsPointerTo(out PointerType pointee))
474-
iteratorType = iteratorType.GetPointee();
497+
if (retType.IsPointerTo(out PointerType pointee))
498+
retType = pointee;
475499

476-
return iteratorType;
500+
return retType;
477501
}
478502

479-
public static string GetIteratorTypeName(AST.Type iteratorType,
480-
TypePrinter typePrinter)
503+
public static string GetIteratorTypeName(AST.Type iteratorType, TypePrinter typePrinter)
481504
{
482505
if (iteratorType.IsPointer())
483506
iteratorType = iteratorType.GetFinalPointee();
@@ -494,7 +517,8 @@ public static string GetIteratorTypeName(AST.Type iteratorType,
494517
else if (iteratorTypeName.Contains("StmtIterator"))
495518
iteratorTypeName = "Stmt";
496519

497-
else if (iteratorTypeName.Contains("CastIterator"))
520+
else if (iteratorTypeName.Contains("CastIterator") ||
521+
iteratorTypeName.Contains("DeclContext::"))
498522
{
499523
if (iteratorType is TypedefType typedefType)
500524
iteratorType = typedefType.Declaration.Type;
@@ -509,9 +533,6 @@ public static string GetIteratorTypeName(AST.Type iteratorType,
509533
iteratorTypeName = CleanClangNamespaceFromName(iteratorTypeName);
510534
}
511535

512-
if (iteratorTypeName == "Decl")
513-
iteratorTypeName = "Declaration";
514-
515536
if (typePrinter is CppTypePrinter)
516537
return $"{iteratorTypeName}*";
517538

@@ -547,5 +568,109 @@ public static string FirstLetterToUpperCase(string s)
547568
a[0] = char.ToUpper(a[0]);
548569
return new string(a);
549570
}
571+
572+
public static bool SkipDeclProperty(Property property)
573+
{
574+
if (!property.IsGenerated || property.Access != AccessSpecifier.Public)
575+
return true;
576+
577+
var @class = property.Namespace as Class;
578+
579+
if (@class.GetBaseProperty(property) != null)
580+
return true;
581+
582+
var typeName = property.Type.Visit(CppTypePrinter).Type;
583+
584+
// Skip properties that deal with source locations/ranges
585+
if (typeName.Contains("SourceLocation") || typeName.Contains("SourceRange"))
586+
return true;
587+
588+
// Skip Clang-specific internal properties
589+
if (typeName.Contains("ASTContext") || typeName.Contains("DeclContext"))
590+
return true;
591+
592+
// Skip template-specific properties that are handled separately
593+
if (typeName.Contains("TemplateParameterList") ||
594+
typeName.Contains("TemplateArgument"))
595+
return true;
596+
597+
return false;
598+
}
599+
600+
public static bool SkipTypeProperty(Property property)
601+
{
602+
if (!property.IsGenerated || property.Access != AccessSpecifier.Public)
603+
return true;
604+
605+
var @class = property.Namespace as Class;
606+
607+
if (@class.GetBaseProperty(property) != null)
608+
return true;
609+
610+
var typeName = property.Type.Visit(CppTypePrinter).Type;
611+
612+
// Skip source location properties
613+
if (typeName.Contains("SourceLocation"))
614+
return true;
615+
616+
// Skip internal Clang type properties
617+
if (typeName.Contains("TypeLoc") || typeName.Contains("ASTContext"))
618+
return true;
619+
620+
return false;
621+
}
622+
623+
public static bool IsAbstractType(Class @class) =>
624+
@class.Name is "Type"
625+
or "ArrayType"
626+
or "TagType"
627+
or "FunctionType";
628+
629+
public static bool IsAbstractDecl(Class @class) =>
630+
@class.Name is "Decl"
631+
or "NamedDecl"
632+
or "ValueDecl"
633+
or "TypeDecl"
634+
or "DeclContext";
635+
636+
public static string GetPropertyAccessorName(Property property, bool isGetter)
637+
{
638+
var baseName = FirstLetterToUpperCase(GetDeclName(property));
639+
return isGetter ? $"Get{baseName}" : $"Set{baseName}";
640+
}
641+
642+
public static string GetPropertyTypeName(Property property, bool includeNamespace = true)
643+
{
644+
var typeName = GetDeclTypeName(property);
645+
646+
// Handle special cases
647+
if (typeName.Contains("QualType") ||
648+
(property.Type.IsPointerTo(out TagType tagType) &&
649+
tagType.Declaration?.Name.Contains("Type") == true))
650+
{
651+
return includeNamespace ? $"AST::{typeName}" : typeName;
652+
}
653+
654+
// Handle template types
655+
if (property.Type is TemplateSpecializationType templateType)
656+
{
657+
var templateName = templateType.Template.TemplatedDecl.Name;
658+
return includeNamespace ? $"AST::{templateName}" : templateName;
659+
}
660+
661+
return typeName;
662+
}
663+
664+
public static bool IsTypeProperty(Property property)
665+
{
666+
if (property.Type.IsPointerTo(out TagType tagType))
667+
return tagType.Declaration?.Name.Contains("Type") == true;
668+
669+
var typeName = GetDeclTypeName(property);
670+
return typeName.Contains("QualType") ||
671+
typeName.Contains("Type") ||
672+
(property.Type is TemplateSpecializationType templateType &&
673+
templateType.Template.TemplatedDecl.Name.Contains("Type"));
674+
}
550675
}
551676
}

0 commit comments

Comments
 (0)