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" |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Steve Naroff | 9637a9b | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 22 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 23 | TUScope = S; |
| 24 | } |
| 25 | |
Steve Naroff | 7aeeda0 | 2007-10-10 23:24:43 +0000 | [diff] [blame] | 26 | /// GetObjcIdType - The following method assumes that "id" is imported |
| 27 | /// via <objc/objc.h>. This is the way GCC worked for almost 20 years. |
| 28 | /// In GCC 4.0, "id" is now a built-in type. Unfortunately, typedefs *cannot* be |
| 29 | /// redefined (even if they are identical). To allow a built-in types to coexist |
| 30 | /// with <objc/objc.h>, GCC has a special hack on decls (DECL_IN_SYSTEM_HEADER). |
| 31 | /// For now, we will *not* install id as a built-in. FIXME: reconsider this. |
| 32 | QualType Sema::GetObjcIdType(SourceLocation Loc) { |
Steve Naroff | ee1de13 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 33 | assert(TUScope && "GetObjcIdType(): Top-level scope is null"); |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame^] | 34 | if (Context.getObjcIdType().isNull()) { |
Steve Naroff | ee1de13 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 35 | IdentifierInfo *IdIdent = &Context.Idents.get("id"); |
| 36 | ScopedDecl *IdDecl = LookupScopedDecl(IdIdent, Decl::IDNS_Ordinary, |
| 37 | SourceLocation(), TUScope); |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame^] | 38 | TypedefDecl *ObjcIdTypedef = dyn_cast_or_null<TypedefDecl>(IdDecl); |
Steve Naroff | 7aeeda0 | 2007-10-10 23:24:43 +0000 | [diff] [blame] | 39 | if (!ObjcIdTypedef) { |
| 40 | Diag(Loc, diag::err_missing_id_definition); |
| 41 | return QualType(); |
| 42 | } |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame^] | 43 | Context.setObjcIdType(ObjcIdTypedef); |
Steve Naroff | ee1de13 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 44 | } |
Steve Naroff | 9d12c90 | 2007-10-15 14:41:52 +0000 | [diff] [blame^] | 45 | return Context.getObjcIdType(); |
Steve Naroff | ee1de13 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 49 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup) |
| 50 | : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) { |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 51 | |
| 52 | // Get IdentifierInfo objects for known functions for which we |
| 53 | // do extra checking. |
| 54 | IdentifierTable& IT = PP.getIdentifierTable(); |
| 55 | |
| 56 | KnownFunctionIDs[ id_printf ] = &IT.get("printf"); |
| 57 | KnownFunctionIDs[ id_fprintf ] = &IT.get("fprintf"); |
| 58 | KnownFunctionIDs[ id_sprintf ] = &IT.get("sprintf"); |
| 59 | KnownFunctionIDs[ id_snprintf ] = &IT.get("snprintf"); |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 60 | KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf"); |
Ted Kremenek | 2d7e953 | 2007-08-10 21:13:51 +0000 | [diff] [blame] | 61 | KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf"); |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 62 | KnownFunctionIDs[ id_vasprintf ] = &IT.get("vasprintf"); |
| 63 | KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf"); |
| 64 | KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf"); |
| 65 | KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf"); |
Steve Naroff | ee1de13 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 66 | |
| 67 | TUScope = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Chris Lattner | b26b7ad | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 70 | void Sema::DeleteExpr(ExprTy *E) { |
| 71 | delete static_cast<Expr*>(E); |
| 72 | } |
| 73 | void Sema::DeleteStmt(StmtTy *S) { |
| 74 | delete static_cast<Stmt*>(S); |
| 75 | } |
| 76 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 77 | //===----------------------------------------------------------------------===// |
| 78 | // Helper functions. |
| 79 | //===----------------------------------------------------------------------===// |
| 80 | |
| 81 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID) { |
| 82 | PP.getDiagnostics().Report(Loc, DiagID); |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) { |
| 87 | PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1); |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 92 | const std::string &Msg2) { |
| 93 | std::string MsgArr[] = { Msg1, Msg2 }; |
| 94 | PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2); |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) { |
| 99 | PP.getDiagnostics().Report(Loc, DiagID, 0, 0, &Range, 1); |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 104 | SourceRange Range) { |
| 105 | PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, &Range, 1); |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 110 | const std::string &Msg2, SourceRange Range) { |
| 111 | std::string MsgArr[] = { Msg1, Msg2 }; |
| 112 | PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2, &Range, 1); |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, |
| 117 | SourceRange R1, SourceRange R2) { |
| 118 | SourceRange RangeArr[] = { R1, R2 }; |
| 119 | PP.getDiagnostics().Report(Loc, DiagID, 0, 0, RangeArr, 2); |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 124 | SourceRange R1, SourceRange R2) { |
| 125 | SourceRange RangeArr[] = { R1, R2 }; |
| 126 | PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, RangeArr, 2); |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1, |
| 131 | const std::string &Msg2, SourceRange R1, SourceRange R2) { |
| 132 | std::string MsgArr[] = { Msg1, Msg2 }; |
| 133 | SourceRange RangeArr[] = { R1, R2 }; |
| 134 | PP.getDiagnostics().Report(Range, DiagID, MsgArr, 2, RangeArr, 2); |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | const LangOptions &Sema::getLangOptions() const { |
| 139 | return PP.getLangOptions(); |
| 140 | } |