Quantcast

Get underlying type of QualType if it's TypedefType

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Get underlying type of QualType if it's TypedefType

Daniel Marjamäki via cfe-dev
Hi,

You can use getCanonicalType() to look through the typedefs, for example:

QualType SrcCanonType = SrcExpr()->getType().getCanonicalType(),
                DestCanonType = DestType.getCanonicalType();

if (const BuiltinType *Src = SrcCanonType->getAs<BuiltinType>())
  if (const BuiltinType *Dest = DestCanonType->getAs<BuiltinType>()) {
    if (Src.getKind() == Dest.getKind()) {
      // do something
    } else {
      // do something else
    }
}

Kirill

> Date: Wed, 12 Apr 2017 23:11:29 +0800
> From: 陳韋任 via cfe-dev <[hidden email]>
> To: [hidden email]
> Subject: [cfe-dev] Get underlying type of QualType if it's TypedefType
> Message-ID:
>         <CAFSLk9eRMBhHqLXR2WGWH7YLRgYe_eXNb0s8yXS6A=[hidden email]>
> Content-Type: text/plain; charset="utf-8"
>
> Hi All,
>
>   I have SrcType and DestType used in explicit cast, and I'd like to check
> if the type conversion is valid in CastOperation::CheckCStyleCast [1]. The
> code snippet below (just get the idea, the code is not runable),
>
>     Builtin *Src = dyn_cast<BuiltinType>(SrcExpr.get()->getType());
>     Builtin *Dest = dyn_cast<BuiltinType>(DestType);
>
>     if (Src.getKind() == Dest.getKind()) {
>       // do something
>     } else {
>       // do something else
>     }
>
> The problem is the SrcType and DestType might be typedef. For example,
>
>     typedef foo FOO;
>
>     FOO b = (FOO)(a);
>
> Src and Dest might be nullptr due to the fail dyn_cast, and I will get
> segfault while calling getKind() upon them.
> I am not familiar with Clang, so is there a better way that I can get the
> underlying type if SrcType and DestType
> are typedef?
>
> Thanks.
>
> [1] https://clang.llvm.org/doxygen/SemaCast_8cpp_source.html
>
> Regards,
> chenwj
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get underlying type of QualType if it's TypedefType

Daniel Marjamäki via cfe-dev
Kirill and Artem,

  My original approach is dyn_cast the type pointer to TypedefType recursively since I need to take
nested typedef into consideration.

    typedef foo FOO;
    typedef FOO MyFoo;

    MyFoo b = (MyFoo)(a); 

The recursive call is kinda of like,

    QualType getUnderlyingType(QualType QT) {

      if (const TypedefType *TT = dyn_cast<TypedefType>(QT)) {
          TypedefNameDef TND = TT->getDecl();
          return getUnderlyingType(TND->getUnderlyingType());
      }

      return QT;
    }

Both of you suggested *canonical* type, I tried and it works.

    QualType SrcCanonType = SrcExpr()->getType().getCanonicalType(),

    const BuiltinType *Src = SrcCanonType->getAs<BuiltinType>()

I guess getCanonicalType() way is preferred?

Regards,
chenwj


2017-04-13 5:51 GMT+08:00 Романенков Кирилл via cfe-dev <[hidden email]>:
Hi,

You can use getCanonicalType() to look through the typedefs, for example:

QualType SrcCanonType = SrcExpr()->getType().getCanonicalType(),
                DestCanonType = DestType.getCanonicalType();

if (const BuiltinType *Src = SrcCanonType->getAs<BuiltinType>())
  if (const BuiltinType *Dest = DestCanonType->getAs<BuiltinType>()) {
    if (Src.getKind() == Dest.getKind()) {
      // do something
    } else {
      // do something else
    }
}

Kirill

> Date: Wed, 12 Apr 2017 23:11:29 +0800
> From: 陳韋任 via cfe-dev <[hidden email]>
> To: [hidden email]
> Subject: [cfe-dev] Get underlying type of QualType if it's TypedefType
> Message-ID:
>         <CAFSLk9eRMBhHqLXR2WGWH7YLRgYe_eXNb0s8yXS6A=[hidden email]>
> Content-Type: text/plain; charset="utf-8"
>
> Hi All,
>
>   I have SrcType and DestType used in explicit cast, and I'd like to check
> if the type conversion is valid in CastOperation::CheckCStyleCast [1]. The
> code snippet below (just get the idea, the code is not runable),
>
>     Builtin *Src = dyn_cast<BuiltinType>(SrcExpr.get()->getType());
>     Builtin *Dest = dyn_cast<BuiltinType>(DestType);
>
>     if (Src.getKind() == Dest.getKind()) {
>       // do something
>     } else {
>       // do something else
>     }
>
> The problem is the SrcType and DestType might be typedef. For example,
>
>     typedef foo FOO;
>
>     FOO b = (FOO)(a);
>
> Src and Dest might be nullptr due to the fail dyn_cast, and I will get
> segfault while calling getKind() upon them.
> I am not familiar with Clang, so is there a better way that I can get the
> underlying type if SrcType and DestType
> are typedef?
>
> Thanks.
>
> [1] https://clang.llvm.org/doxygen/SemaCast_8cpp_source.html
>
> Regards,
> chenwj
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



