Browse Source

Simplified the construction of names by using stacks.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/337/merge
Dimitar Dobrev 9 years ago
parent
commit
6353b4b2a5
  1. 7
      src/AST/Declaration.cs
  2. 5
      src/AST/Namespace.cs
  3. 18
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs

7
src/AST/Declaration.cs

@ -128,7 +128,6 @@ namespace CppSharp.AST
return getName(this); return getName(this);
var namespaces = GatherNamespaces(getNamespace(this)); var namespaces = GatherNamespaces(getNamespace(this));
namespaces.Reverse();
var names = namespaces.Select(getName).ToList(); var names = namespaces.Select(getName).ToList();
names.Add(getName(this)); names.Add(getName(this));
@ -137,14 +136,14 @@ namespace CppSharp.AST
return string.Join(QualifiedNameSeparator, names); return string.Join(QualifiedNameSeparator, names);
} }
public static List<Declaration> GatherNamespaces(DeclarationContext @namespace) private static IEnumerable<Declaration> GatherNamespaces(DeclarationContext @namespace)
{ {
var namespaces = new List<Declaration>(); var namespaces = new Stack<Declaration>();
var currentNamespace = @namespace; var currentNamespace = @namespace;
while (currentNamespace != null) while (currentNamespace != null)
{ {
namespaces.Add(currentNamespace); namespaces.Push(currentNamespace);
currentNamespace = currentNamespace.Namespace; currentNamespace = currentNamespace.Namespace;
} }

5
src/AST/Namespace.cs

@ -88,18 +88,17 @@ namespace CppSharp.AST
public IEnumerable<DeclarationContext> GatherParentNamespaces() public IEnumerable<DeclarationContext> GatherParentNamespaces()
{ {
var children = new List<DeclarationContext>(); var children = new Stack<DeclarationContext>();
var currentNamespace = this; var currentNamespace = this;
while (currentNamespace != null) while (currentNamespace != null)
{ {
if (!(currentNamespace is TranslationUnit)) if (!(currentNamespace is TranslationUnit))
children.Add(currentNamespace); children.Push(currentNamespace);
currentNamespace = currentNamespace.Namespace; currentNamespace = currentNamespace.Namespace;
} }
children.Reverse();
return children; return children;
} }

18
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -668,7 +668,7 @@ namespace CppSharp.Generators.CSharp
public string GetNestedQualifiedName(Declaration decl) public string GetNestedQualifiedName(Declaration decl)
{ {
var names = new List<string>(); var names = new Stack<string>();
Declaration ctx; Declaration ctx;
var specialization = decl as ClassTemplateSpecialization; var specialization = decl as ClassTemplateSpecialization;
@ -678,36 +678,36 @@ namespace CppSharp.Generators.CSharp
if (specialization.OriginalNamespace is Class && if (specialization.OriginalNamespace is Class &&
!(specialization.OriginalNamespace is ClassTemplateSpecialization)) !(specialization.OriginalNamespace is ClassTemplateSpecialization))
{ {
names.Add(string.Format("{0}_{1}", decl.OriginalNamespace.Name, decl.Name)); names.Push(string.Format("{0}_{1}", decl.OriginalNamespace.Name, decl.Name));
ctx = ctx.Namespace ?? ctx; ctx = ctx.Namespace ?? ctx;
} }
else else
{ {
names.Add(decl.Name); names.Push(decl.Name);
} }
} }
else else
{ {
names.Add(decl.Name); names.Push(decl.Name);
ctx = decl.Namespace; ctx = decl.Namespace;
} }
if (decl is Variable && !(decl.Namespace is Class)) if (decl is Variable && !(decl.Namespace is Class))
names.Add(decl.TranslationUnit.FileNameWithoutExtension); names.Push(decl.TranslationUnit.FileNameWithoutExtension);
while (!(ctx is TranslationUnit)) while (!(ctx is TranslationUnit))
{ {
if (!string.IsNullOrWhiteSpace(ctx.Name)) if (!string.IsNullOrWhiteSpace(ctx.Name))
names.Add(ctx.Name); names.Push(ctx.Name);
ctx = ctx.Namespace; ctx = ctx.Namespace;
} }
if (!ctx.TranslationUnit.IsSystemHeader && ctx.TranslationUnit.IsValid && if (!ctx.TranslationUnit.IsSystemHeader && ctx.TranslationUnit.IsValid &&
!string.IsNullOrWhiteSpace(ctx.TranslationUnit.Module.OutputNamespace)) !string.IsNullOrWhiteSpace(ctx.TranslationUnit.Module.OutputNamespace))
names.Add(ctx.TranslationUnit.Module.OutputNamespace); names.Push(ctx.TranslationUnit.Module.OutputNamespace);
names.Reverse(); names.Reverse();
var isInCurrentOutputNamespace = names[0] == Generator.CurrentOutputNamespace; var isInCurrentOutputNamespace = names.Peek() == Generator.CurrentOutputNamespace;
if (isInCurrentOutputNamespace || !FullyQualify) if (isInCurrentOutputNamespace || !FullyQualify)
names.RemoveAt(0); names.Pop();
return (isInCurrentOutputNamespace || !FullyQualify ? return (isInCurrentOutputNamespace || !FullyQualify ?
string.Empty : "global::") + string.Join(".", names); string.Empty : "global::") + string.Join(".", names);

Loading…
Cancel
Save