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" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
| 20 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 21 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 24 | bool Sema::isBuiltinObjCType(TypedefDecl *TD) { |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 25 | const char *typeName = TD->getIdentifier()->getName(); |
| 26 | return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 || |
Fariborz Jahanian | 66c5dfc | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 27 | strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0; |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 30 | static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) |
| 31 | { |
| 32 | if (C.getLangOptions().CPlusPlus) |
| 33 | return CXXRecordDecl::Create(C, TagDecl::TK_struct, |
| 34 | C.getTranslationUnitDecl(), |
| 35 | SourceLocation(), &C.Idents.get(Name), 0); |
| 36 | else |
| 37 | return RecordDecl::Create(C, TagDecl::TK_struct, |
| 38 | C.getTranslationUnitDecl(), |
| 39 | SourceLocation(), &C.Idents.get(Name), 0); |
| 40 | } |
| 41 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 42 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 43 | TUScope = S; |
Argyrios Kyrtzidis | ef17782 | 2008-04-17 14:40:12 +0000 | [diff] [blame] | 44 | CurContext = Context.getTranslationUnitDecl(); |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 45 | if (!PP.getLangOptions().ObjC1) return; |
| 46 | |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 47 | // Synthesize "typedef struct objc_selector *SEL;" |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 48 | RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector"); |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 49 | PushOnScopeChains(SelTag, TUScope); |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 50 | |
| 51 | QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 52 | TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext, |
| 53 | SourceLocation(), |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 54 | &Context.Idents.get("SEL"), |
Chris Lattner | c63e660 | 2008-03-15 21:32:50 +0000 | [diff] [blame] | 55 | SelT, 0); |
Argyrios Kyrtzidis | 87f3ff0 | 2008-04-12 00:47:19 +0000 | [diff] [blame] | 56 | PushOnScopeChains(SelTypedef, TUScope); |
Steve Naroff | 69d6375 | 2008-02-24 16:25:02 +0000 | [diff] [blame] | 57 | Context.setObjCSelType(SelTypedef); |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 27933c1 | 2008-06-21 22:44:51 +0000 | [diff] [blame] | 59 | // FIXME: Make sure these don't leak! |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 60 | RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class"); |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 61 | QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag)); |
| 62 | TypedefDecl *ClassTypedef = |
| 63 | TypedefDecl::Create(Context, CurContext, SourceLocation(), |
| 64 | &Context.Idents.get("Class"), ClassT, 0); |
| 65 | PushOnScopeChains(ClassTag, TUScope); |
| 66 | PushOnScopeChains(ClassTypedef, TUScope); |
| 67 | Context.setObjCClassType(ClassTypedef); |
| 68 | // Synthesize "@class Protocol; |
| 69 | ObjCInterfaceDecl *ProtocolDecl = |
Chris Lattner | b752f28 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 70 | ObjCInterfaceDecl::Create(Context, SourceLocation(), |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 71 | &Context.Idents.get("Protocol"), |
| 72 | SourceLocation(), true); |
| 73 | Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); |
| 74 | PushOnScopeChains(ProtocolDecl, TUScope); |
| 75 | |
| 76 | // Synthesize "typedef struct objc_object { Class isa; } *id;" |
Anders Carlsson | c303606 | 2008-08-23 22:20:38 +0000 | [diff] [blame] | 77 | RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object"); |
| 78 | |
Chris Lattner | 6ee1f9c | 2008-06-21 20:20:39 +0000 | [diff] [blame] | 79 | QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag)); |
| 80 | PushOnScopeChains(ObjectTag, TUScope); |
| 81 | TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext, |
| 82 | SourceLocation(), |
| 83 | &Context.Idents.get("id"), |
| 84 | ObjT, 0); |
| 85 | PushOnScopeChains(IdTypedef, TUScope); |
| 86 | Context.setObjCIdType(IdTypedef); |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 89 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer) |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 90 | : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0) { |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 91 | |
| 92 | // Get IdentifierInfo objects for known functions for which we |
| 93 | // do extra checking. |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 94 | IdentifierTable &IT = PP.getIdentifierTable(); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 96 | KnownFunctionIDs[id_printf] = &IT.get("printf"); |
| 97 | KnownFunctionIDs[id_fprintf] = &IT.get("fprintf"); |
| 98 | KnownFunctionIDs[id_sprintf] = &IT.get("sprintf"); |
| 99 | KnownFunctionIDs[id_snprintf] = &IT.get("snprintf"); |
| 100 | KnownFunctionIDs[id_asprintf] = &IT.get("asprintf"); |
Ted Kremenek | 7ff22b2 | 2008-06-16 18:00:42 +0000 | [diff] [blame] | 101 | KnownFunctionIDs[id_NSLog] = &IT.get("NSLog"); |
Chris Lattner | 2ae34ed | 2008-02-06 00:46:58 +0000 | [diff] [blame] | 102 | KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf"); |
| 103 | KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf"); |
| 104 | KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf"); |
| 105 | KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf"); |
| 106 | KnownFunctionIDs[id_vprintf] = &IT.get("vprintf"); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 107 | |
Daniel Dunbar | 662e8b5 | 2008-08-14 22:04:54 +0000 | [diff] [blame] | 108 | SuperID = &IT.get("super"); |
| 109 | |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 110 | TUScope = 0; |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 111 | if (getLangOptions().CPlusPlus) |
| 112 | FieldCollector.reset(new CXXFieldCollector()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 115 | /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. |
| 116 | /// If there is already an implicit cast, merge into the existing one. |
| 117 | void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) { |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 118 | if (Context.getCanonicalType(Expr->getType()) == |
| 119 | Context.getCanonicalType(Type)) return; |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 120 | |
| 121 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) |
| 122 | ImpCast->setType(Type); |
| 123 | else |
| 124 | Expr = new ImplicitCastExpr(Type, Expr); |
| 125 | } |
| 126 | |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 127 | void Sema::DeleteExpr(ExprTy *E) { |
| 128 | delete static_cast<Expr*>(E); |
| 129 | } |
| 130 | void Sema::DeleteStmt(StmtTy *S) { |
| 131 | delete static_cast<Stmt*>(S); |
| 132 | } |
| 133 | |
Chris Lattner | 9299f3f | 2008-08-23 03:19:52 +0000 | [diff] [blame] | 134 | /// ActOnEndOfTranslationUnit - This is called at the very end of the |
| 135 | /// translation unit when EOF is reached and all but the top-level scope is |
| 136 | /// popped. |
| 137 | void Sema::ActOnEndOfTranslationUnit() { |
| 138 | |
| 139 | } |
| 140 | |
| 141 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 142 | //===----------------------------------------------------------------------===// |
| 143 | // Helper functions. |
| 144 | //===----------------------------------------------------------------------===// |
| 145 | |
| 146 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 147 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID); |
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, const std::string &Msg) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 152 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 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 &Msg1, |
| 157 | const std::string &Msg2) { |
| 158 | std::string MsgArr[] = { Msg1, Msg2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 159 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 160 | return true; |
| 161 | } |
| 162 | |
Argyrios Kyrtzidis | a88b509 | 2008-08-24 13:14:02 +0000 | [diff] [blame^] | 163 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const SourceRange& Range) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 164 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 165 | return true; |
| 166 | } |
| 167 | |
| 168 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
Argyrios Kyrtzidis | a88b509 | 2008-08-24 13:14:02 +0000 | [diff] [blame^] | 169 | const SourceRange& Range) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 170 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 171 | return true; |
| 172 | } |
| 173 | |
| 174 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
Argyrios Kyrtzidis | a88b509 | 2008-08-24 13:14:02 +0000 | [diff] [blame^] | 175 | const std::string &Msg2, const SourceRange& Range) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 176 | std::string MsgArr[] = { Msg1, Msg2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 177 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 178 | return true; |
| 179 | } |
| 180 | |
Chris Lattner | 4667ac3 | 2008-01-03 23:38:43 +0000 | [diff] [blame] | 181 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 182 | const std::string &Msg2, const std::string &Msg3, |
Argyrios Kyrtzidis | a88b509 | 2008-08-24 13:14:02 +0000 | [diff] [blame^] | 183 | const SourceRange& R1) { |
Chris Lattner | 4667ac3 | 2008-01-03 23:38:43 +0000 | [diff] [blame] | 184 | std::string MsgArr[] = { Msg1, Msg2, Msg3 }; |
| 185 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1); |
| 186 | return true; |
| 187 | } |
| 188 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 189 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, |
Argyrios Kyrtzidis | a88b509 | 2008-08-24 13:14:02 +0000 | [diff] [blame^] | 190 | const SourceRange& R1, const SourceRange& R2) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 191 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 192 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 193 | return true; |
| 194 | } |
| 195 | |
| 196 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
Argyrios Kyrtzidis | a88b509 | 2008-08-24 13:14:02 +0000 | [diff] [blame^] | 197 | const SourceRange& R1, const SourceRange& R2) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 198 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 199 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 200 | return true; |
| 201 | } |
| 202 | |
| 203 | bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1, |
Argyrios Kyrtzidis | a88b509 | 2008-08-24 13:14:02 +0000 | [diff] [blame^] | 204 | const std::string &Msg2, const SourceRange& R1, |
| 205 | const SourceRange& R2) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 206 | std::string MsgArr[] = { Msg1, Msg2 }; |
| 207 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 208 | PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 209 | return true; |
| 210 | } |
| 211 | |
| 212 | const LangOptions &Sema::getLangOptions() const { |
| 213 | return PP.getLangOptions(); |
| 214 | } |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 215 | |
| 216 | ObjCMethodDecl *Sema::getCurMethodDecl() { |
| 217 | return dyn_cast<ObjCMethodDecl>(CurContext); |
| 218 | } |