blob: 330d9dce741ac2f4dc6226f4c41d668106e275bd [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
48
Chris Lattner4b009652007-07-25 00:24:17 +000049Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup)
50 : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) {
Chris Lattner2e64c072007-08-10 20:18:51 +000051
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 Lattner2e64c072007-08-10 20:18:51 +000060 KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf");
Ted Kremenek2d7e9532007-08-10 21:13:51 +000061 KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf");
Chris Lattner2e64c072007-08-10 20:18:51 +000062 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 Naroffee1de132007-10-10 21:53:07 +000066
67 TUScope = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000068}
69
Chris Lattnerb26b7ad2007-08-31 04:53:24 +000070void Sema::DeleteExpr(ExprTy *E) {
71 delete static_cast<Expr*>(E);
72}
73void Sema::DeleteStmt(StmtTy *S) {
74 delete static_cast<Stmt*>(S);
75}
76
Chris Lattner4b009652007-07-25 00:24:17 +000077//===----------------------------------------------------------------------===//
78// Helper functions.
79//===----------------------------------------------------------------------===//
80
81bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
82 PP.getDiagnostics().Report(Loc, DiagID);
83 return true;
84}
85
86bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
87 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1);
88 return true;
89}
90
91bool 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
98bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
99 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, &Range, 1);
100 return true;
101}
102
103bool 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
109bool 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
116bool 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
123bool 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
130bool 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
138const LangOptions &Sema::getLangOptions() const {
139 return PP.getLangOptions();
140}