Browse Source

Merge pull request #271 from ddobrev/def_vals

Some refactoring in preparation for the default values of parameters
pull/272/head
João Matos 11 years ago
parent
commit
6310ce6ecc
  1. 2
      src/AST/Enumeration.cs
  2. 7
      src/AST/Function.cs
  3. 4
      src/AST/Method.cs
  4. 2
      src/CppParser/AST.cpp
  5. 2
      src/CppParser/AST.h
  6. 6
      src/CppParser/Bindings/CLI/AST.cpp
  7. 2
      src/CppParser/Bindings/CLI/AST.h
  8. 15
      src/CppParser/Bindings/CSharp/i686-pc-win32/AST.cs
  9. 48
      src/CppParser/Parser.cpp
  10. 1
      src/CppParser/Parser.h
  11. 27
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  12. 2
      src/Generator/Passes/CheckOperatorsOverloads.cs
  13. 3
      src/Generator/Passes/ConstructorToConversionOperatorPass.cs
  14. 2
      src/Generator/Passes/GenerateAbstractImplementationsPass.cs
  15. 6
      src/Generator/Passes/ObjectOverridesPass.cs

2
src/AST/Enumeration.cs

@ -6,7 +6,7 @@ namespace CppSharp.AST
/// <summary> /// <summary>
/// Represents a C/C++ enumeration declaration. /// Represents a C/C++ enumeration declaration.
/// </summary> /// </summary>
public class Enumeration : Declaration public class Enumeration : DeclarationContext
{ {
[Flags] [Flags]
public enum EnumModifiers public enum EnumModifiers

7
src/AST/Function.cs

@ -88,7 +88,10 @@ namespace CppSharp.AST
public enum FunctionSynthKind public enum FunctionSynthKind
{ {
None, None,
NonMemberOperator NonMemberOperator,
ComplementOperator,
AbstractImplCall,
DefaultValueOverload
} }
public class Function : Declaration, ITypedDecl, IMangledDecl public class Function : Declaration, ITypedDecl, IMangledDecl
@ -109,7 +112,7 @@ namespace CppSharp.AST
Parameters = new List<Parameter>(); Parameters = new List<Parameter>();
ReturnType = function.ReturnType; ReturnType = function.ReturnType;
IsReturnIndirect = function.IsReturnIndirect; IsReturnIndirect = function.IsReturnIndirect;
Parameters.AddRange(function.Parameters); Parameters.AddRange(function.Parameters.Select(p => new Parameter(p)));
IsVariadic = function.IsVariadic; IsVariadic = function.IsVariadic;
IsInline = function.IsInline; IsInline = function.IsInline;
IsPure = function.IsPure; IsPure = function.IsPure;

4
src/AST/Method.cs

@ -86,7 +86,6 @@ namespace CppSharp.AST
IsVirtual = method.IsVirtual; IsVirtual = method.IsVirtual;
IsConst = method.IsConst; IsConst = method.IsConst;
IsImplicit = method.IsImplicit; IsImplicit = method.IsImplicit;
IsSynthetized = method.IsSynthetized;
IsOverride = method.IsOverride; IsOverride = method.IsOverride;
IsProxy = method.IsProxy; IsProxy = method.IsProxy;
Kind = method.Kind; Kind = method.Kind;
@ -94,6 +93,7 @@ namespace CppSharp.AST
IsCopyConstructor = method.IsCopyConstructor; IsCopyConstructor = method.IsCopyConstructor;
IsMoveConstructor = method.IsMoveConstructor; IsMoveConstructor = method.IsMoveConstructor;
Conversion = method.Conversion; Conversion = method.Conversion;
SynthKind = method.SynthKind;
} }
public Method(Function function) public Method(Function function)
@ -109,7 +109,7 @@ namespace CppSharp.AST
public bool IsConst { get; set; } public bool IsConst { get; set; }
public bool IsImplicit { get; set; } public bool IsImplicit { get; set; }
public bool IsExplicit { get; set; } public bool IsExplicit { get; set; }
public bool IsSynthetized { get; set; } public bool IsSynthetized { get { return SynthKind != FunctionSynthKind.None; } }
public bool IsOverride { get; set; } public bool IsOverride { get; set; }
public bool IsProxy { get; set; } public bool IsProxy { get; set; }

2
src/CppParser/AST.cpp

@ -450,7 +450,7 @@ Method::Method()
// Enumeration // Enumeration
Enumeration::Enumeration() : Declaration(DeclarationKind::Enumeration), Enumeration::Enumeration() : DeclarationContext(DeclarationKind::Enumeration),
Modifiers((EnumModifiers)0), Type(0), BuiltinType(0) {} Modifiers((EnumModifiers)0), Type(0), BuiltinType(0) {}
DEF_VECTOR(Enumeration, Enumeration::Item, Items) DEF_VECTOR(Enumeration, Enumeration::Item, Items)

2
src/CppParser/AST.h

@ -548,7 +548,7 @@ struct CS_API Method : public Function
QualifiedType ConversionType; QualifiedType ConversionType;
}; };
struct CS_API Enumeration : public Declaration struct CS_API Enumeration : public DeclarationContext
{ {
DECLARE_DECL_KIND(Enumeration, Enumeration) DECLARE_DECL_KIND(Enumeration, Enumeration)

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

@ -1855,18 +1855,18 @@ void CppSharp::Parser::AST::Enumeration::Item::Value::set(unsigned long long val
} }
CppSharp::Parser::AST::Enumeration::Enumeration(::CppSharp::CppParser::AST::Enumeration* native) CppSharp::Parser::AST::Enumeration::Enumeration(::CppSharp::CppParser::AST::Enumeration* native)
: CppSharp::Parser::AST::Declaration((::CppSharp::CppParser::AST::Declaration*)native) : CppSharp::Parser::AST::DeclarationContext((::CppSharp::CppParser::AST::DeclarationContext*)native)
{ {
} }
CppSharp::Parser::AST::Enumeration::Enumeration(System::IntPtr native) CppSharp::Parser::AST::Enumeration::Enumeration(System::IntPtr native)
: CppSharp::Parser::AST::Declaration(native) : CppSharp::Parser::AST::DeclarationContext(native)
{ {
auto __native = (::CppSharp::CppParser::AST::Enumeration*)native.ToPointer(); auto __native = (::CppSharp::CppParser::AST::Enumeration*)native.ToPointer();
} }
CppSharp::Parser::AST::Enumeration::Enumeration() CppSharp::Parser::AST::Enumeration::Enumeration()
: CppSharp::Parser::AST::Declaration((::CppSharp::CppParser::AST::Declaration*)nullptr) : CppSharp::Parser::AST::DeclarationContext((::CppSharp::CppParser::AST::DeclarationContext*)nullptr)
{ {
NativePtr = new ::CppSharp::CppParser::AST::Enumeration(); NativePtr = new ::CppSharp::CppParser::AST::Enumeration();
} }

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

@ -1325,7 +1325,7 @@ namespace CppSharp
} }
}; };
public ref class Enumeration : CppSharp::Parser::AST::Declaration public ref class Enumeration : CppSharp::Parser::AST::DeclarationContext
{ {
public: public:

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

@ -4222,9 +4222,9 @@ namespace CppSharp
} }
} }
public unsafe partial class Enumeration : CppSharp.Parser.AST.Declaration, IDisposable public unsafe partial class Enumeration : CppSharp.Parser.AST.DeclarationContext, IDisposable
{ {
[StructLayout(LayoutKind.Explicit, Size = 144)] [StructLayout(LayoutKind.Explicit, Size = 240)]
public new struct Internal public new struct Internal
{ {
[FieldOffset(0)] [FieldOffset(0)]
@ -4257,13 +4257,16 @@ namespace CppSharp
[FieldOffset(92)] [FieldOffset(92)]
public void* OriginalPtr; public void* OriginalPtr;
[FieldOffset(120)] [FieldOffset(212)]
public bool IsAnonymous;
[FieldOffset(216)]
public CppSharp.Parser.AST.Enumeration.EnumModifiers Modifiers; public CppSharp.Parser.AST.Enumeration.EnumModifiers Modifiers;
[FieldOffset(124)] [FieldOffset(220)]
public global::System.IntPtr Type; public global::System.IntPtr Type;
[FieldOffset(128)] [FieldOffset(224)]
public global::System.IntPtr BuiltinType; public global::System.IntPtr BuiltinType;
[SuppressUnmanagedCodeSecurity] [SuppressUnmanagedCodeSecurity]
@ -4447,7 +4450,7 @@ namespace CppSharp
public Enumeration() public Enumeration()
: this(IntPtr.Zero) : this(IntPtr.Zero)
{ {
__Instance = Marshal.AllocHGlobal(144); __Instance = Marshal.AllocHGlobal(240);
Internal.ctor_0(__Instance); Internal.ctor_0(__Instance);
} }

48
src/CppParser/Parser.cpp

@ -1349,6 +1349,12 @@ DeclarationContext* Parser::GetNamespace(clang::Decl* D,
DC = WalkClassTemplatePartialSpecialization(CTPSpec); DC = WalkClassTemplatePartialSpecialization(CTPSpec);
continue; continue;
} }
case Decl::Enum:
{
auto CTPSpec = cast<EnumDecl>(Ctx);
DC = WalkEnum(CTPSpec);
continue;
}
default: default:
{ {
StringRef Kind = Ctx->getDeclKindName(); StringRef Kind = Ctx->getDeclKindName();
@ -2006,24 +2012,28 @@ Enumeration* Parser::WalkEnum(clang::EnumDecl* ED)
E->IsIncomplete = false; E->IsIncomplete = false;
for(auto it = ED->enumerator_begin(); it != ED->enumerator_end(); ++it) for(auto it = ED->enumerator_begin(); it != ED->enumerator_end(); ++it)
{ {
clang::EnumConstantDecl* ECD = (*it); E->Items.push_back(*WalkEnumItem(*it));
}
return E;
}
auto EnumItem = Enumeration::Item(); Enumeration::Item* Parser::WalkEnumItem(clang::EnumConstantDecl* ECD)
HandleDeclaration(ECD, &EnumItem); {
auto EnumItem = new Enumeration::Item();
HandleDeclaration(ECD, EnumItem);
EnumItem.Name = ECD->getNameAsString(); EnumItem->Name = ECD->getNameAsString();
auto Value = ECD->getInitVal(); auto Value = ECD->getInitVal();
EnumItem.Value = Value.isSigned() ? Value.getSExtValue() EnumItem->Value = Value.isSigned() ? Value.getSExtValue()
: Value.getZExtValue(); : Value.getZExtValue();
EnumItem->_Namespace = GetNamespace(ECD);
std::string Text; std::string Text;
if (GetDeclText(ECD->getSourceRange(), Text)) if (GetDeclText(ECD->getSourceRange(), Text))
EnumItem.Expression = Text; EnumItem->Expression = Text;
E->Items.push_back(EnumItem); return EnumItem;
}
return E;
} }
//-----------------------------------// //-----------------------------------//
@ -2615,10 +2625,16 @@ Declaration* Parser::WalkDeclaration(clang::Decl* D,
} }
case Decl::Enum: case Decl::Enum:
{ {
EnumDecl* ED = cast<EnumDecl>(D); auto ED = cast<EnumDecl>(D);
Decl = WalkEnum(ED); Decl = WalkEnum(ED);
break; break;
} }
case Decl::EnumConstant:
{
auto ED = cast<EnumConstantDecl>(D);
Decl = WalkEnumItem(ED);
break;
}
case Decl::Function: case Decl::Function:
{ {
FunctionDecl* FD = cast<FunctionDecl>(D); FunctionDecl* FD = cast<FunctionDecl>(D);
@ -2685,9 +2701,17 @@ Declaration* Parser::WalkDeclaration(clang::Decl* D,
NS->Variables.push_back(static_cast<Variable*>(Decl)); NS->Variables.push_back(static_cast<Variable*>(Decl));
break; break;
} }
case Decl::CXXConstructor:
{
auto MD = cast<CXXMethodDecl>(D);
Decl = WalkMethodCXX(MD);
auto NS = GetNamespace(MD);
Decl->_Namespace = NS;
break;
}
// Ignore these declarations since they must have been declared in // Ignore these declarations since they must have been declared in
// a class already. // a class already.
case Decl::CXXConstructor:
case Decl::CXXDestructor: case Decl::CXXDestructor:
case Decl::CXXConversion: case Decl::CXXConversion:
case Decl::CXXMethod: case Decl::CXXMethod:

1
src/CppParser/Parser.h

@ -63,6 +63,7 @@ protected:
bool IgnoreSystemDecls = true, bool CanBeDefinition = false); bool IgnoreSystemDecls = true, bool CanBeDefinition = false);
Declaration* WalkDeclarationDef(clang::Decl* D); Declaration* WalkDeclarationDef(clang::Decl* D);
Enumeration* WalkEnum(clang::EnumDecl* ED); Enumeration* WalkEnum(clang::EnumDecl* ED);
Enumeration::Item* WalkEnumItem(clang::EnumConstantDecl* ECD);
Function* WalkFunction(clang::FunctionDecl* FD, bool IsDependent = false, Function* WalkFunction(clang::FunctionDecl* FD, bool IsDependent = false,
bool AddToNamespace = true); bool AddToNamespace = true);
Class* GetRecord(clang::RecordDecl* Record, bool& IsComplete); Class* GetRecord(clang::RecordDecl* Record, bool& IsComplete);

27
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -471,13 +471,14 @@ namespace CppSharp.Generators.CSharp
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
private ISet<Function> GatherClassInternalFunctions(Class @class) private IEnumerable<Function> GatherClassInternalFunctions(Class @class)
{ {
var functions = new HashSet<Function>(); var functions = new HashSet<Function>();
Action<Method> tryAddOverload = method => Action<Method> tryAddOverload = method =>
{ {
if (method.IsSynthetized) if (method.SynthKind != FunctionSynthKind.NonMemberOperator &&
method.SynthKind != FunctionSynthKind.None)
return; return;
if (method.IsProxy) if (method.IsProxy)
@ -884,9 +885,9 @@ namespace CppSharp.Generators.CSharp
var method = function as Method; var method = function as Method;
if (method != null && method.OperatorKind == CXXOperatorKind.Subscript) if (method != null && method.OperatorKind == CXXOperatorKind.Subscript)
{ {
if (method.IsOverride && method.IsSynthetized) if (method.SynthKind == FunctionSynthKind.AbstractImplCall)
{ {
GenerateVirtualTableFunctionCall(method, @class); GenerateAbstractImplCall(method, @class);
} }
else else
{ {
@ -1010,8 +1011,8 @@ namespace CppSharp.Generators.CSharp
NewLine(); NewLine();
WriteStartBraceIndent(); WriteStartBraceIndent();
var method = function as Method; var method = function as Method;
if (method != null && method.IsOverride && method.IsSynthetized) if (method != null && method.SynthKind == FunctionSynthKind.AbstractImplCall)
GenerateVirtualTableFunctionCall(method, @class); GenerateAbstractImplCall(method, @class);
else else
GenerateInternalFunctionCall(function, function.Parameters, returnType.Type); GenerateInternalFunctionCall(function, function.Parameters, returnType.Type);
} }
@ -1886,7 +1887,7 @@ namespace CppSharp.Generators.CSharp
var hasBaseClass = @class.HasBaseClass && @class.BaseClass.IsRefType; var hasBaseClass = @class.HasBaseClass && @class.BaseClass.IsRefType;
if (hasBaseClass) if (hasBaseClass)
WriteLineIndent(": base(native{0})", WriteLineIndent(": base(native{0})",
@class.Methods.Any(m => !m.IsPure && m.IsOverride && m.IsSynthetized) ? @class.Methods.Any(m => m.SynthKind == FunctionSynthKind.AbstractImplCall) ?
", true" : string.Empty); ", true" : string.Empty);
WriteStartBraceIndent(); WriteStartBraceIndent();
@ -2043,9 +2044,9 @@ namespace CppSharp.Generators.CSharp
{ {
GenerateOperator(method, @class); GenerateOperator(method, @class);
} }
else if (method.IsOverride && method.IsSynthetized) else if (method.SynthKind == FunctionSynthKind.AbstractImplCall)
{ {
GenerateVirtualTableFunctionCall(method, @class); GenerateAbstractImplCall(method, @class);
} }
else else
{ {
@ -2101,14 +2102,14 @@ namespace CppSharp.Generators.CSharp
} }
} }
private void GenerateVirtualTableFunctionCall(Method method, Class @class) private void GenerateAbstractImplCall(Method method, Class @class)
{ {
string delegateId; string delegateId;
Write(GetVirtualCallDelegate(method, @class, out delegateId)); Write(GetAbstractCallDelegate(method, @class, out delegateId));
GenerateFunctionCall(delegateId, method.Parameters, method); GenerateFunctionCall(delegateId, method.Parameters, method);
} }
public string GetVirtualCallDelegate(Method method, Class @class, public string GetAbstractCallDelegate(Method method, Class @class,
out string delegateId) out string delegateId)
{ {
var virtualCallBuilder = new StringBuilder(); var virtualCallBuilder = new StringBuilder();
@ -2129,7 +2130,7 @@ namespace CppSharp.Generators.CSharp
private void GenerateOperator(Method method, Class @class) private void GenerateOperator(Method method, Class @class)
{ {
if (method.IsSynthetized) if (method.SynthKind == FunctionSynthKind.ComplementOperator)
{ {
if (method.Kind == CXXMethodKind.Conversion) if (method.Kind == CXXMethodKind.Conversion)
{ {

2
src/Generator/Passes/CheckOperatorsOverloads.cs

@ -140,7 +140,7 @@ namespace CppSharp.Passes
{ {
Name = Operators.GetOperatorIdentifier(missingKind), Name = Operators.GetOperatorIdentifier(missingKind),
Namespace = @class, Namespace = @class,
IsSynthetized = true, SynthKind = FunctionSynthKind.ComplementOperator,
Kind = CXXMethodKind.Operator, Kind = CXXMethodKind.Operator,
OperatorKind = missingKind, OperatorKind = missingKind,
ReturnType = op.ReturnType, ReturnType = op.ReturnType,

3
src/Generator/Passes/ConstructorToConversionOperatorPass.cs

@ -1,7 +1,6 @@
using System.Linq; using System.Linq;
using CppSharp.AST; using CppSharp.AST;
using CppSharp.AST.Extensions; using CppSharp.AST.Extensions;
using CppSharp.Generators;
namespace CppSharp.Passes namespace CppSharp.Passes
{ {
@ -45,7 +44,7 @@ namespace CppSharp.Passes
Name = Operators.GetOperatorIdentifier(operatorKind), Name = Operators.GetOperatorIdentifier(operatorKind),
Namespace = castFromClass, Namespace = castFromClass,
Kind = CXXMethodKind.Conversion, Kind = CXXMethodKind.Conversion,
IsSynthetized = true, SynthKind = FunctionSynthKind.ComplementOperator,
ConversionType = qualifiedCastToType, ConversionType = qualifiedCastToType,
ReturnType = qualifiedCastToType ReturnType = qualifiedCastToType
}; };

2
src/Generator/Passes/GenerateAbstractImplementationsPass.cs

@ -70,7 +70,7 @@ namespace CppSharp.Passes
OriginalFunction = method, OriginalFunction = method,
IsPure = false, IsPure = false,
IsOverride = true, IsOverride = true,
IsSynthetized = true SynthKind = FunctionSynthKind.AbstractImplCall
}); });
internalImpl.Layout = new ClassLayout(@class.Layout); internalImpl.Layout = new ClassLayout(@class.Layout);

6
src/Generator/Passes/ObjectOverridesPass.cs

@ -119,7 +119,7 @@ namespace CppSharp
Name = "ToString", Name = "ToString",
Namespace = @class, Namespace = @class,
ReturnType = new QualifiedType(stringType), ReturnType = new QualifiedType(stringType),
IsSynthetized = true, SynthKind = FunctionSynthKind.NonMemberOperator,
IsOverride = true, IsOverride = true,
IsProxy = true IsProxy = true
}; };
@ -144,7 +144,7 @@ namespace CppSharp
Namespace = @class, Namespace = @class,
ReturnType = new QualifiedType(new BuiltinType(PrimitiveType.Bool)), ReturnType = new QualifiedType(new BuiltinType(PrimitiveType.Bool)),
Parameters = new List<Parameter> { methodEqualsParam }, Parameters = new List<Parameter> { methodEqualsParam },
IsSynthetized = true, SynthKind = FunctionSynthKind.ComplementOperator,
IsOverride = true, IsOverride = true,
IsProxy = true IsProxy = true
}; };
@ -155,7 +155,7 @@ namespace CppSharp
Name = "GetHashCode", Name = "GetHashCode",
Namespace = @class, Namespace = @class,
ReturnType = new QualifiedType(new BuiltinType(PrimitiveType.Int)), ReturnType = new QualifiedType(new BuiltinType(PrimitiveType.Int)),
IsSynthetized = true, SynthKind = FunctionSynthKind.ComplementOperator,
IsOverride = true, IsOverride = true,
IsProxy = true IsProxy = true
}; };

Loading…
Cancel
Save