Browse Source

Big rework of the type system to preserve the qualifiers on types.

pull/1/head
triton 13 years ago
parent
commit
2564730771
  1. 6
      src/Bridge/ASTVisitor.cs
  2. 5
      src/Bridge/Declaration.cs
  3. 8
      src/Bridge/Field.cs
  4. 5
      src/Bridge/Function.cs
  5. 31
      src/Bridge/Type.cs
  6. 4
      src/Generator/Generators/CLI/CLIHeadersTemplate.cs
  7. 5
      src/Generator/Generators/CLI/CLIMarshal.cs
  8. 8
      src/Generator/Generators/CLI/CLITypePrinter.cs
  9. 4
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  10. 47
      src/Parser/Parser.cpp

6
src/Bridge/ASTVisitor.cs

@ -120,7 +120,7 @@ namespace Cxxi @@ -120,7 +120,7 @@ namespace Cxxi
public virtual bool VisitFieldDecl(Field field)
{
return field.Type.Visit(this);
return field.Type.Visit(this, field.QualifiedType.Qualifiers);
}
public virtual bool VisitFunctionDecl(Function function)
@ -141,14 +141,14 @@ namespace Cxxi @@ -141,14 +141,14 @@ namespace Cxxi
public virtual bool VisitParameterDecl(Parameter parameter)
{
return parameter.Type.Visit(this);
return parameter.Type.Visit(this, parameter.QualifiedType.Qualifiers);
}
public virtual bool VisitTypedefDecl(TypedefDecl typedef)
{
if (typedef.Type == null)
return false;
return typedef.Type.Visit(this);
return typedef.Type.Visit(this, typedef.QualifiedType.Qualifiers);
}
public virtual bool VisitEnumDecl(Enumeration @enum)

5
src/Bridge/Declaration.cs

@ -11,6 +11,7 @@ namespace Cxxi @@ -11,6 +11,7 @@ namespace Cxxi
public interface ITypedDecl
{
Type Type { get; }
QualifiedType QualifiedType { get; }
}
/// <summary>
@ -96,8 +97,8 @@ namespace Cxxi @@ -96,8 +97,8 @@ namespace Cxxi
/// </summary>
public class TypedefDecl : Declaration, ITypedDecl
{
/// Type defined.
public Type Type { get; set; }
public Type Type { get { return QualifiedType.Type; } }
public QualifiedType QualifiedType { get; set; }
public override T Visit<T>(IDeclVisitor<T> visitor)
{

8
src/Bridge/Field.cs

@ -5,7 +5,9 @@ namespace Cxxi @@ -5,7 +5,9 @@ namespace Cxxi
/// </summary>
public class Field : Declaration, ITypedDecl
{
public Type Type { get; set; }
public Type Type { get { return QualifiedType.Type; } }
public QualifiedType QualifiedType { get; set; }
public AccessSpecifier Access { get; set; }
public uint Offset { get; set; }
@ -14,10 +16,10 @@ namespace Cxxi @@ -14,10 +16,10 @@ namespace Cxxi
Offset = 0;
}
public Field(string name, Type type, AccessSpecifier access)
public Field(string name, QualifiedType type, AccessSpecifier access)
{
Name = name;
Type = type;
QualifiedType = type;
Access = access;
Offset = 0;
}

5
src/Bridge/Function.cs

@ -26,14 +26,13 @@ namespace Cxxi @@ -26,14 +26,13 @@ namespace Cxxi
{
Usage = ParameterUsage.Unknown;
HasDefaultValue = false;
IsConst = false;
Conversion = TypeConversionKind.None;
}
public Type Type { get; set; }
public Type Type { get { return QualifiedType.Type; } }
public QualifiedType QualifiedType { get; set; }
public ParameterUsage Usage { get; set; }
public bool HasDefaultValue { get; set; }
public bool IsConst { get; set; }
public TypeConversionKind Conversion { get; set; }

31
src/Bridge/Type.cs

@ -3,7 +3,7 @@ @@ -3,7 +3,7 @@
namespace Cxxi
{
/// <summary>
/// Represents a C++ type reference.
/// Represents a C++ type.
/// </summary>
public abstract class Type
{
@ -115,6 +115,9 @@ namespace Cxxi @@ -115,6 +115,9 @@ namespace Cxxi
}
}
/// <summary>
/// Represents C++ type qualifiers.
/// </summary>
public struct TypeQualifiers
{
public bool IsConst;
@ -123,7 +126,21 @@ namespace Cxxi @@ -123,7 +126,21 @@ namespace Cxxi
}
/// <summary>
/// Represents a C++ tag type reference.
/// Represents a qualified C++ type.
/// </summary>
public struct QualifiedType
{
public Type Type { get; set; }
public TypeQualifiers Qualifiers { get; set; }
public override string ToString()
{
return Type.ToString();
}
}
/// <summary>
/// Represents a C++ tag type.
/// </summary>
public class TagType : Type
{
@ -236,13 +253,14 @@ namespace Cxxi @@ -236,13 +253,14 @@ namespace Cxxi
}
}
public Type Pointee;
public QualifiedType QualifiedPointee;
public Type Pointee { get { return QualifiedPointee.Type; } }
public TypeModifier Modifier;
public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals)
{
return visitor.VisitPointerType(this, quals);
return visitor.VisitPointerType(this, QualifiedPointee.Qualifiers);
}
}
@ -322,7 +340,7 @@ namespace Cxxi @@ -322,7 +340,7 @@ namespace Cxxi
}
public ArgumentKind Kind;
public Type Type;
public QualifiedType Type;
public Declaration Declaration;
public long Integral;
}
@ -363,8 +381,7 @@ namespace Cxxi @@ -363,8 +381,7 @@ namespace Cxxi
public override T Visit<T>(ITypeVisitor<T> visitor,
TypeQualifiers quals = new TypeQualifiers())
{
//return visitor.VisitTemplateParameterType(this, quals);
return default(T);
return visitor.VisitTemplateParameterType(this, quals);
}
}

