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

5
src/Bridge/Declaration.cs

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

8
src/Bridge/Field.cs

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

5
src/Bridge/Function.cs

@ -26,14 +26,13 @@ namespace Cxxi
{ {
Usage = ParameterUsage.Unknown; Usage = ParameterUsage.Unknown;
HasDefaultValue = false; HasDefaultValue = false;
IsConst = false;
Conversion = TypeConversionKind.None; 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 ParameterUsage Usage { get; set; }
public bool HasDefaultValue { get; set; } public bool HasDefaultValue { get; set; }
public bool IsConst { get; set; }
public TypeConversionKind Conversion { get; set; } public TypeConversionKind Conversion { get; set; }

31
src/Bridge/Type.cs

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

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

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

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

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

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

@ -66,12 +66,10 @@ namespace Cxxi.Generators.CLI
return s; 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 = param.Type.Visit(this, param.QualifiedType.Qualifiers);
var name = param.Name;
var type = arg.Type.Visit(this, quals);
var name = arg.Name;
if (hasName && !string.IsNullOrEmpty(name)) if (hasName && !string.IsNullOrEmpty(name))
return string.Format("{0} {1}", type, name); return string.Format("{0} {1}", type, name);

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

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

47
src/Parser/Parser.cpp

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

Loading…
Cancel
Save