Skip to content

Instantly share code, notes, and snippets.

@tritao
Created May 11, 2014 17:47
Show Gist options
  • Save tritao/46efd8b545e4f6833f37 to your computer and use it in GitHub Desktop.
Save tritao/46efd8b545e4f6833f37 to your computer and use it in GitHub Desktop.
From 98ccb772b3edfa0c1c03bb4a5b23c730c2937f76 Mon Sep 17 00:00:00 2001
From: triton <joao@tritao.eu>
Date: Thu, 8 May 2014 15:14:44 +0100
Subject: [PATCH 2/2] Misc changes
---
build/LLVM.lua | 1 +
src/AST/Template.cs | 6 +++-
src/Core/Parser/ASTConverter.cs | 10 ++++--
src/CppParser/AST.cpp | 25 ++++++++++++++
src/CppParser/AST.h | 8 +++++
src/CppParser/Bindings/CLI/AST.cpp | 7 ++++
src/CppParser/Bindings/CLI/AST.h | 9 +++++
src/CppParser/Bindings/ParserGen.cs | 32 +++++++++++-------
src/CppParser/Parser.cpp | 40 +++++++++++++++++++++--
src/Generator/Generators/CLI/CLITypeReferences.cs | 14 +++++---
10 files changed, 130 insertions(+), 22 deletions(-)
diff --git a/build/LLVM.lua b/build/LLVM.lua
index 61d7724..5c188ee 100644
--- a/build/LLVM.lua
+++ b/build/LLVM.lua
@@ -75,6 +75,7 @@ function SetupLLVMLibs()
"clangDriver",
"clangEdit",
"clangFrontend",
+ "clangIndex",
"clangLex",
"clangParse",
"clangSema",
diff --git a/src/AST/Template.cs b/src/AST/Template.cs
index 74b28b2..6cc406c 100644
--- a/src/AST/Template.cs
+++ b/src/AST/Template.cs
@@ -168,7 +168,7 @@ namespace CppSharp.AST
/// </summary>
public class ClassTemplateSpecialization : Class
{
- public ClassTemplate TemplatedDecl;
+ public ClassTemplate TemplatedDecl;
public List<TemplateArgument> Arguments;
@@ -193,6 +193,10 @@ namespace CppSharp.AST
/// </summary>
public class FunctionTemplate : Template
{
+ public FunctionTemplate()
+ {
+ }
+
public FunctionTemplate(Declaration decl)
: base(decl)
{
diff --git a/src/Core/Parser/ASTConverter.cs b/src/Core/Parser/ASTConverter.cs
index 8803e98..75c3957 100644
--- a/src/Core/Parser/ASTConverter.cs
+++ b/src/Core/Parser/ASTConverter.cs
@@ -502,7 +502,6 @@ namespace CppSharp
public override AST.Type VisitTemplateParameterSubstitution(TemplateParameterSubstitutionType type)
{
- throw new NotImplementedException();
var _type = new CppSharp.AST.TemplateParameterSubstitutionType();
VisitType(type, _type);
return _type;
@@ -682,6 +681,10 @@ namespace CppSharp
void VisitDeclaration(Declaration decl, AST.Declaration _decl)
{
+ var usr = decl.USR;
+ if (usr != null)
+ Console.WriteLine(usr);
+
var originalPtr = new IntPtr(decl.OriginalPtr);
if (CheckForDuplicates(decl))
@@ -1289,7 +1292,10 @@ namespace CppSharp
public override AST.Declaration VisitFunctionTemplate(FunctionTemplate decl)
{
- throw new NotImplementedException();
+ var _decl = new AST.FunctionTemplate();
+ VisitTemplate(decl, _decl);
+
+ return _decl;
}
void VisitPreprocessedEntity(PreprocessedEntity entity, AST.PreprocessedEntity _entity)
diff --git a/src/CppParser/AST.cpp b/src/CppParser/AST.cpp
index 35567fd..21a4ee8 100644
--- a/src/CppParser/AST.cpp
+++ b/src/CppParser/AST.cpp
@@ -10,6 +10,10 @@
#include <string>
#include <vector>
+#include <clang/AST/Decl.h>
+#include <clang/Index/USRGeneration.h>
+#include <llvm/ADT/SmallString.h>
+
template<typename T>
static std::vector<T> split(const T & str, const T & delimiters) {
std::vector<T> v;
@@ -113,6 +117,7 @@ Declaration::Declaration(DeclarationKind kind)
, CompleteDeclaration(0)
, DefinitionOrder(0)
, OriginalPtr(0)
+ , OriginalDecl(0)
{
}
@@ -129,6 +134,7 @@ Declaration::Declaration(const Declaration& rhs)
, DefinitionOrder(rhs.DefinitionOrder)
, PreprocessedEntities(rhs.PreprocessedEntities)
, OriginalPtr(rhs.OriginalPtr)
+ , OriginalDecl(rhs.OriginalDecl)
{
}
@@ -136,6 +142,25 @@ DEF_STRING(Declaration, Name)
DEF_STRING(Declaration, DebugText)
DEF_VECTOR(Declaration, PreprocessedEntity*, PreprocessedEntities)
+const char* Declaration::getUSR()
+{
+ if (!OriginalDecl)
+ return 0;
+
+ if (OriginalDecl->getLocStart().isInvalid())
+ return 0;
+
+ if (!USRCache.empty())
+ return USRCache.c_str();
+
+ llvm::SmallString<128> USR;
+ if (clang::index::generateUSRForDecl(OriginalDecl, USR))
+ return 0;
+
+ USRCache = USR.str();
+ return USRCache.c_str();
+}
+
DeclarationContext::DeclarationContext() : IsAnonymous(false),
Declaration(DeclarationKind::DeclarationContext) {}
diff --git a/src/CppParser/AST.h b/src/CppParser/AST.h
index ad12495..95de917 100644
--- a/src/CppParser/AST.h
+++ b/src/CppParser/AST.h
@@ -9,6 +9,8 @@
#include "Helpers.h"
+namespace clang { class Decl; }
+
namespace CppSharp { namespace CppParser { namespace AST {
#pragma region Types
@@ -372,6 +374,12 @@ struct CS_API Declaration
unsigned DefinitionOrder;
VECTOR(PreprocessedEntity*, PreprocessedEntities)
void* OriginalPtr;
+#ifndef CPPSHARP
+ clang::Decl* OriginalDecl;
+#endif
+ const char* getUSR();
+private:
+ std::string USRCache;
};
struct Class;
diff --git a/src/CppParser/Bindings/CLI/AST.cpp b/src/CppParser/Bindings/CLI/AST.cpp
index 635b1d4..f3158a1 100644
--- a/src/CppParser/Bindings/CLI/AST.cpp
+++ b/src/CppParser/Bindings/CLI/AST.cpp
@@ -1135,6 +1135,13 @@ unsigned int CppSharp::Parser::AST::Declaration::PreprocessedEntitiesCount::get(
return __ret;
}
+System::String^ CppSharp::Parser::AST::Declaration::USR::get()
+{
+ auto __ret = ((::CppSharp::CppParser::AST::Declaration*)NativePtr)->getUSR();
+ if (__ret == nullptr) return nullptr;
+ return clix::marshalString<clix::E_UTF8>(__ret);
+}
+
CppSharp::Parser::AST::DeclarationKind CppSharp::Parser::AST::Declaration::Kind::get()
{
return (CppSharp::Parser::AST::DeclarationKind)((::CppSharp::CppParser::AST::Declaration*)NativePtr)->Kind;
diff --git a/src/CppParser/Bindings/CLI/AST.h b/src/CppParser/Bindings/CLI/AST.h
index b5471ce..64b5837 100644
--- a/src/CppParser/Bindings/CLI/AST.h
+++ b/src/CppParser/Bindings/CLI/AST.h
@@ -77,6 +77,10 @@ namespace CppSharp
}
}
+namespace clang
+{
+}
+
namespace CppSharp
{
namespace Parser
@@ -943,6 +947,11 @@ namespace CppSharp
unsigned int get();
}
+ property System::String^ USR
+ {
+ System::String^ get();
+ }
+
property CppSharp::Parser::AST::DeclarationKind Kind
{
CppSharp::Parser::AST::DeclarationKind get();
diff --git a/src/CppParser/Bindings/ParserGen.cs b/src/CppParser/Bindings/ParserGen.cs
index 0a8447f..ff6fc55 100644
--- a/src/CppParser/Bindings/ParserGen.cs
+++ b/src/CppParser/Bindings/ParserGen.cs
@@ -54,6 +54,11 @@ namespace CppSharp
options.Headers.Add("AST.h");
options.Headers.Add("CppParser.h");
options.Libraries.Add("CppSharp.CppParser.lib");
+#if OLD_PARSER
+ options.Defines.Add("CPPSHARP");
+#else
+ options.addDefines("CPPSHARP");
+#endif
if (Triple.Contains("apple"))
SetupMacOptions(options);
@@ -70,14 +75,15 @@ namespace CppSharp
#endif
options.OutputDir = "../../../../src/CppParser/Bindings/";
- options.OutputDir += Kind.ToString();
+ options.OutputDir += Kind.ToString() + 1;
if (Kind == GeneratorKind.CSharp)
options.OutputDir += "/" + options.TargetTriple;
options.GenerateLibraryNamespace = false;
options.CheckSymbols = false;
- options.Verbose = false;
+ options.Verbose = true;
+ //options.DryRun = true;
}
private static void SetupMacOptions(DriverOptions options)
@@ -121,23 +127,25 @@ namespace CppSharp
public static void Main(string[] args)
{
+ Console.BufferHeight = 2000;
+
Console.WriteLine("Generating the C++/CLI parser bindings...");
ConsoleDriver.Run(new ParserGen(GeneratorKind.CLI, "i686-pc-win32",
CppAbi.Microsoft));
Console.WriteLine();
- Console.WriteLine("Generating the C# parser bindings...");
- ConsoleDriver.Run(new ParserGen(GeneratorKind.CSharp, "i686-pc-win32",
- CppAbi.Microsoft));
+ //Console.WriteLine("Generating the C# parser bindings...");
+ //ConsoleDriver.Run(new ParserGen(GeneratorKind.CSharp, "i686-pc-win32",
+ // CppAbi.Microsoft));
- // Uncoment the following lines to enable generation of Mac parser bindings.
- // This is disabled by default for now since it requires a non-trivial
- // environment setup: a copy of the Mac SDK native headers and a recent checkout
- // of libcxx since the one provided by the Mac SDK is not compatible with a recent
- // Clang frontend that we use to parse it.
+ //// Uncoment the following lines to enable generation of Mac parser bindings.
+ //// This is disabled by default for now since it requires a non-trivial
+ //// environment setup: a copy of the Mac SDK native headers and a recent checkout
+ //// of libcxx since the one provided by the Mac SDK is not compatible with a recent
+ //// Clang frontend that we use to parse it.
- ConsoleDriver.Run(new ParserGen(GeneratorKind.CSharp, "i686-apple-darwin12.4.0",
- CppAbi.Itanium));
+ //ConsoleDriver.Run(new ParserGen(GeneratorKind.CSharp, "i686-apple-darwin12.4.0",
+ // CppAbi.Itanium));
}
}
diff --git a/src/CppParser/Parser.cpp b/src/CppParser/Parser.cpp
index 07f9ad9..b99a704 100644
--- a/src/CppParser/Parser.cpp
+++ b/src/CppParser/Parser.cpp
@@ -19,6 +19,7 @@
#include <clang/AST/ASTContext.h>
#include <clang/AST/Comment.h>
#include <clang/AST/DeclTemplate.h>
+#include <clang/Index/USRGeneration.h>
#include <clang/Lex/DirectoryLookup.h>
#include <clang/Lex/HeaderSearch.h>
#include <clang/Lex/PreprocessingRecord.h>
@@ -1029,7 +1030,9 @@ TranslationUnit* Parser::GetTranslationUnit(clang::SourceLocation Loc,
auto Unit = Lib->FindOrCreateModule(File);
if (Unit->OriginalPtr == nullptr)
- Unit->OriginalPtr = (void*) SM.getFileEntryForID(SM.getFileID(Loc));
+ Unit->OriginalPtr = (void*) Unit;
+
+ assert(Unit->OriginalPtr != nullptr);
if (LocKind != SourceLocationKind::Invalid)
Unit->IsSystemHeader = SM.isInSystemHeader(Loc);
@@ -1222,6 +1225,28 @@ static CallingConvention ConvertCallConv(clang::CallingConv CC)
return CallingConvention::Default;
}
+static void DumpTypeLoc(clang::TypeLoc TL, bool Recurse = false)
+{
+ if (Recurse)
+ puts("");
+
+ switch(TL.getTypeLocClass())
+ {
+#define ABSTRACT_TYPE(Type, Parent)
+#define TYPE(Type, Parent) \
+ case clang::TypeLoc::Type: \
+ printf("Type: %s\n", #Type); \
+ break;
+#include "clang/AST/TypeNodes.def"
+ }
+
+ if (!Recurse) return;
+
+ clang::TypeLoc Next = TL;
+ while(Next = Next.getNextTypeLoc())
+ DumpTypeLoc(Next);
+}
+
static ParserIntType ConvertIntType(clang::TargetInfo::IntType IT)
{
switch (IT)
@@ -1451,8 +1476,17 @@ Type* Parser::WalkType(clang::QualType QualType, clang::TypeLoc* TL,
TypeLoc RL;
if (TL && !TL->isNull())
{
+ DumpTypeLoc(*TL);
+
FTL = TL->getAs<FunctionProtoTypeLoc>();
- RL = FTL.getReturnLoc();
+ if (FTL)
+ {
+ RL = FTL.getReturnLoc();
+ }
+ else
+ {
+ RL = TL->getAs<ParenTypeLoc>().getInnerLoc();
+ }
}
auto F = new FunctionType();
@@ -2181,6 +2215,8 @@ void Parser::HandleOriginalText(clang::Decl* D, Declaration* Decl)
void Parser::HandleDeclaration(clang::Decl* D, Declaration* Decl)
{
+ Decl->OriginalDecl = D;
+
if (Decl->OriginalPtr != nullptr)
return;
diff --git a/src/Generator/Generators/CLI/CLITypeReferences.cs b/src/Generator/Generators/CLI/CLITypeReferences.cs
index ffabdea..d5e6072 100644
--- a/src/Generator/Generators/CLI/CLITypeReferences.cs
+++ b/src/Generator/Generators/CLI/CLITypeReferences.cs
@@ -184,11 +184,15 @@ namespace CppSharp.Generators.CLI
public override bool VisitClassDecl(Class @class)
{
if (!VisitDeclaration(@class))
- return false;
-
- if (@class.IsIncomplete && @class.CompleteDeclaration != null)
- @class = (Class) @class.CompleteDeclaration;
-
+ return false;
+
+ if (@class.IsIncomplete)
+ {
+ if (@class.CompleteDeclaration == null)
+ return false;
+ @class = (Class)@class.CompleteDeclaration;
+ }
+
var keywords = @class.IsValueType? "value struct" : "ref class";
var @ref = string.Format("{0} {1};", keywords, @class.Name);
--
1.8.3.msysgit.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment