[cfe-commits] r83116 - in /cfe/trunk: include/clang/AST/Type.h include/clang/Analysis/PathSensitive/MemRegion.h lib/AST/Type.cpp lib/Analysis/RegionStore.cpp lib/Sema/Sema.cpp lib/Sema/SemaDeclCXX.cpp

Douglas Gregor dgregor at apple.com
Tue Sep 29 16:47:41 PDT 2009


On Sep 29, 2009, at 4:03 PM, John McCall wrote:

> Author: rjmccall
> Date: Tue Sep 29 18:03:30 2009
> New Revision: 83116
>
> URL: http://llvm.org/viewvc/llvm-project?rev=83116&view=rev
> Log:
> Desugaring optimizations.  Add single-step desugaring methods to all
> concrete types.  Use unqualified desugaring for getAs<> and sundry.
> Fix a few users to either not desugar or use qualified desugar, as  
> seemed
> appropriate.  Removed Type's qualified desugar method, as it was easy
> to accidentally use instead of QualType's.

Nice.

> +  /// getUnqualifiedDesugaredType() - Return the specified type with
> +  /// any "sugar" removed from the type, removing any typedefs,
> +  /// typeofs, etc., as well as any qualifiers.
> +  const Type *getUnqualifiedDesugaredType() const;
[snip]
> +/// getUnqualifiedDesugaredType - Pull any qualifiers and syntactic
> +/// sugar off the given type.  This should produce an object of the
> +/// same dynamic type as the canonical type.
> +const Type *Type::getUnqualifiedDesugaredType() const {
> +  const Type *Cur = this;
> +
> +  while (true) {
> +    switch (Cur->getTypeClass()) {
> +#define ABSTRACT_TYPE(Class, Parent)
> +#define TYPE(Class, Parent) \
> +    case Class: { \
> +      const Class##Type *Ty = cast<Class##Type>(Cur); \
> +      if (!Ty->isSugared()) return Cur; \
> +      Cur = Ty->desugar().getTypePtr(); \
> +      break; \
> +    }
> +#include "clang/AST/TypeNodes.def"
> +    }
> +  }
> }

I think it's just a minor documentation issue, but this routine won't  
remove *all* qualifiers. For example, I expect that, given

	typedef const int CInt1;
	typedef CInt1 CInt2;
	typedef CInt2 CInt3;

desugaring "CInt3" will get us CInt2, which still points to a const- 
qualified type. That's the right behavior IMO, but I think the comment  
for getUnqualifedDesugaredType should make that clear.

>
> --- cfe/trunk/lib/Sema/Sema.cpp (original)
> +++ cfe/trunk/lib/Sema/Sema.cpp Tue Sep 29 18:03:30 2009
> @@ -44,16 +44,23 @@
>
>     // If this is a sugared type (like a typedef, typeof, etc), then  
> unwrap one
>     // level of the sugar so that the type is more obvious to the  
> user.
> -    QualType DesugaredTy = Ty.getDesugaredType(true);
> +    QualType DesugaredTy = Ty.getDesugaredType();
>
>     if (Ty != DesugaredTy &&
>         // If the desugared type is a vector type, we don't want to  
> expand it,
>         // it will turn into an attribute mess. People want their  
> "vec4".
>         !isa<VectorType>(DesugaredTy) &&
>
> -        // Don't aka just because we saw an elaborated type.
> +        // Don't aka just because we saw an elaborated type...
>         (!isa<ElaboratedType>(Ty) ||
> -         cast<ElaboratedType>(Ty)->getUnderlyingType() !=  
> DesugaredTy) &&
> +         cast<ElaboratedType>(Ty)->desugar() != DesugaredTy) &&
> +
> +        // ...or a qualified name type...
> +        (!isa<QualifiedNameType>(Ty) ||
> +         cast<QualifiedNameType>(Ty)->desugar() != DesugaredTy) &&
> +
> +        // ...or a non-dependent template specialization.
> +        (!isa<TemplateSpecializationType>(Ty) || Ty->isDependentType 
> ()) &&
>
>         // Don't desugar magic Objective-C types.
>         Ty.getUnqualifiedType() != Context.getObjCIdType() &&

Much cleaner than my ForDisplayOnly hack. Thanks!

	- Doug



More information about the cfe-commits mailing list