Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the actions class which performs semantic analysis and |
| 11 | // builds an AST out of a parse stream. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "Sema.h" |
| 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/Lex/Preprocessor.h" |
| 18 | #include "clang/Basic/Diagnostic.h" |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 19 | #include "clang/Parse/Scope.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 20 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 23 | bool Sema::isBuiltinObjcType(TypedefDecl *TD) { |
| 24 | const char *typeName = TD->getIdentifier()->getName(); |
| 25 | return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 || |
Fariborz Jahanian | 66c5dfc | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 26 | strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0; |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Fariborz Jahanian | 1f990d6 | 2008-01-04 00:27:46 +0000 | [diff] [blame^] | 29 | bool Sema::isObjcObjectPointerType(QualType type) const { |
| 30 | if (!type->isPointerType() && !type->isObjcQualifiedIdType()) |
| 31 | return false; |
| 32 | if (type == Context.getObjcIdType() || type == Context.getObjcClassType() || |
| 33 | type->isObjcQualifiedIdType()) |
| 34 | return true; |
| 35 | |
| 36 | while (type->isPointerType()) { |
| 37 | PointerType *pointerType = static_cast<PointerType*>(type.getTypePtr()); |
| 38 | type = pointerType->getPointeeType(); |
| 39 | } |
| 40 | return (type->isObjcInterfaceType() || type->isObjcQualifiedIdType()); |
| 41 | } |
| 42 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 43 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 44 | TUScope = S; |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 45 | if (PP.getLangOptions().ObjC1) { |
| 46 | TypedefType *t; |
Chris Lattner | fb1051b | 2007-10-30 20:57:56 +0000 | [diff] [blame] | 47 | |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 48 | // Add the built-in ObjC types. |
| 49 | t = dyn_cast<TypedefType>(Context.getObjcIdType().getTypePtr()); |
| 50 | t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl()); |
| 51 | TUScope->AddDecl(t->getDecl()); |
| 52 | t = dyn_cast<TypedefType>(Context.getObjcClassType().getTypePtr()); |
| 53 | t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl()); |
| 54 | TUScope->AddDecl(t->getDecl()); |
Fariborz Jahanian | 66c5dfc | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 55 | ObjcInterfaceType *it = dyn_cast<ObjcInterfaceType>(Context.getObjcProtoType()); |
| 56 | ObjcInterfaceDecl *IDecl = it->getDecl(); |
| 57 | IDecl->getIdentifier()->setFETokenInfo(IDecl); |
| 58 | TUScope->AddDecl(IDecl); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 59 | t = dyn_cast<TypedefType>(Context.getObjcSelType().getTypePtr()); |
| 60 | t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl()); |
| 61 | TUScope->AddDecl(t->getDecl()); |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 62 | } |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Steve Naroff | 89307ff | 2007-11-29 23:05:20 +0000 | [diff] [blame] | 65 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt) |
Anders Carlsson | 6c19a04 | 2007-11-30 20:01:38 +0000 | [diff] [blame] | 66 | : PP(pp), Context(ctxt), CurFunctionDecl(0), CurMethodDecl(0) { |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 67 | |
| 68 | // Get IdentifierInfo objects for known functions for which we |
| 69 | // do extra checking. |
| 70 | IdentifierTable& IT = PP.getIdentifierTable(); |
| 71 | |
| 72 | KnownFunctionIDs[ id_printf ] = &IT.get("printf"); |
| 73 | KnownFunctionIDs[ id_fprintf ] = &IT.get("fprintf"); |
| 74 | KnownFunctionIDs[ id_sprintf ] = &IT.get("sprintf"); |
| 75 | KnownFunctionIDs[ id_snprintf ] = &IT.get("snprintf"); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 76 | KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf"); |
Ted Kremenek | e0eb80a | 2007-08-10 21:13:51 +0000 | [diff] [blame] | 77 | KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf"); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 78 | KnownFunctionIDs[ id_vasprintf ] = &IT.get("vasprintf"); |
| 79 | KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf"); |
| 80 | KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf"); |
| 81 | KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf"); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 82 | |
| 83 | if (PP.getLangOptions().ObjC1) { |
| 84 | // Synthesize "typedef struct objc_class *Class;" |
| 85 | RecordDecl *ClassTag = new RecordDecl(Decl::Struct, SourceLocation(), |
| 86 | &IT.get("objc_class"), 0); |
| 87 | QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag)); |
| 88 | TypedefDecl *ClassTypedef = new TypedefDecl(SourceLocation(), |
| 89 | &Context.Idents.get("Class"), |
| 90 | ClassT, 0); |
| 91 | Context.setObjcClassType(ClassTypedef); |
| 92 | |
Fariborz Jahanian | 66c5dfc | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 93 | // Synthesize "@class Protocol; |
| 94 | ObjcInterfaceDecl *ProtocolDecl = new ObjcInterfaceDecl(SourceLocation(), 0, |
| 95 | &Context.Idents.get("Protocol"), true); |
| 96 | Context.setObjcProtoType(Context.getObjcInterfaceType(ProtocolDecl)); |
| 97 | |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 98 | // Synthesize "typedef struct objc_object { Class isa; } *id;" |
| 99 | RecordDecl *ObjectTag = new RecordDecl(Decl::Struct, SourceLocation(), |
| 100 | &IT.get("objc_object"), 0); |
| 101 | FieldDecl *IsaDecl = new FieldDecl(SourceLocation(), 0, |
| 102 | Context.getObjcClassType()); |
| 103 | ObjectTag->defineBody(&IsaDecl, 1); |
| 104 | QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag)); |
| 105 | TypedefDecl *IdTypedef = new TypedefDecl(SourceLocation(), |
| 106 | &Context.Idents.get("id"), |
| 107 | ObjT, 0); |
| 108 | Context.setObjcIdType(IdTypedef); |
| 109 | |
| 110 | // Synthesize "typedef struct objc_selector *SEL;" |
| 111 | RecordDecl *SelTag = new RecordDecl(Decl::Struct, SourceLocation(), |
| 112 | &IT.get("objc_selector"), 0); |
| 113 | QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); |
| 114 | TypedefDecl *SelTypedef = new TypedefDecl(SourceLocation(), |
| 115 | &Context.Idents.get("SEL"), |
| 116 | SelT, 0); |
| 117 | Context.setObjcSelType(SelTypedef); |
Fariborz Jahanian | 66c5dfc | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 118 | |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 119 | } |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 120 | TUScope = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 123 | void Sema::DeleteExpr(ExprTy *E) { |
| 124 | delete static_cast<Expr*>(E); |
| 125 | } |
| 126 | void Sema::DeleteStmt(StmtTy *S) { |
| 127 | delete static_cast<Stmt*>(S); |
| 128 | } |
| 129 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | //===----------------------------------------------------------------------===// |
| 131 | // Helper functions. |
| 132 | //===----------------------------------------------------------------------===// |
| 133 | |
| 134 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 135 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | |
| 139 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 140 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | |
| 144 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 145 | const std::string &Msg2) { |
| 146 | std::string MsgArr[] = { Msg1, Msg2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 147 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 148 | return true; |
| 149 | } |
| 150 | |
| 151 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 152 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 153 | return true; |
| 154 | } |
| 155 | |
| 156 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 157 | SourceRange Range) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 158 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | return true; |
| 160 | } |
| 161 | |
| 162 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 163 | const std::string &Msg2, SourceRange Range) { |
| 164 | std::string MsgArr[] = { Msg1, Msg2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 165 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 166 | return true; |
| 167 | } |
| 168 | |
Chris Lattner | 4667ac3 | 2008-01-03 23:38:43 +0000 | [diff] [blame] | 169 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 170 | const std::string &Msg2, const std::string &Msg3, |
| 171 | SourceRange R1) { |
| 172 | std::string MsgArr[] = { Msg1, Msg2, Msg3 }; |
| 173 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1); |
| 174 | return true; |
| 175 | } |
| 176 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 177 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, |
| 178 | SourceRange R1, SourceRange R2) { |
| 179 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 180 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 181 | return true; |
| 182 | } |
| 183 | |
| 184 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 185 | SourceRange R1, SourceRange R2) { |
| 186 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 187 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 188 | return true; |
| 189 | } |
| 190 | |
| 191 | bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1, |
| 192 | const std::string &Msg2, SourceRange R1, SourceRange R2) { |
| 193 | std::string MsgArr[] = { Msg1, Msg2 }; |
| 194 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 195 | PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 196 | return true; |
| 197 | } |
| 198 | |
| 199 | const LangOptions &Sema::getLangOptions() const { |
| 200 | return PP.getLangOptions(); |
| 201 | } |