[cfe-commits] r39284 - in /cfe/cfe/trunk: AST/ASTContext.cpp AST/SemaType.cpp AST/Type.cpp Parse/ParseDecl.cpp Sema/SemaType.cpp include/clang/AST/ASTContext.h include/clang/AST/Decl.h include/clang/AST/Type.h
sabre at cs.uiuc.edu
sabre at cs.uiuc.edu
Wed Jul 11 09:42:28 PDT 2007
Author: sabre
Date: Wed Jul 11 11:42:28 2007
New Revision: 39284
URL: http://llvm.org/viewvc/llvm-project?rev=39284&view=rev
Log:
Add TaggedType, which represents tagged decls as types. Create these when
converting a declspec with TST = struct/union. Pretty print as well.
Modified:
cfe/cfe/trunk/AST/ASTContext.cpp
cfe/cfe/trunk/AST/SemaType.cpp
cfe/cfe/trunk/AST/Type.cpp
cfe/cfe/trunk/Parse/ParseDecl.cpp
cfe/cfe/trunk/Sema/SemaType.cpp
cfe/cfe/trunk/include/clang/AST/ASTContext.h
cfe/cfe/trunk/include/clang/AST/Decl.h
cfe/cfe/trunk/include/clang/AST/Type.h
Modified: cfe/cfe/trunk/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/ASTContext.cpp?rev=39284&r1=39283&r2=39284&view=diff
==============================================================================
--- cfe/cfe/trunk/AST/ASTContext.cpp (original)
+++ cfe/cfe/trunk/AST/ASTContext.cpp Wed Jul 11 11:42:28 2007
@@ -216,3 +216,20 @@
return Types.back();
}
+/// getTagDeclType - Return the unique reference to the type for the
+/// specified TagDecl (struct/union/class/enum) decl.
+TypeRef ASTContext::getTagDeclType(TagDecl *Decl) {
+ // FIXME: This is obviously braindead!
+ // Unique TypeDecl, to guarantee there is only one TaggedType.
+ for (unsigned i = 0, e = Types.size(); i != e; ++i)
+ if (TaggedType *Ty = dyn_cast<TaggedType>(Types[i]))
+ if (Ty->getDecl() == Decl)
+ return Types[i];
+
+ // FIXME: does this lose qualifiers from the typedef??
+
+ Types.push_back(new TaggedType(Decl, 0));
+ return Types.back();
+}
+
+
Modified: cfe/cfe/trunk/AST/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/SemaType.cpp?rev=39284&r1=39283&r2=39284&view=diff
==============================================================================
--- cfe/cfe/trunk/AST/SemaType.cpp (original)
+++ cfe/cfe/trunk/AST/SemaType.cpp Wed Jul 11 11:42:28 2007
@@ -74,9 +74,17 @@
case DeclSpec::TST_decimal64: // _Decimal64
case DeclSpec::TST_decimal128: // _Decimal128
assert(0 && "FIXME: GNU decimal extensions not supported yet!");
-//case DeclSpec::TST_enum:
-//case DeclSpec::TST_union:
-//case DeclSpec::TST_struct:
+ case DeclSpec::TST_enum:
+ case DeclSpec::TST_union:
+ case DeclSpec::TST_struct: {
+ Decl *D = static_cast<Decl *>(DS.getTypeRep());
+ assert(D && "Didn't get a decl for a enum/union/struct?");
+ assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
+ DS.getTypeSpecSign() == 0 &&
+ "Can't handle qualifiers on typedef names yet!");
+ // TypeQuals handled by caller.
+ return Ctx.getTagDeclType(cast<TagDecl>(D));
+ }
case DeclSpec::TST_typedef: {
Decl *D = static_cast<Decl *>(DS.getTypeRep());
assert(D && "Didn't get a decl for a typedef?");
Modified: cfe/cfe/trunk/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/AST/Type.cpp?rev=39284&r1=39283&r2=39284&view=diff
==============================================================================
--- cfe/cfe/trunk/AST/Type.cpp (original)
+++ cfe/cfe/trunk/AST/Type.cpp Wed Jul 11 11:42:28 2007
@@ -167,11 +167,21 @@
void TypeNameType::getAsString(std::string &InnerString) const {
- if (InnerString.empty()) {
- InnerString = getDecl()->getIdentifier()->getName();
- } else {
- // Prefix the basic type, e.g. 'typedefname X'.
+ if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
InnerString = ' ' + InnerString;
- InnerString = getDecl()->getIdentifier()->getName() + InnerString;
- }
+ InnerString = getDecl()->getIdentifier()->getName() + InnerString;
+}
+
+void TaggedType::getAsString(std::string &InnerString) const {
+ if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
+ InnerString = ' ' + InnerString;
+
+ const char *Kind = getDecl()->getKindName();
+ const char *ID;
+ if (const IdentifierInfo *II = getDecl()->getIdentifier())
+ ID = II->getName();
+ else
+ ID = "<anonymous>";
+
+ InnerString = std::string(Kind) + " " + ID + InnerString;
}
Modified: cfe/cfe/trunk/Parse/ParseDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Parse/ParseDecl.cpp?rev=39284&r1=39283&r2=39284&view=diff
==============================================================================
--- cfe/cfe/trunk/Parse/ParseDecl.cpp (original)
+++ cfe/cfe/trunk/Parse/ParseDecl.cpp Wed Jul 11 11:42:28 2007
@@ -408,10 +408,11 @@
void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
assert((Tok.getKind() == tok::kw_struct ||
Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
- SourceLocation StartLoc = ConsumeToken();
DeclSpec::TST TagType =
Tok.getKind() == tok::kw_union ? DeclSpec::TST_union : DeclSpec::TST_struct;
+ SourceLocation StartLoc = ConsumeToken();
+
// If attributes exist after tag, parse them.
if (Tok.getKind() == tok::kw___attribute)
ParseAttributes();
Modified: cfe/cfe/trunk/Sema/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Sema/SemaType.cpp?rev=39284&r1=39283&r2=39284&view=diff
==============================================================================
--- cfe/cfe/trunk/Sema/SemaType.cpp (original)
+++ cfe/cfe/trunk/Sema/SemaType.cpp Wed Jul 11 11:42:28 2007
@@ -74,9 +74,17 @@
case DeclSpec::TST_decimal64: // _Decimal64
case DeclSpec::TST_decimal128: // _Decimal128
assert(0 && "FIXME: GNU decimal extensions not supported yet!");
-//case DeclSpec::TST_enum:
-//case DeclSpec::TST_union:
-//case DeclSpec::TST_struct:
+ case DeclSpec::TST_enum:
+ case DeclSpec::TST_union:
+ case DeclSpec::TST_struct: {
+ Decl *D = static_cast<Decl *>(DS.getTypeRep());
+ assert(D && "Didn't get a decl for a enum/union/struct?");
+ assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
+ DS.getTypeSpecSign() == 0 &&
+ "Can't handle qualifiers on typedef names yet!");
+ // TypeQuals handled by caller.
+ return Ctx.getTagDeclType(cast<TagDecl>(D));
+ }
case DeclSpec::TST_typedef: {
Decl *D = static_cast<Decl *>(DS.getTypeRep());
assert(D && "Didn't get a decl for a typedef?");
Modified: cfe/cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/AST/ASTContext.h?rev=39284&r1=39283&r2=39284&view=diff
==============================================================================
--- cfe/cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/cfe/trunk/include/clang/AST/ASTContext.h Wed Jul 11 11:42:28 2007
@@ -66,6 +66,10 @@
/// getTypeDeclType - Return the unique reference to the type for the
/// specified typename decl.
TypeRef getTypeDeclType(TypeDecl *Decl);
+
+ /// getTagDeclType - Return the unique reference to the type for the
+ /// specified TagDecl (struct/union/class/enum) decl.
+ TypeRef getTagDeclType(TagDecl *Decl);
private:
void InitBuiltinTypes();
Modified: cfe/cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/AST/Decl.h?rev=39284&r1=39283&r2=39284&view=diff
==============================================================================
--- cfe/cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/cfe/trunk/include/clang/AST/Decl.h Wed Jul 11 11:42:28 2007
@@ -190,6 +190,16 @@
TagDecl(Kind DK, SourceLocation L, IdentifierInfo *Id) : Decl(DK, L, Id) {}
public:
+ const char *getKindName() const {
+ switch (getKind()) {
+ default: assert(0 && "Unknown TagDecl!");
+ case Struct: return "struct";
+ case Union: return "union";
+ case Class: return "class";
+ case Enum: return "enum";
+ }
+ }
+
// Implement isa/cast/dyncast/etc.
static bool classof(const Decl *D) {
return D->getKind() == Struct || D->getKind() == Union ||
Modified: cfe/cfe/trunk/include/clang/AST/Type.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/AST/Type.h?rev=39284&r1=39283&r2=39284&view=diff
==============================================================================
--- cfe/cfe/trunk/include/clang/AST/Type.h (original)
+++ cfe/cfe/trunk/include/clang/AST/Type.h Wed Jul 11 11:42:28 2007
@@ -23,6 +23,7 @@
namespace clang {
class ASTContext;
class TypeDecl;
+ class TagDecl;
class Type;
/// TypeRef - For efficiency, we don't store CVR-qualified types as nodes on
@@ -162,7 +163,7 @@
class Type {
public:
enum TypeClass {
- Builtin, Pointer, Array, FunctionNoProto, FunctionProto, TypeName
+ Builtin, Pointer, Array, FunctionNoProto, FunctionProto, TypeName, Tagged
};
private:
Type *CanonicalType;
@@ -367,6 +368,22 @@
};
+class TaggedType : public Type {
+ TagDecl *Decl;
+ TaggedType(TagDecl *D, Type *can) : Type(Tagged, can), Decl(D) {}
+ friend class ASTContext; // ASTContext creates these.
+public:
+
+ TagDecl *getDecl() const { return Decl; }
+
+ virtual void getAsString(std::string &InnerString) const;
+
+ static bool classof(const Type *T) { return T->getTypeClass() == Tagged; }
+ static bool classof(const TaggedType *) { return true; }
+};
+
+
+
/// ...
// TODO: When we support C++, we should have types for uses of template with
More information about the cfe-commits
mailing list