Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 19 | #include "clang/Parse/Scope.h" |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 20 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 23 | bool Sema::isBuiltinObjCType(TypedefDecl *TD) { |
Steve Naroff | ae84af8 | 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 | b4452ed | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 26 | strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0; |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 29 | bool Sema::isObjCObjectPointerType(QualType type) const { |
| 30 | if (!type->isPointerType() && !type->isObjCQualifiedIdType()) |
Fariborz Jahanian | fe0982d | 2008-01-04 00:27:46 +0000 | [diff] [blame] | 31 | return false; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 32 | if (type == Context.getObjCIdType() || type == Context.getObjCClassType() || |
| 33 | type->isObjCQualifiedIdType()) |
Fariborz Jahanian | fe0982d | 2008-01-04 00:27:46 +0000 | [diff] [blame] | 34 | return true; |
| 35 | |
Fariborz Jahanian | c761261 | 2008-01-07 18:14:04 +0000 | [diff] [blame] | 36 | if (type->isPointerType()) { |
Fariborz Jahanian | fe0982d | 2008-01-04 00:27:46 +0000 | [diff] [blame] | 37 | PointerType *pointerType = static_cast<PointerType*>(type.getTypePtr()); |
| 38 | type = pointerType->getPointeeType(); |
| 39 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 40 | return (type->isObjCInterfaceType() || type->isObjCQualifiedIdType()); |
Fariborz Jahanian | fe0982d | 2008-01-04 00:27:46 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Steve Naroff | 9637a9b | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 43 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 44 | TUScope = S; |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 45 | if (PP.getLangOptions().ObjC1) { |
| 46 | TypedefType *t; |
Chris Lattner | 77470dc | 2007-10-30 20:57:56 +0000 | [diff] [blame] | 47 | |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 48 | // Add the built-in ObjC types. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 49 | t = dyn_cast<TypedefType>(Context.getObjCIdType().getTypePtr()); |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 50 | t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl()); |
| 51 | TUScope->AddDecl(t->getDecl()); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 52 | t = dyn_cast<TypedefType>(Context.getObjCClassType().getTypePtr()); |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 53 | t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl()); |
| 54 | TUScope->AddDecl(t->getDecl()); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 55 | ObjCInterfaceType *it = dyn_cast<ObjCInterfaceType>(Context.getObjCProtoType()); |
| 56 | ObjCInterfaceDecl *IDecl = it->getDecl(); |
Fariborz Jahanian | b4452ed | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 57 | IDecl->getIdentifier()->setFETokenInfo(IDecl); |
| 58 | TUScope->AddDecl(IDecl); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 59 | t = dyn_cast<TypedefType>(Context.getObjCSelType().getTypePtr()); |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 60 | t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl()); |
| 61 | TUScope->AddDecl(t->getDecl()); |
Steve Naroff | ee1de13 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 62 | } |
Steve Naroff | ee1de13 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Steve Naroff | ca44ffd | 2007-11-29 23:05:20 +0000 | [diff] [blame] | 65 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt) |
Anders Carlsson | c922526 | 2007-11-30 20:01:38 +0000 | [diff] [blame] | 66 | : PP(pp), Context(ctxt), CurFunctionDecl(0), CurMethodDecl(0) { |
Chris Lattner | 2e64c07 | 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 | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 76 | KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf"); |
Ted Kremenek | 2d7e953 | 2007-08-10 21:13:51 +0000 | [diff] [blame] | 77 | KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf"); |
Chris Lattner | 2e64c07 | 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 | ae84af8 | 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); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 91 | Context.setObjCClassType(ClassTypedef); |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 92 | |
Fariborz Jahanian | b4452ed | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 93 | // Synthesize "@class Protocol; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 94 | ObjCInterfaceDecl *ProtocolDecl = new ObjCInterfaceDecl(SourceLocation(), 0, |
Fariborz Jahanian | b4452ed | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 95 | &Context.Idents.get("Protocol"), true); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 96 | Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl)); |
Fariborz Jahanian | b4452ed | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 97 | |
Steve Naroff | ae84af8 | 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, |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 102 | Context.getObjCClassType()); |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 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); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 108 | Context.setObjCIdType(IdTypedef); |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 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); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 117 | Context.setObjCSelType(SelTypedef); |
Fariborz Jahanian | b4452ed | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 118 | |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 119 | } |
Steve Naroff | ee1de13 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 120 | TUScope = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame^] | 123 | /// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast. |
| 124 | /// If there is already an implicit cast, merge into the existing one. |
| 125 | void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) { |
| 126 | if (Expr->getType() == Type) return; |
| 127 | |
| 128 | if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) |
| 129 | ImpCast->setType(Type); |
| 130 | else |
| 131 | Expr = new ImplicitCastExpr(Type, Expr); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | |
Chris Lattner | b26b7ad | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 136 | void Sema::DeleteExpr(ExprTy *E) { |
| 137 | delete static_cast<Expr*>(E); |
| 138 | } |
| 139 | void Sema::DeleteStmt(StmtTy *S) { |
| 140 | delete static_cast<Stmt*>(S); |
| 141 | } |
| 142 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 143 | //===----------------------------------------------------------------------===// |
| 144 | // Helper functions. |
| 145 | //===----------------------------------------------------------------------===// |
| 146 | |
| 147 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID) { |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 148 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 149 | return true; |
| 150 | } |
| 151 | |
| 152 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) { |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 153 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 154 | return true; |
| 155 | } |
| 156 | |
| 157 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 158 | const std::string &Msg2) { |
| 159 | std::string MsgArr[] = { Msg1, Msg2 }; |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 160 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | |
| 164 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) { |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 165 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 166 | return true; |
| 167 | } |
| 168 | |
| 169 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 170 | SourceRange Range) { |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 171 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 172 | return true; |
| 173 | } |
| 174 | |
| 175 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 176 | const std::string &Msg2, SourceRange Range) { |
| 177 | std::string MsgArr[] = { Msg1, Msg2 }; |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 178 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 179 | return true; |
| 180 | } |
| 181 | |
Chris Lattner | 01e854d | 2008-01-03 23:38:43 +0000 | [diff] [blame] | 182 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 183 | const std::string &Msg2, const std::string &Msg3, |
| 184 | SourceRange R1) { |
| 185 | std::string MsgArr[] = { Msg1, Msg2, Msg3 }; |
| 186 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1); |
| 187 | return true; |
| 188 | } |
| 189 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 190 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, |
| 191 | SourceRange R1, SourceRange R2) { |
| 192 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 193 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 194 | return true; |
| 195 | } |
| 196 | |
| 197 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 198 | SourceRange R1, SourceRange R2) { |
| 199 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 200 | PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 201 | return true; |
| 202 | } |
| 203 | |
| 204 | bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1, |
| 205 | const std::string &Msg2, SourceRange R1, SourceRange R2) { |
| 206 | std::string MsgArr[] = { Msg1, Msg2 }; |
| 207 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 208 | PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 209 | return true; |
| 210 | } |
| 211 | |
| 212 | const LangOptions &Sema::getLangOptions() const { |
| 213 | return PP.getLangOptions(); |
| 214 | } |