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