什么是Clang中的规范类型? [英] What are canonical types in Clang?

查看:160
本文介绍了什么是Clang中的规范类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于clang的简单标头解析器,并且从某些来源获得了typedef.

I have a simple header parser based on clang and I get the typedefs from some source.

struct _poire {
int g;
tomate rouge;
};
typedef struct _poire kudamono;

解析后,我得到一个clang::TypedefDecl,然后用clang::TypedefDecl::getUnderlyingType()

After parsing this I have a clang::TypedefDecl then I get the clang::QualType of the typedef with clang::TypedefDecl::getUnderlyingType()

如果使用getAsString方法,使用QualType可以找到"struct _poire" std::string.一切都好.

With the QualType if I use the getAsString method I can find the "struct _poire" std::string. All this is Ok.

问题是,如果我尝试查看此类型是否为规范类型,则使用QualType::isCanonical(),它将返回false.

The problem is if I try to see if this type is a canonical type, with QualType::isCanonical(), it returns false.

因此,我尝试使用QualType::getCanonicalType().getAsString()获取规范类型,并且它返回相同的字符串"struct _poire".

So I try to get the canonical type with QualType::getCanonicalType().getAsString() and it returns the same string "struct _poire".

根据类型为 http://clang.llvm.org/docs/InternalsManual的clang参考. html#canonical-types ,我认为isCanonical()应该返回 如果不涉及typedef,则为true.

according to the clang reference on type http://clang.llvm.org/docs/InternalsManual.html#canonical-types , I thought that the isCanonical() should return true when no typedef is involved.

那么什么是真正的规范类型?

So what are really canonical type?

推荐答案

经过进一步的调查和clang邮件列表中的一个问题,我认为我已经弄清楚什么是规范类型.

After further investigations and a question in the clang mailing list, I think I have figured out what is a canonical type.

首先,重要的是不要专注于QualType以便理解规范类型.看这个(代码/伪代码):

Firstly it 's important to not focus on the QualType in order to understand Canonical Type. Look this (code /pseudocode):

源文件:

typedef struct _poire kudamono; 

c语代码:

QualType t = clang::TypedefDecl::getUnderlyingType()

t.getAsString() // "struct _poire"
t.isCanonical() // false
t.getTypePtr()->getTypeClassName() // ElaboredType

c = t.getCanonicalType()
c.getAsString() // "struct _poire"
c.isCanonical() // true
c.getTypePtr()->getTypeClassName() // RecordType

即使

c和t具有相同的字符串表示形式,它们也不是相同的QualType. QualType用于将限定符("const","volatile" ...)与clang类型关联. Clang Types类很多,因为clang需要跟踪用户指定的类型以进行诊断.( http://clang.llvm.org/doxygen/classclang_1_1Type.html )

c and t are not the same QualType even if they have the same string representation. QualType are used to associate qualifiers ("const", "volatile"...) with a clang type. There are a lot of Clang Types classes because clang needs to keep tracks of the user-specified types for diagnostics.( http://clang.llvm.org/docs/InternalsManual.html#the-type-class-and-its-subclasses and http://clang.llvm.org/doxygen/classclang_1_1Type.html )

所使用的clang类型在很大程度上取决于与源文件中的C/C ++类型关联的语法糖或修饰符.

The clang types used depends heavily on the syntaxic sugars or modifiers associated with the C/C++ types in the source file.

在上面的示例中,QualType t与ElaboratedType相关联.此类型允许跟踪源代码中编写的类型名称.但是规范的QualType与RecordType相关联.

In the exemple above, the QualType t is associated with an ElaboratedType. This type allows to keep track of the type name as written in the source code. But the canonical QualType is associated with a RecordType.

另一个例子: 源文件:

Another example: source file:

typedef struct _poire kudamono;
typedef kudamono tabemono;

c语代码:

QualType t = clang::TypedefDecl::getUnderlyingType()
t.getAsString() // "kudamono"
t.isCanonical() // false
t.getTypePtr()->getTypeClassName() // TypedefType

c = t.getCanonicalType()
c.getAsString() // "struct _poire"
c.isCanonical() // true
c.getTypePtr()->getTypeClassName() // RecordType

在这里,我们可以看到typedef的基础类型被记录为"kudamono"和TypedefType,而不是"struct _poire"被记录为ElaboratedType.

Here we can see that the underlying type of the typedef is recorded as "kudamono" a TypedefType and not "struct _poire" an ElaboratedType.

TypedefType"kudamono"的规范类型是RecordType"struct _poire".

The canonical type for the TypedefType "kudamono" is a RecordType "struct _poire".

我从clang邮件列表中获得的另一个示例( http://article.gmane.org/gmane.comp.compilers.clang.devel/38371/match=canonical+type ):

Another examples that I have had from the clang mailing-list ( http://article.gmane.org/gmane.comp.compilers.clang.devel/38371/match=canonical+type ):

考虑:

int (x);

x的类型不是BuiltinType;这是一个ParenType,其规范类型为BuiltinType.并给予

The type of x is not a BuiltinType; it's a ParenType whose canonical type is a BuiltinType. And given

struct X { int n; };
struct X x;

x的类型可能会表示为ElaboratedType,其规范类型为RecordType.

the type of x will probably be represented as an ElaboratedType whose canonical type is a RecordType.

因此,lang语中的规范类型是不与任何语法糖或修饰符或typedef(例如BuiltinType或RecordType)相关联的类型的类.其他类型的类型(例如ParentType,TypedefType或ElaboratedType)用于跟踪用户类型以进行诊断(错误消息...).

So the canonical Type in clang are classes of types that are not associated with any syntaxic sugars or modifiers or typedef (like BuiltinType or RecordType). Other classes of types (like ParentType, TypedefType or ElaboratedType) are used to keep tracks of the user type for diagnostics (error message ...).

这篇关于什么是Clang中的规范类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