--

--
Wei-Ren Chen (陳韋任)
Homepage: https://people.cs.nctu.edu.tw/~chenwj

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Get underlying type of QualType if it's TypedefType

Daniel Marjamäki via cfe-dev
Thanks. :-)

2017-04-15 1:40 GMT+08:00 Романенков Кирилл <[hidden email]>:
Yes, I think getCanonicalType() is prefered for among others performance considerations.
 
Kirill.
 
13.04.2017, 15:27, "陳韋任" <[hidden email]>:
Kirill and Artem,
 
  My original approach is dyn_cast the type pointer to TypedefType recursively since I need to take
nested typedef into consideration.
 
    typedef foo FOO;
    typedef FOO MyFoo;
 
    MyFoo b = (MyFoo)(a); 
 
The recursive call is kinda of like,
 
    QualType getUnderlyingType(QualType QT) {
 
      if (const TypedefType *TT = dyn_cast<TypedefType>(QT)) {
          TypedefNameDef TND = TT->getDecl();
          return getUnderlyingType(TND->getUnderlyingType());
      }
 
      return QT;
    }
 
Both of you suggested *canonical* type, I tried and it works.
 
    QualType SrcCanonType = SrcExpr()->getType().getCanonicalType(),

    const BuiltinType *Src = SrcCanonType->getAs<BuiltinType>()
 
I guess getCanonicalType() way is preferred?
 
Regards,
chenwj
 
 
2017-04-13 5:51 GMT+08:00 Романенков Кирилл via cfe-dev <[hidden email]>:
Hi,

You can use getCanonicalType() to look through the typedefs, for example:

QualType SrcCanonType = SrcExpr()->getType().getCanonicalType(),
                DestCanonType = DestType.getCanonicalType();

if (const BuiltinType *Src = SrcCanonType->getAs<BuiltinType>())
  if (const BuiltinType *Dest = DestCanonType->getAs<BuiltinType>()) {
    if (Src.getKind() == Dest.getKind()) {
      // do something
    } else {
      // do something else
    }
}

Kirill

> Date: Wed, 12 Apr 2017 23:11:29 +0800
> From: 陳韋任 via cfe-dev <[hidden email]>
> To: [hidden email]
> Subject: [cfe-dev] Get underlying type of QualType if it's TypedefType
> Message-ID:
>         <CAFSLk9eRMBhHqLXR2WGWH7YLRgYe_eXNb0s8yXS6A=[hidden email]>
> Content-Type: text/plain; charset="utf-8"
>
> Hi All,
>
>   I have SrcType and DestType used in explicit cast, and I'd like to check
> if the type conversion is valid in CastOperation::CheckCStyleCast [1]. The
> code snippet below (just get the idea, the code is not runable),
>
>     Builtin *Src = dyn_cast<BuiltinType>(SrcExpr.get()->getType());
>     Builtin *Dest = dyn_cast<BuiltinType>(DestType);
>
>     if (Src.getKind() == Dest.getKind()) {
>       // do something
>     } else {
>       // do something else
>     }
>
> The problem is the SrcType and DestType might be typedef. For example,
>
>     typedef foo FOO;
>
>     FOO b = (FOO)(a);
>
> Src and Dest might be nullptr due to the fail dyn_cast, and I will get
> segfault while calling getKind() upon them.
> I am not familiar with Clang, so is there a better way that I can get the
> underlying type if SrcType and DestType
> are typedef?
>
> Thanks.
>
> [1] https://clang.llvm.org/doxygen/SemaCast_8cpp_source.html
>
> Regards,
> chenwj
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
 
 
--

--
Wei-Ren Chen (陳韋任)
Homepage: https://people.cs.nctu.edu.tw/~chenwj



--

--
Wei-Ren Chen (陳韋任)
Homepage: https://people.cs.nctu.edu.tw/~chenwj

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Loading...