4
src/Generator/Generators/CLI/CLIHeadersTemplate.cs

@ -356,9 +356,7 @@ namespace Cxxi.Generators.CLI @@ -356,9 +356,7 @@ namespace Cxxi.Generators.CLI
public void GenerateFieldProperty(Field field)
{
field.Type.Visit<string>(Type.TypePrinter);
var type = field.Type.Visit(Type.TypePrinter);
var type = field.Type.Visit(Type.TypePrinter, field.QualifiedType.Qualifiers);
WriteLine("property {0} {1};", type, field.Name);
}

5
src/Generator/Generators/CLI/CLIMarshal.cs

@ -180,7 +180,7 @@ namespace Cxxi.Generators.CLI @@ -180,7 +180,7 @@ namespace Cxxi.Generators.CLI
public bool VisitParameterDecl(Parameter parameter)
{
throw new NotImplementedException();
return parameter.Type.Visit(this, parameter.QualifiedType.Qualifiers);
}
public bool VisitTypedefDecl(TypedefDecl typedef)
@ -489,7 +489,8 @@ namespace Cxxi.Generators.CLI @@ -489,7 +489,8 @@ namespace Cxxi.Generators.CLI
{
Context.Parameter = new Parameter
{
Name = Context.ArgName, Type = field.Type
Name = Context.ArgName,
QualifiedType = field.QualifiedType
};
return field.Type.Visit(this);

8
src/Generator/Generators/CLI/CLITypePrinter.cs

@ -66,12 +66,10 @@ namespace Cxxi.Generators.CLI @@ -66,12 +66,10 @@ namespace Cxxi.Generators.CLI
return s;
}
public string GetArgumentString(Parameter arg, bool hasName = true)
public string GetArgumentString(Parameter param, bool hasName = true)
{
var quals = new TypeQualifiers { IsConst = arg.IsConst };
var type = arg.Type.Visit(this, quals);
var name = arg.Name;
var type = param.Type.Visit(this, param.QualifiedType.Qualifiers);
var name = param.Name;
if (hasName && !string.IsNullOrEmpty(name))
return string.Format("{0} {1}", type, name);

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

@ -217,9 +217,7 @@ namespace Cxxi.Generators.CSharp @@ -217,9 +217,7 @@ namespace Cxxi.Generators.CSharp
public string GetArgumentString(Parameter arg, bool hasName)
{
var quals = new TypeQualifiers { IsConst = arg.IsConst };
var type = arg.Type.Visit(this, quals);
var type = arg.Type.Visit(this, arg.QualifiedType.Qualifiers);
var name = arg.Name;
if (hasName && !string.IsNullOrEmpty(name))

47
src/Parser/Parser.cpp

@ -264,6 +264,23 @@ std::string Parser::GetTypeName(const clang::Type* Type) @@ -264,6 +264,23 @@ std::string Parser::GetTypeName(const clang::Type* Type)
return TypeName;
}
Cxxi::TypeQualifiers GetTypeQualifiers(clang::QualType Type)
{
Cxxi::TypeQualifiers quals;
quals.IsConst = Type.isLocalConstQualified();
quals.IsRestrict = Type.isLocalRestrictQualified();
quals.IsVolatile = Type.isVolatileQualified();
return quals;
}
Cxxi::QualifiedType GetQualifiedType(clang::QualType qual, Cxxi::Type^ type)
{
Cxxi::QualifiedType qualType;
qualType.Type = type;
qualType.Qualifiers = GetTypeQualifiers(qual);
return qualType;
}
//-----------------------------------//
static Cxxi::AccessSpecifier ConvertToAccess(clang::AccessSpecifier AS)
@ -509,7 +526,7 @@ Cxxi::Field^ Parser::WalkFieldCXX(clang::FieldDecl* FD) @@ -509,7 +526,7 @@ Cxxi::Field^ Parser::WalkFieldCXX(clang::FieldDecl* FD)
F->Name = marshalString<E_UTF8>(FD->getName());
auto TL = FD->getTypeSourceInfo()->getTypeLoc();
F->Type = WalkType(FD->getType(), &TL);
F->QualifiedType = GetQualifiedType(FD->getType(), WalkType(FD->getType(), &TL));
F->Access = ConvertToAccess(FD->getAccess());
HandleComments(FD, F);
@ -678,7 +695,9 @@ Cxxi::Type^ Parser::WalkType(clang::QualType QualType, clang::TypeLoc* TL, @@ -678,7 +695,9 @@ Cxxi::Type^ Parser::WalkType(clang::QualType QualType, clang::TypeLoc* TL,
P->Modifier = Cxxi::PointerType::TypeModifier::Pointer;
auto Next = TL->getNextTypeLoc();
P->Pointee = WalkType(Pointer->getPointeeType(), &Next);
auto Pointee = Pointer->getPointeeType();
P->QualifiedPointee = GetQualifiedType(Pointee, WalkType(Pointee, &Next));
return P;
}
@ -748,7 +767,7 @@ Cxxi::Type^ Parser::WalkType(clang::QualType QualType, clang::TypeLoc* TL, @@ -748,7 +767,7 @@ Cxxi::Type^ Parser::WalkType(clang::QualType QualType, clang::TypeLoc* TL,
auto PTL = PVD->getTypeSourceInfo()->getTypeLoc();
FA->Name = marshalString<E_UTF8>(PVD->getNameAsString());
FA->Type = WalkType(PVD->getType(), &PTL);
FA->QualifiedType = GetQualifiedType(PVD->getType(), WalkType(PVD->getType(), &PTL));
F->Arguments->Add(FA);
}
@ -816,7 +835,7 @@ Cxxi::Type^ Parser::WalkType(clang::QualType QualType, clang::TypeLoc* TL, @@ -816,7 +835,7 @@ Cxxi::Type^ Parser::WalkType(clang::QualType QualType, clang::TypeLoc* TL,
Arg.Kind = Cxxi::TemplateArgument::ArgumentKind::Type;
TypeLoc ArgTL;
ArgTL = ArgLoc.getTypeSourceInfo()->getTypeLoc();
Arg.Type = WalkType(TA.getAsType(), &ArgTL);
Arg.Type = GetQualifiedType(TA.getAsType(), WalkType(TA.getAsType(), &ArgTL));
break;
}
case TemplateArgument::Declaration:
@ -878,8 +897,10 @@ Cxxi::Type^ Parser::WalkType(clang::QualType QualType, clang::TypeLoc* TL, @@ -878,8 +897,10 @@ Cxxi::Type^ Parser::WalkType(clang::QualType QualType, clang::TypeLoc* TL,
TypeLoc Next;
if (!TL->isNull())
Next = TL->getNextTypeLoc();
P->Pointee = WalkType(LR->getPointeeType(), &Next);
auto Pointee = LR->getPointeeType();
P->QualifiedPointee = GetQualifiedType(Pointee, WalkType(Pointee, &Next));
return P;
}
case Type::RValueReference:
@ -892,8 +913,10 @@ Cxxi::Type^ Parser::WalkType(clang::QualType QualType, clang::TypeLoc* TL, @@ -892,8 +913,10 @@ Cxxi::Type^ Parser::WalkType(clang::QualType QualType, clang::TypeLoc* TL,
TypeLoc Next;
if (!TL->isNull())
Next = TL->getNextTypeLoc();
P->Pointee = WalkType(LR->getPointeeType(), &Next);
auto Pointee = LR->getPointeeType();
P->QualifiedPointee = GetQualifiedType(Pointee, WalkType(Pointee, &Next));
return P;
}
default:
@ -1006,7 +1029,7 @@ void Parser::WalkFunction(clang::FunctionDecl* FD, Cxxi::Function^ F, @@ -1006,7 +1029,7 @@ void Parser::WalkFunction(clang::FunctionDecl* FD, Cxxi::Function^ F,
if (auto TSI = FD->getTypeSourceInfo())
{
TypeLoc TL = TSI->getTypeLoc();
RTL = ((FunctionTypeLoc*) &TL)->getResultLoc();
RTL = TL.getAs<FunctionTypeLoc>().getResultLoc();
}
F->ReturnType = WalkType(FD->getResultType(), &RTL);
@ -1019,12 +1042,11 @@ void Parser::WalkFunction(clang::FunctionDecl* FD, Cxxi::Function^ F, @@ -1019,12 +1042,11 @@ void Parser::WalkFunction(clang::FunctionDecl* FD, Cxxi::Function^ F,
auto P = gcnew Cxxi::Parameter();
P->Name = marshalString<E_UTF8>(VD->getNameAsString());
P->IsConst = VD->getType().isConstQualified();
TypeLoc PTL;
if (auto TSI = VD->getTypeSourceInfo())
PTL = VD->getTypeSourceInfo()->getTypeLoc();
P->Type = WalkType(VD->getType(), &PTL);
P->QualifiedType = GetQualifiedType(VD->getType(), WalkType(VD->getType(), &PTL));
P->HasDefaultValue = VD->hasDefaultArg();
@ -1391,7 +1413,8 @@ Cxxi::Declaration^ Parser::WalkDeclaration(clang::Decl* D, clang::TypeLoc* TL, @@ -1391,7 +1413,8 @@ Cxxi::Declaration^ Parser::WalkDeclaration(clang::Decl* D, clang::TypeLoc* TL,
Typedef = NS->FindTypedef(Name, /*Create=*/true);
auto TTL = TD->getTypeSourceInfo()->getTypeLoc();
Typedef->Type = WalkType(TD->getUnderlyingType(), &TTL);
Typedef->QualifiedType = GetQualifiedType(TD->getUnderlyingType(),
WalkType(TD->getUnderlyingType(), &TTL));
Decl = Typedef;

Loading…
Cancel
Save