Parse and remember types enough that we can pretty print:

void foo(int X) {
  X = __alignof(int);
  X = sizeof(const int** restrict ** volatile*);
}

as:

x = __alignof(int)
x = sizeof(int const **restrict **volatile *)

llvm-svn: 39181
This commit is contained in:
Chris Lattner 2006-11-19 01:48:02 +00:00
parent 558cb292da
commit 33e8a55ed0
9 changed files with 43 additions and 8 deletions

View File

@ -48,6 +48,7 @@ public:
//
TypeRef GetTypeForDeclarator(Declarator &D, Scope *S);
virtual TypeResult ParseTypeName(Scope *S, Declarator &D);
//===--------------------------------------------------------------------===//
// Symbol table / Decl tracking callbacks: SemaDecl.cpp.

View File

@ -304,7 +304,9 @@ Action::ExprResult Sema::
ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
SourceLocation LParenLoc, TypeTy *Ty,
SourceLocation RParenLoc) {
return new SizeOfAlignOfTypeExpr(isSizeof, (Type*)Ty);
// Error parsing type, ignore.
if (Ty == 0) return 0;
return new SizeOfAlignOfTypeExpr(isSizeof, TypeRef::getFromOpaquePtr(Ty));
}

View File

@ -119,3 +119,11 @@ TypeRef Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
return T;
}
Sema::TypeResult Sema::ParseTypeName(Scope *S, Declarator &D) {
// FIXME: Validate Declarator.
TypeRef T = GetTypeForDeclarator(D, S);
return T.getAsOpaquePtr();
}

View File

@ -257,9 +257,11 @@ void StmtPrinter::VisitUnaryOperator(UnaryOperator *Node) {
}
void StmtPrinter::VisitSizeOfAlignOfTypeExpr(SizeOfAlignOfTypeExpr *Node) {
OS << (Node->isSizeOf() ? "sizeof(" : "alignof(");
// FIXME: print type.
OS << "ty)";
OS << (Node->isSizeOf() ? "sizeof(" : "__alignof(");
std::string TypeStr;
Node->getArgumentType()->getAsString(TypeStr);
OS << TypeStr << ")";
}
void StmtPrinter::VisitArraySubscriptExpr(ArraySubscriptExpr *Node) {
PrintExpr(Node->getBase());

View File

@ -48,6 +48,7 @@ public:
//
TypeRef GetTypeForDeclarator(Declarator &D, Scope *S);
virtual TypeResult ParseTypeName(Scope *S, Declarator &D);
//===--------------------------------------------------------------------===//
// Symbol table / Decl tracking callbacks: SemaDecl.cpp.

View File

@ -304,7 +304,9 @@ Action::ExprResult Sema::
ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
SourceLocation LParenLoc, TypeTy *Ty,
SourceLocation RParenLoc) {
return new SizeOfAlignOfTypeExpr(isSizeof, (Type*)Ty);
// Error parsing type, ignore.
if (Ty == 0) return 0;
return new SizeOfAlignOfTypeExpr(isSizeof, TypeRef::getFromOpaquePtr(Ty));
}

View File

@ -119,3 +119,11 @@ TypeRef Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
return T;
}
Sema::TypeResult Sema::ParseTypeName(Scope *S, Declarator &D) {
// FIXME: Validate Declarator.
TypeRef T = GetTypeForDeclarator(D, S);
return T.getAsOpaquePtr();
}

View File

@ -15,12 +15,12 @@
#define LLVM_CLANG_AST_EXPR_H
#include "clang/AST/Stmt.h"
#include "clang/AST/Type.h"
namespace llvm {
namespace clang {
class IdentifierInfo;
class Decl;
class Type;
/// Expr - This represents one expression. Note that Expr's are subclasses of
/// Stmt. This allows an expression to be transparently used any place a Stmt
@ -135,12 +135,13 @@ private:
/// *types*. sizeof(expr) is handled by UnaryOperator.
class SizeOfAlignOfTypeExpr : public Expr {
bool isSizeof; // true if sizeof, false if alignof.
Type *Ty;
TypeRef Ty;
public:
SizeOfAlignOfTypeExpr(bool issizeof, Type *ty) : isSizeof(issizeof), Ty(ty) {
SizeOfAlignOfTypeExpr(bool issizeof, TypeRef ty) : isSizeof(issizeof), Ty(ty){
}
bool isSizeOf() const { return isSizeof; }
TypeRef getArgumentType() const { return Ty; }
virtual void visit(StmtVisitor &Visitor);
};

View File

@ -53,6 +53,12 @@ public:
assert((ThePtr & CVRFlags) == 0 && "Type pointer not 8-byte aligned?");
ThePtr |= Quals;
}
static TypeRef getFromOpaquePtr(void *Ptr) {
TypeRef T;
T.ThePtr = reinterpret_cast<uintptr_t>(Ptr);
return T;
}
unsigned getQualifiers() const {
return ThePtr & CVRFlags;
@ -61,6 +67,10 @@ public:
return reinterpret_cast<Type*>(ThePtr & ~CVRFlags);
}
void *getAsOpaquePtr() const {
return reinterpret_cast<void*>(ThePtr);
}
Type &operator*() const {
return *getTypePtr();
}