blob: dc68eb03d85586b5ee98ce09200e5f5f0f60c35b [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- 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 Lattner2e64c072007-08-10 20:18:51 +000019
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
Steve Naroff9637a9b2007-10-09 22:01:59 +000022void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
23 TUScope = S;
24}
25
Steve Naroff7aeeda02007-10-10 23:24:43 +000026/// 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.
32QualType Sema::GetObjcIdType(SourceLocation Loc) {
Steve Naroffee1de132007-10-10 21:53:07 +000033 assert(TUScope && "GetObjcIdType(): Top-level scope is null");
Steve Naroff9d12c902007-10-15 14:41:52 +000034 if (Context.getObjcIdType().isNull()) {
Steve Naroffee1de132007-10-10 21:53:07 +000035 IdentifierInfo *IdIdent = &Context.Idents.get("id");
36 ScopedDecl *IdDecl = LookupScopedDecl(IdIdent, Decl::IDNS_Ordinary,
37 SourceLocation(), TUScope);
Steve Naroff9d12c902007-10-15 14:41:52 +000038 TypedefDecl *ObjcIdTypedef = dyn_cast_or_null<TypedefDecl>(IdDecl);
Steve Naroff7aeeda02007-10-10 23:24:43 +000039 if (!ObjcIdTypedef) {
40 Diag(Loc, diag::err_missing_id_definition);
41 return QualType();
42 }
Steve Naroff9d12c902007-10-15 14:41:52 +000043 Context.setObjcIdType(ObjcIdTypedef);
Steve Naroffee1de132007-10-10 21:53:07 +000044 }
Steve Naroff9d12c902007-10-15 14:41:52 +000045 return Context.getObjcIdType();
Steve Naroffee1de132007-10-10 21:53:07 +000046}
47
Fariborz Jahanianf807c202007-10-16 20:40:23 +000048/// GetObjcSelType - See comments for Sema::GetObjcIdType above; replace "id"
49/// with "SEL".
50QualType Sema::GetObjcSelType(SourceLocation Loc) {
51 assert(TUScope && "GetObjcSelType(): Top-level scope is null");
52 if (Context.getObjcSelType().isNull()) {
53 IdentifierInfo *SelIdent = &Context.Idents.get("SEL");
54 ScopedDecl *SelDecl = LookupScopedDecl(SelIdent, Decl::IDNS_Ordinary,
55 SourceLocation(), TUScope);
56 TypedefDecl *ObjcSelTypedef = dyn_cast_or_null<TypedefDecl>(SelDecl);
57 if (!ObjcSelTypedef) {
58 Diag(Loc, diag::err_missing_sel_definition);
59 return QualType();
60 }
61 Context.setObjcSelType(ObjcSelTypedef);
62 }
63 return Context.getObjcSelType();
64}
Steve Naroffee1de132007-10-10 21:53:07 +000065
Chris Lattner4b009652007-07-25 00:24:17 +000066Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup)
67 : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) {
Chris Lattner2e64c072007-08-10 20:18:51 +000068
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 Lattner2e64c072007-08-10 20:18:51 +000077 KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf");
Ted Kremenek2d7e9532007-08-10 21:13:51 +000078 KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf");
Chris Lattner2e64c072007-08-10 20:18:51 +000079 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 Naroffee1de132007-10-10 21:53:07 +000083
84 TUScope = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000085}
86
Chris Lattnerb26b7ad2007-08-31 04:53:24 +000087void Sema::DeleteExpr(ExprTy *E) {
88 delete static_cast<Expr*>(E);
89}
90void Sema::DeleteStmt(StmtTy *S) {
91 delete static_cast<Stmt*>(S);
92}
93
Chris Lattner4b009652007-07-25 00:24:17 +000094//===----------------------------------------------------------------------===//
95// Helper functions.
96//===----------------------------------------------------------------------===//
97
98bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
99 PP.getDiagnostics().Report(Loc, DiagID);
100 return true;
101}
102
103bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
104 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1);
105 return true;
106}
107
108bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
109 const std::string &Msg2) {
110 std::string MsgArr[] = { Msg1, Msg2 };
111 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2);
112 return true;
113}
114
115bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
116 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, &Range, 1);
117 return true;
118}
119
120bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
121 SourceRange Range) {
122 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, &Range, 1);
123 return true;
124}
125
126bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
127 const std::string &Msg2, SourceRange Range) {
128 std::string MsgArr[] = { Msg1, Msg2 };
129 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2, &Range, 1);
130 return true;
131}
132
133bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
134 SourceRange R1, SourceRange R2) {
135 SourceRange RangeArr[] = { R1, R2 };
136 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, RangeArr, 2);
137 return true;
138}
139
140bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
141 SourceRange R1, SourceRange R2) {
142 SourceRange RangeArr[] = { R1, R2 };
143 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, RangeArr, 2);
144 return true;
145}
146
147bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
148 const std::string &Msg2, SourceRange R1, SourceRange R2) {
149 std::string MsgArr[] = { Msg1, Msg2 };
150 SourceRange RangeArr[] = { R1, R2 };
151 PP.getDiagnostics().Report(Range, DiagID, MsgArr, 2, RangeArr, 2);
152 return true;
153}
154
155const LangOptions &Sema::getLangOptions() const {
156 return PP.getLangOptions();
157}