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