From 440632896f0adc06c8d1a570036a10b8d31db9af Mon Sep 17 00:00:00 2001 From: triton Date: Tue, 8 Apr 2014 12:41:50 +0100 Subject: [PATCH] Added support for logical names to the declarations. While at it, I also refactored the logic of Qualified name properties. Now instead of recursion it will gather all the used namespaces and filter them. --- src/AST/Declaration.cs | 95 +++++++++++++++++++++++++++++++++++------- src/AST/Namespace.cs | 24 ++++++++++- 2 files changed, 101 insertions(+), 18 deletions(-) diff --git a/src/AST/Declaration.cs b/src/AST/Declaration.cs index f88cb2ea..008fbcb1 100644 --- a/src/AST/Declaration.cs +++ b/src/AST/Declaration.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; namespace CppSharp.AST { @@ -64,14 +65,11 @@ namespace CppSharp.AST } } - private string name; - public virtual string OriginalName - { - get { return originalName; } - set { originalName = value; } - } + public virtual string OriginalName { get; set; } // Name of the declaration. + private string name; + public virtual string Name { get { return name; } @@ -83,14 +81,64 @@ namespace CppSharp.AST } } + /// + /// The effective name of a declaration is the logical name + /// for the declaration. We need this to make the distiction between + /// the real and the "effective" name of the declaration to properly + /// support things like inline namespaces when handling type maps. + /// + public virtual string LogicalName + { + get { return Name; } + } + + /// + /// The effective original name of a declaration is the logical + /// original name for the declaration. + /// + public virtual string LogicalOriginalName + { + get { return OriginalName; } + } + + private string GetQualifiedName(Func getName, + Func getNamespace) + { + if (Namespace == null) + return getName(this); + + if (Namespace.IsRoot) + return getName(this); + + var namespaces = GatherNamespaces(getNamespace(this)); + namespaces.Reverse(); + + var names = namespaces.Select(getName).ToList(); + names.Add(getName(this)); + names = names.Where(s => !string.IsNullOrWhiteSpace(s)).ToList(); + + return string.Join("::", names); + } + + private List GatherNamespaces(DeclarationContext @namespace) + { + var namespaces = new List(); + + var currentNamespace = @namespace; + while (currentNamespace != null) + { + namespaces.Add(currentNamespace); + currentNamespace = currentNamespace.Namespace; + } + + return namespaces; + } + public string QualifiedName { get { - if (Namespace == null) - return Name; - return Namespace.IsRoot ? Name - : string.Format("{0}::{1}", Namespace.QualifiedName, Name); + return GetQualifiedName(decl => decl.Name, decl => decl.Namespace); } } @@ -98,10 +146,26 @@ namespace CppSharp.AST { get { - if (OriginalNamespace == null) - return OriginalName; - return OriginalNamespace.IsRoot ? OriginalName - : string.Format("{0}::{1}", OriginalNamespace.QualifiedOriginalName, OriginalName); + return GetQualifiedName( + decl => decl.OriginalName, decl => decl.OriginalNamespace); + } + } + + public string QualifiedLogicalName + { + get + { + return GetQualifiedName( + decl => decl.LogicalName, decl => decl.Namespace); + } + } + + public string QualifiedLogicalOriginalName + { + get + { + return GetQualifiedName( + decl => decl.LogicalOriginalName, decl => decl.OriginalNamespace); } } @@ -194,7 +258,6 @@ namespace CppSharp.AST // Pointer to the original declaration from Clang. public IntPtr OriginalPtr; - private string originalName; public List Attributes { get; private set; } @@ -217,7 +280,7 @@ namespace CppSharp.AST : this() { Namespace = declaration.Namespace; - originalName = declaration.OriginalName; + OriginalName = declaration.OriginalName; name = declaration.Name; Comment = declaration.Comment; IgnoreFlags = declaration.IgnoreFlags; diff --git a/src/AST/Namespace.cs b/src/AST/Namespace.cs index c84f2dd4..87487140 100644 --- a/src/AST/Namespace.cs +++ b/src/AST/Namespace.cs @@ -25,7 +25,17 @@ namespace CppSharp.AST public Dictionary Anonymous; // True if the context is inside an extern "C" context. - public bool IsExternCContext; + public bool IsExternCContext; + + public override string LogicalName + { + get { return IsAnonymous ? "" : base.Name; } + } + + public override string LogicalOriginalName + { + get { return IsAnonymous ? "" : base.OriginalName; } + } protected DeclarationContext() { @@ -310,8 +320,18 @@ namespace CppSharp.AST /// /// Represents a C++ namespace. /// - public class Namespace : DeclarationContext + public class Namespace : DeclarationContext { + public override string LogicalName + { + get { return IsInline ? string.Empty : base.Name; } + } + + public override string LogicalOriginalName + { + get { return IsInline ? string.Empty : base.OriginalName; } + } + public bool IsInline; public override T Visit(IDeclVisitor visitor)