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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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 | |
Steve Naroff | ae84af8 | 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 | 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 | |
Steve Naroff | 9637a9b | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 29 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 30 | TUScope = S; |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 31 | if (PP.getLangOptions().ObjC1) { |
| 32 | TypedefType *t; |
Chris Lattner | 77470dc | 2007-10-30 20:57:56 +0000 | [diff] [blame] | 33 | |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 34 | // Add the built-in ObjC types. |
| 35 | t = dyn_cast<TypedefType>(Context.getObjcIdType().getTypePtr()); |
| 36 | t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl()); |
| 37 | TUScope->AddDecl(t->getDecl()); |
| 38 | t = dyn_cast<TypedefType>(Context.getObjcClassType().getTypePtr()); |
| 39 | t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl()); |
| 40 | TUScope->AddDecl(t->getDecl()); |
Fariborz Jahanian | b4452ed | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 41 | ObjcInterfaceType *it = dyn_cast<ObjcInterfaceType>(Context.getObjcProtoType()); |
| 42 | ObjcInterfaceDecl *IDecl = it->getDecl(); |
| 43 | IDecl->getIdentifier()->setFETokenInfo(IDecl); |
| 44 | TUScope->AddDecl(IDecl); |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 45 | t = dyn_cast<TypedefType>(Context.getObjcSelType().getTypePtr()); |
| 46 | t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl()); |
| 47 | TUScope->AddDecl(t->getDecl()); |
Steve Naroff | ee1de13 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 48 | } |
Steve Naroff | ee1de13 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Steve Naroff | ca44ffd | 2007-11-29 23:05:20 +0000 | [diff] [blame] | 51 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt) |
Anders Carlsson | c922526 | 2007-11-30 20:01:38 +0000 | [diff] [blame] | 52 | : PP(pp), Context(ctxt), CurFunctionDecl(0), CurMethodDecl(0) { |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 53 | |
| 54 | // Get IdentifierInfo objects for known functions for which we |
| 55 | // do extra checking. |
| 56 | IdentifierTable& IT = PP.getIdentifierTable(); |
| 57 | |
| 58 | KnownFunctionIDs[ id_printf ] = &IT.get("printf"); |
| 59 | KnownFunctionIDs[ id_fprintf ] = &IT.get("fprintf"); |
| 60 | KnownFunctionIDs[ id_sprintf ] = &IT.get("sprintf"); |
| 61 | KnownFunctionIDs[ id_snprintf ] = &IT.get("snprintf"); |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 62 | KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf"); |
Ted Kremenek | 2d7e953 | 2007-08-10 21:13:51 +0000 | [diff] [blame] | 63 | KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf"); |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 64 | KnownFunctionIDs[ id_vasprintf ] = &IT.get("vasprintf"); |
| 65 | KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf"); |
| 66 | KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf"); |
| 67 | KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf"); |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 68 | |
| 69 | if (PP.getLangOptions().ObjC1) { |
| 70 | // Synthesize "typedef struct objc_class *Class;" |
| 71 | RecordDecl *ClassTag = new RecordDecl(Decl::Struct, SourceLocation(), |
| 72 | &IT.get("objc_class"), 0); |
| 73 | QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag)); |
| 74 | TypedefDecl *ClassTypedef = new TypedefDecl(SourceLocation(), |
| 75 | &Context.Idents.get("Class"), |
| 76 | ClassT, 0); |
| 77 | Context.setObjcClassType(ClassTypedef); |
| 78 | |
Fariborz Jahanian | b4452ed | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 79 | // Synthesize "@class Protocol; |
| 80 | ObjcInterfaceDecl *ProtocolDecl = new ObjcInterfaceDecl(SourceLocation(), 0, |
| 81 | &Context.Idents.get("Protocol"), true); |
| 82 | Context.setObjcProtoType(Context.getObjcInterfaceType(ProtocolDecl)); |
| 83 | |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 84 | // Synthesize "typedef struct objc_object { Class isa; } *id;" |
| 85 | RecordDecl *ObjectTag = new RecordDecl(Decl::Struct, SourceLocation(), |
| 86 | &IT.get("objc_object"), 0); |
| 87 | FieldDecl *IsaDecl = new FieldDecl(SourceLocation(), 0, |
| 88 | Context.getObjcClassType()); |
| 89 | ObjectTag->defineBody(&IsaDecl, 1); |
| 90 | QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag)); |
| 91 | TypedefDecl *IdTypedef = new TypedefDecl(SourceLocation(), |
| 92 | &Context.Idents.get("id"), |
| 93 | ObjT, 0); |
| 94 | Context.setObjcIdType(IdTypedef); |
| 95 | |
| 96 | // Synthesize "typedef struct objc_selector *SEL;" |
| 97 | RecordDecl *SelTag = new RecordDecl(Decl::Struct, SourceLocation(), |
| 98 | &IT.get("objc_selector"), 0); |
| 99 | QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); |
| 100 | TypedefDecl *SelTypedef = new TypedefDecl(SourceLocation(), |
| 101 | &Context.Idents.get("SEL"), |
| 102 | SelT, 0); |
| 103 | Context.setObjcSelType(SelTypedef); |
Fariborz Jahanian | b4452ed | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 104 | |
Steve Naroff | ae84af8 | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 105 | } |
Steve Naroff | ee1de13 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 106 | TUScope = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Chris Lattner | b26b7ad | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 109 | void Sema::DeleteExpr(ExprTy *E) { |
| 110 | delete static_cast<Expr*>(E); |
| 111 | } |
| 112 | void Sema::DeleteStmt(StmtTy *S) { |
| 113 | delete static_cast<Stmt*>(S); |
| 114 | } |
| 115 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 116 | //===----------------------------------------------------------------------===// |
| 117 | // Helper functions. |
| 118 | //===----------------------------------------------------------------------===// |
| 119 | |
| 120 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID) { |
Ted Kremenek | b3ee193 | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 121 | PP.getDiagnostics().Report(Loc, DiagID, PP.getSourceManager()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 122 | return true; |
| 123 | } |
| 124 | |
| 125 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) { |
Ted Kremenek | b3ee193 | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 126 | PP.getDiagnostics().Report(Loc, DiagID, PP.getSourceManager(), &Msg, 1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | |
| 130 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 131 | const std::string &Msg2) { |
| 132 | std::string MsgArr[] = { Msg1, Msg2 }; |
Ted Kremenek | b3ee193 | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 133 | PP.getDiagnostics().Report(Loc, DiagID, PP.getSourceManager(), MsgArr, 2); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 134 | return true; |
| 135 | } |
| 136 | |
| 137 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) { |
Ted Kremenek | b3ee193 | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 138 | PP.getDiagnostics().Report(Loc, DiagID, PP.getSourceManager(), 0,0, &Range,1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 139 | return true; |
| 140 | } |
| 141 | |
| 142 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 143 | SourceRange Range) { |
Ted Kremenek | b3ee193 | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 144 | PP.getDiagnostics().Report(Loc,DiagID,PP.getSourceManager(),&Msg,1,&Range,1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 145 | return true; |
| 146 | } |
| 147 | |
| 148 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 149 | const std::string &Msg2, SourceRange Range) { |
| 150 | std::string MsgArr[] = { Msg1, Msg2 }; |
Ted Kremenek | b3ee193 | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 151 | PP.getDiagnostics().Report(Loc,DiagID,PP.getSourceManager(), |
| 152 | MsgArr,2,&Range,1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 153 | return true; |
| 154 | } |
| 155 | |
| 156 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, |
| 157 | SourceRange R1, SourceRange R2) { |
| 158 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | b3ee193 | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 159 | PP.getDiagnostics().Report(Loc, DiagID, PP.getSourceManager(), |
| 160 | 0, 0, RangeArr, 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, const std::string &Msg, |
| 165 | SourceRange R1, SourceRange R2) { |
| 166 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | b3ee193 | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 167 | PP.getDiagnostics().Report(Loc, DiagID, PP.getSourceManager(), &Msg, |
| 168 | 1, RangeArr, 2); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 169 | return true; |
| 170 | } |
| 171 | |
| 172 | bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1, |
| 173 | const std::string &Msg2, SourceRange R1, SourceRange R2) { |
| 174 | std::string MsgArr[] = { Msg1, Msg2 }; |
| 175 | SourceRange RangeArr[] = { R1, R2 }; |
Ted Kremenek | b3ee193 | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 176 | PP.getDiagnostics().Report(Range, DiagID, PP.getSourceManager(), MsgArr, 2, |
| 177 | RangeArr, 2); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 178 | return true; |
| 179 | } |
| 180 | |
| 181 | const LangOptions &Sema::getLangOptions() const { |
| 182 | return PP.getLangOptions(); |
| 183 | } |