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 | // |
| 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 | 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 | |
Steve Naroff | 8ee529b | 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 || |
| 26 | strcmp(typeName, "SEL") == 0; |
| 27 | } |
| 28 | |
Steve Naroff | b216c88 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 29 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 30 | TUScope = S; |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 31 | if (PP.getLangOptions().ObjC1) { |
| 32 | TypedefType *t; |
Chris Lattner | fb1051b | 2007-10-30 20:57:56 +0000 | [diff] [blame] | 33 | |
Steve Naroff | 8ee529b | 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()); |
| 41 | t = dyn_cast<TypedefType>(Context.getObjcSelType().getTypePtr()); |
| 42 | t->getDecl()->getIdentifier()->setFETokenInfo(t->getDecl()); |
| 43 | TUScope->AddDecl(t->getDecl()); |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 44 | } |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 47 | /// FIXME: remove this. |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 48 | /// GetObjcProtoType - See comments for Sema::GetObjcIdType above; replace "id" |
| 49 | /// with "Protocol". |
| 50 | QualType Sema::GetObjcProtoType(SourceLocation Loc) { |
| 51 | assert(TUScope && "GetObjcProtoType(): Top-level scope is null"); |
| 52 | if (Context.getObjcProtoType().isNull()) { |
| 53 | IdentifierInfo *ProtoIdent = &Context.Idents.get("Protocol"); |
| 54 | ScopedDecl *ProtoDecl = LookupScopedDecl(ProtoIdent, Decl::IDNS_Ordinary, |
| 55 | SourceLocation(), TUScope); |
| 56 | TypedefDecl *ObjcProtoTypedef = dyn_cast_or_null<TypedefDecl>(ProtoDecl); |
| 57 | if (!ObjcProtoTypedef) { |
| 58 | Diag(Loc, diag::err_missing_proto_definition); |
| 59 | return QualType(); |
| 60 | } |
| 61 | Context.setObjcProtoType(ObjcProtoTypedef); |
| 62 | } |
| 63 | return Context.getObjcProtoType(); |
| 64 | } |
| 65 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup) |
| 67 | : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) { |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 68 | |
| 69 | // Get IdentifierInfo objects for known functions for which we |
| 70 | // do extra checking. |
| 71 | IdentifierTable& IT = PP.getIdentifierTable(); |
| 72 | |
| 73 | KnownFunctionIDs[ id_printf ] = &IT.get("printf"); |
| 74 | KnownFunctionIDs[ id_fprintf ] = &IT.get("fprintf"); |
| 75 | KnownFunctionIDs[ id_sprintf ] = &IT.get("sprintf"); |
| 76 | KnownFunctionIDs[ id_snprintf ] = &IT.get("snprintf"); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 77 | KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf"); |
Ted Kremenek | e0eb80a | 2007-08-10 21:13:51 +0000 | [diff] [blame] | 78 | KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf"); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 79 | KnownFunctionIDs[ id_vasprintf ] = &IT.get("vasprintf"); |
| 80 | KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf"); |
| 81 | KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf"); |
| 82 | KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf"); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 83 | |
| 84 | if (PP.getLangOptions().ObjC1) { |
| 85 | // Synthesize "typedef struct objc_class *Class;" |
| 86 | RecordDecl *ClassTag = new RecordDecl(Decl::Struct, SourceLocation(), |
| 87 | &IT.get("objc_class"), 0); |
| 88 | QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag)); |
| 89 | TypedefDecl *ClassTypedef = new TypedefDecl(SourceLocation(), |
| 90 | &Context.Idents.get("Class"), |
| 91 | ClassT, 0); |
| 92 | Context.setObjcClassType(ClassTypedef); |
| 93 | |
| 94 | // Synthesize "typedef struct objc_object { Class isa; } *id;" |
| 95 | RecordDecl *ObjectTag = new RecordDecl(Decl::Struct, SourceLocation(), |
| 96 | &IT.get("objc_object"), 0); |
| 97 | FieldDecl *IsaDecl = new FieldDecl(SourceLocation(), 0, |
| 98 | Context.getObjcClassType()); |
| 99 | ObjectTag->defineBody(&IsaDecl, 1); |
| 100 | QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag)); |
| 101 | TypedefDecl *IdTypedef = new TypedefDecl(SourceLocation(), |
| 102 | &Context.Idents.get("id"), |
| 103 | ObjT, 0); |
| 104 | Context.setObjcIdType(IdTypedef); |
| 105 | |
| 106 | // Synthesize "typedef struct objc_selector *SEL;" |
| 107 | RecordDecl *SelTag = new RecordDecl(Decl::Struct, SourceLocation(), |
| 108 | &IT.get("objc_selector"), 0); |
| 109 | QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag)); |
| 110 | TypedefDecl *SelTypedef = new TypedefDecl(SourceLocation(), |
| 111 | &Context.Idents.get("SEL"), |
| 112 | SelT, 0); |
| 113 | Context.setObjcSelType(SelTypedef); |
| 114 | } |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 115 | TUScope = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 118 | void Sema::DeleteExpr(ExprTy *E) { |
| 119 | delete static_cast<Expr*>(E); |
| 120 | } |
| 121 | void Sema::DeleteStmt(StmtTy *S) { |
| 122 | delete static_cast<Stmt*>(S); |
| 123 | } |
| 124 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 125 | //===----------------------------------------------------------------------===// |
| 126 | // Helper functions. |
| 127 | //===----------------------------------------------------------------------===// |
| 128 | |
| 129 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID) { |
| 130 | PP.getDiagnostics().Report(Loc, DiagID); |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) { |
| 135 | PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1); |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 140 | const std::string &Msg2) { |
| 141 | std::string MsgArr[] = { Msg1, Msg2 }; |
| 142 | PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2); |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) { |
| 147 | PP.getDiagnostics().Report(Loc, DiagID, 0, 0, &Range, 1); |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 152 | SourceRange Range) { |
| 153 | PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, &Range, 1); |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 158 | const std::string &Msg2, SourceRange Range) { |
| 159 | std::string MsgArr[] = { Msg1, Msg2 }; |
| 160 | PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2, &Range, 1); |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, |
| 165 | SourceRange R1, SourceRange R2) { |
| 166 | SourceRange RangeArr[] = { R1, R2 }; |
| 167 | PP.getDiagnostics().Report(Loc, DiagID, 0, 0, RangeArr, 2); |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 172 | SourceRange R1, SourceRange R2) { |
| 173 | SourceRange RangeArr[] = { R1, R2 }; |
| 174 | PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, RangeArr, 2); |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1, |
| 179 | const std::string &Msg2, SourceRange R1, SourceRange R2) { |
| 180 | std::string MsgArr[] = { Msg1, Msg2 }; |
| 181 | SourceRange RangeArr[] = { R1, R2 }; |
| 182 | PP.getDiagnostics().Report(Range, DiagID, MsgArr, 2, RangeArr, 2); |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | const LangOptions &Sema::getLangOptions() const { |
| 187 | return PP.getLangOptions(); |
| 188 | } |