Browse Source

Added support for representing and parsing source locations.

pull/266/merge
triton 11 years ago
parent
commit
43dc88b5de
  1. 3
      src/AST/Declaration.cs
  2. 86
      src/AST/SourceLocation.cs
  3. 1
      src/Core/Parser/ASTConverter.cs
  4. 2
      src/CppParser/AST.cpp
  5. 2
      src/CppParser/AST.h
  6. 12
      src/CppParser/Bindings/CLI/AST.cpp
  7. 7
      src/CppParser/Bindings/CLI/AST.h
  8. 32
      src/CppParser/Bindings/CLI/Sources.cpp
  9. 37
      src/CppParser/Bindings/CLI/Sources.h
  10. 615
      src/CppParser/Bindings/CSharp/i686-pc-win32/AST.cs
  11. 123
      src/CppParser/Bindings/CSharp/i686-pc-win32/Sources.cs
  12. 8
      src/CppParser/Bindings/ParserGen.cs
  13. 2
      src/CppParser/Helpers.h
  14. 1
      src/CppParser/Parser.cpp
  15. 15
      src/CppParser/Sources.cpp
  16. 21
      src/CppParser/Sources.h

3
src/AST/Declaration.cs

@ -53,7 +53,8 @@ namespace CppSharp.AST @@ -53,7 +53,8 @@ namespace CppSharp.AST
/// </summary>
public abstract class Declaration : INamedDecl
{
public SourceLocation Location;
private DeclarationContext @namespace;
public DeclarationContext OriginalNamespace;

86
src/AST/SourceLocation.cs

@ -1,6 +1,90 @@ @@ -1,6 +1,90 @@
namespace CppSharp.AST
namespace CppSharp
{
/// <summary>
/// Encodes a location in the source.
/// The SourceManager can decode this to get at the full include stack,
/// line and column information.
/// </summary>
public struct SourceLocation
{
private const uint MacroIDBit = 1U << 31;
public SourceLocation(uint id)
{
ID = id;
}
public readonly uint ID;
public bool IsFileID
{
get { return (ID & MacroIDBit) == 0; }
}
public bool IsMacroID
{
get { return (ID & MacroIDBit) != 0; }
}
/// <summary>
/// Return true if this is a valid SourceLocation object.
/// </summary>
public bool IsValid
{
get { return ID != 0; }
}
/// <summary>
/// Return true if this is an invalid SourceLocation object.
/// Invalid SourceLocations are often used when events have no corresponding
/// location in the source (e.g. a diagnostic is required for a command line
/// option).
/// </summary>
public bool IsInvalid
{
get { return ID == 0; }
}
/// <summary>
/// Offset into the source manager's global input view.
/// </summary>
public uint Offset
{
get { return ID & ~MacroIDBit; }
}
public static bool operator ==(SourceLocation a, SourceLocation b)
{
return a.ID == b.ID;
}
public static bool operator !=(SourceLocation a, SourceLocation b)
{
return !(a == b);
}
public bool Equals(SourceLocation other)
{
return ID == other.ID;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is SourceLocation && Equals((SourceLocation)obj);
}
public override int GetHashCode()
{
return (int)ID;
}
public override string ToString()
{
if (IsInvalid)
return "<invalid>";
return IsMacroID ? "Macro ID: " + ID : "File ID: " + Offset;
}
}
}

1
src/Core/Parser/ASTConverter.cs

@ -718,6 +718,7 @@ namespace CppSharp @@ -718,6 +718,7 @@ namespace CppSharp
_decl.Access = VisitAccessSpecifier(decl.Access);
_decl.Name = decl.Name;
_decl.Namespace = Visit(decl._Namespace) as AST.DeclarationContext;
_decl.Location = new SourceLocation(decl.Location.ID);
_decl.DebugText = decl.DebugText;
_decl.IsIncomplete = decl.IsIncomplete;
_decl.IsDependent = decl.IsDependent;

2
src/CppParser/AST.cpp

@ -122,6 +122,7 @@ Declaration::Declaration(DeclarationKind kind) @@ -122,6 +122,7 @@ Declaration::Declaration(DeclarationKind kind)
: Kind(kind)
, Access(AccessSpecifier::Public)
, _Namespace(0)
, Location(0)
, Comment(0)
, IsIncomplete(false)
, IsDependent(false)
@ -135,6 +136,7 @@ Declaration::Declaration(const Declaration& rhs) @@ -135,6 +136,7 @@ Declaration::Declaration(const Declaration& rhs)
: Kind(rhs.Kind)
, Access(rhs.Access)
, _Namespace(rhs._Namespace)
, Location(rhs.Location.ID)
, Name(rhs.Name)
, Comment(rhs.Comment)
, DebugText(rhs.DebugText)

2
src/CppParser/AST.h

@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
#pragma once
#include "Helpers.h"
#include "Sources.h"
namespace CppSharp { namespace CppParser { namespace AST {
@ -367,6 +368,7 @@ struct CS_API Declaration @@ -367,6 +368,7 @@ struct CS_API Declaration
DeclarationKind Kind;
AccessSpecifier Access;
DeclarationContext* _Namespace;
SourceLocation Location;
STRING(Name)
RawComment* Comment;
STRING(DebugText)

12
src/CppParser/Bindings/CLI/AST.cpp

@ -1205,6 +1205,18 @@ void CppSharp::Parser::AST::Declaration::_Namespace::set(CppSharp::Parser::AST:: @@ -1205,6 +1205,18 @@ void CppSharp::Parser::AST::Declaration::_Namespace::set(CppSharp::Parser::AST::
((::CppSharp::CppParser::AST::Declaration*)NativePtr)->_Namespace = (::CppSharp::CppParser::AST::DeclarationContext*)value->NativePtr;
}
CppSharp::Parser::SourceLocation CppSharp::Parser::AST::Declaration::Location::get()
{
return CppSharp::Parser::SourceLocation((::CppSharp::CppParser::SourceLocation*)&((::CppSharp::CppParser::AST::Declaration*)NativePtr)->Location);
}
void CppSharp::Parser::AST::Declaration::Location::set(CppSharp::Parser::SourceLocation value)
{
auto _marshal0 = ::CppSharp::CppParser::SourceLocation();
_marshal0.ID = value.ID;
((::CppSharp::CppParser::AST::Declaration*)NativePtr)->Location = _marshal0;
}
CppSharp::Parser::AST::RawComment^ CppSharp::Parser::AST::Declaration::Comment::get()
{
return (((::CppSharp::CppParser::AST::Declaration*)NativePtr)->Comment == nullptr) ? nullptr : gcnew CppSharp::Parser::AST::RawComment((::CppSharp::CppParser::AST::RawComment*)((::CppSharp::CppParser::AST::Declaration*)NativePtr)->Comment);

7
src/CppParser/Bindings/CLI/AST.h

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
#include "CppSharp.h"
#include <AST.h>
#include "Sources.h"
namespace CppSharp
{
@ -986,6 +987,12 @@ namespace CppSharp @@ -986,6 +987,12 @@ namespace CppSharp
void set(CppSharp::Parser::AST::DeclarationContext^);
}
property CppSharp::Parser::SourceLocation Location
{
CppSharp::Parser::SourceLocation get();
void set(CppSharp::Parser::SourceLocation);
}
property CppSharp::Parser::AST::RawComment^ Comment
{
CppSharp::Parser::AST::RawComment^ get();

32
src/CppParser/Bindings/CLI/Sources.cpp

@ -0,0 +1,32 @@ @@ -0,0 +1,32 @@
#include "Sources.h"
using namespace System;
using namespace System::Runtime::InteropServices;
CppSharp::Parser::SourceLocation::SourceLocation(::CppSharp::CppParser::SourceLocation* native)
{
__ID = native->ID;
}
CppSharp::Parser::SourceLocation::SourceLocation(System::IntPtr native)
{
auto __native = (::CppSharp::CppParser::SourceLocation*)native.ToPointer();
__ID = __native->ID;
}
CppSharp::Parser::SourceLocation::SourceLocation(unsigned int ID)
{
::CppSharp::CppParser::SourceLocation _native(ID);
this->ID = _native.ID;
}
unsigned int CppSharp::Parser::SourceLocation::ID::get()
{
return __ID;
}
void CppSharp::Parser::SourceLocation::ID::set(unsigned int value)
{
__ID = value;
}

37
src/CppParser/Bindings/CLI/Sources.h

@ -0,0 +1,37 @@ @@ -0,0 +1,37 @@
#pragma once
#include "CppSharp.h"
#include <Sources.h>
namespace CppSharp
{
namespace Parser
{
value struct SourceLocation;
value struct SourceManager;
}
}
namespace CppSharp
{
namespace Parser
{
public value struct SourceLocation
{
public:
SourceLocation(::CppSharp::CppParser::SourceLocation* native);
SourceLocation(System::IntPtr native);
SourceLocation(unsigned int ID);
property unsigned int ID
{
unsigned int get();
void set(unsigned int);
}
private:
unsigned int __ID;
};
}
}

615
src/CppParser/Bindings/CSharp/i686-pc-win32/AST.cs

File diff suppressed because it is too large Load Diff

123
src/CppParser/Bindings/CSharp/i686-pc-win32/Sources.cs

@ -0,0 +1,123 @@ @@ -0,0 +1,123 @@
//----------------------------------------------------------------------------
// This is autogenerated code by CppSharp.
// Do not edit this file or all your changes will be lost after re-generation.
//----------------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;
using System.Security;
namespace CppSharp
{
namespace Parser
{
public unsafe partial struct SourceLocation
{
[StructLayout(LayoutKind.Explicit, Size = 4)]
public struct Internal
{
[FieldOffset(0)]
public uint ID;
[SuppressUnmanagedCodeSecurity]
[DllImport("CppSharp.CppParser.dll", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.ThisCall,
EntryPoint="??0SourceLocation@CppParser@CppSharp@@QAE@XZ")]
internal static extern global::System.IntPtr ctor_0(global::System.IntPtr instance);
[SuppressUnmanagedCodeSecurity]
[DllImport("CppSharp.CppParser.dll", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.ThisCall,
EntryPoint="??0SourceLocation@CppParser@CppSharp@@QAE@I@Z")]
internal static extern global::System.IntPtr ctor_1(global::System.IntPtr instance, uint ID);
[SuppressUnmanagedCodeSecurity]
[DllImport("CppSharp.CppParser.dll", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.ThisCall,
EntryPoint="??0SourceLocation@CppParser@CppSharp@@QAE@ABU012@@Z")]
internal static extern global::System.IntPtr cctor_2(global::System.IntPtr instance, global::System.IntPtr _0);
}
internal SourceLocation(SourceLocation.Internal* native)
: this(new global::System.IntPtr(native))
{
}
internal SourceLocation(SourceLocation.Internal native)
: this(&native)
{
}
public SourceLocation(global::System.IntPtr native, bool isInternalImpl = false) : this()
{
var __ptr = (Internal*)native.ToPointer();
ID = __ptr->ID;
}
internal Internal ToInternal()
{
var __native = new CppSharp.Parser.SourceLocation.Internal();
var __ptr = &__native;
__native.ID = ID;
return __native;
}
internal void FromInternal(Internal* native)
{
var __ptr = native;
ID = __ptr->ID;
}
public SourceLocation(uint ID)
: this()
{
var __fixedInstance = ToInternal();
Internal.ctor_1(new global::System.IntPtr(&__fixedInstance), ID);
FromInternal(&__fixedInstance);
}
public uint ID
{
get;
set
;
}
}
public unsafe partial struct SourceManager
{
[StructLayout(LayoutKind.Explicit, Size = 1)]
public struct Internal
{
[SuppressUnmanagedCodeSecurity]
[DllImport("CppSharp.CppParser.dll", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.ThisCall,
EntryPoint="??0SourceManager@CppParser@CppSharp@@QAE@ABU012@@Z")]
internal static extern global::System.IntPtr cctor_1(global::System.IntPtr instance, global::System.IntPtr _0);
}
internal SourceManager(SourceManager.Internal* native)
: this(new global::System.IntPtr(native))
{
}
internal SourceManager(SourceManager.Internal native)
: this(&native)
{
}
public SourceManager(global::System.IntPtr native, bool isInternalImpl = false) : this()
{
var __ptr = (Internal*)native.ToPointer();
}
internal Internal ToInternal()
{
var __native = new CppSharp.Parser.SourceManager.Internal();
var __ptr = &__native;
return __native;
}
internal void FromInternal(Internal* native)
{
var __ptr = native;
}
}
}
}

8
src/CppParser/Bindings/ParserGen.cs

@ -51,8 +51,12 @@ namespace CppSharp @@ -51,8 +51,12 @@ namespace CppSharp
options.Abi = Abi;
options.LibraryName = "CppSharp.CppParser.dll";
options.GeneratorKind = Kind;
options.Headers.Add("AST.h");
options.Headers.Add("CppParser.h");
options.Headers.AddRange(new string[]
{
"AST.h",
"Sources.h",
"CppParser.h"
});
options.Libraries.Add("CppSharp.CppParser.lib");
if (Triple.Contains("apple"))

2
src/CppParser/Helpers.h

@ -22,6 +22,8 @@ @@ -22,6 +22,8 @@
#endif
#define CS_ABSTRACT
#define CS_VALUE_TYPE
/** We use these macros to workaround the lack of good standard C++
* containers/string support in the C# binding backend. */

1
src/CppParser/Parser.cpp

@ -2415,6 +2415,7 @@ void Parser::HandleDeclaration(clang::Decl* D, Declaration* Decl) @@ -2415,6 +2415,7 @@ void Parser::HandleDeclaration(clang::Decl* D, Declaration* Decl)
Decl->OriginalPtr = (void*) D;
Decl->USR = GetDeclUSR(D);
Decl->Location = SourceLocation(D->getLocation().getRawEncoding());
if (Decl->PreprocessedEntities.empty() && !D->isImplicit())
{

15
src/CppParser/Sources.cpp

@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
#include "Sources.h"
namespace CppSharp { namespace CppParser {
SourceLocation::SourceLocation()
: ID(0)
{
}
SourceLocation::SourceLocation(unsigned ID)
: ID(ID)
{
}
} }

21
src/CppParser/Sources.h

@ -0,0 +1,21 @@ @@ -0,0 +1,21 @@
/************************************************************************
*
* CppSharp
* Licensed under the simplified BSD license. All rights reserved.
*
************************************************************************/
#pragma once
#include "Helpers.h"
namespace CppSharp { namespace CppParser {
struct CS_API CS_VALUE_TYPE SourceLocation
{
SourceLocation();
SourceLocation(unsigned ID);
unsigned ID;
};
} }
Loading…
Cancel
Save