blob: 30ada28d8bcd15a01da6f4673203c1084f2a8fc4 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000017#include "clang/AST/DeclObjC.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/Lex/Preprocessor.h"
20#include "clang/Basic/Diagnostic.h"
Chris Lattner59907c42007-08-10 20:18:51 +000021
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Ted Kremeneka526c5c2008-01-07 19:49:32 +000024bool Sema::isBuiltinObjCType(TypedefDecl *TD) {
Steve Naroff8ee529b2007-10-31 18:42:27 +000025 const char *typeName = TD->getIdentifier()->getName();
26 return strcmp(typeName, "id") == 0 || strcmp(typeName, "Class") == 0 ||
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +000027 strcmp(typeName, "SEL") == 0 || strcmp(typeName, "Protocol") == 0;
Steve Naroff8ee529b2007-10-31 18:42:27 +000028}
29
Anders Carlssonc3036062008-08-23 22:20:38 +000030static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name)
31{
32 if (C.getLangOptions().CPlusPlus)
33 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
34 C.getTranslationUnitDecl(),
35 SourceLocation(), &C.Idents.get(Name), 0);
36 else
37 return RecordDecl::Create(C, TagDecl::TK_struct,
38 C.getTranslationUnitDecl(),
39 SourceLocation(), &C.Idents.get(Name), 0);
40}
41
Steve Naroffb216c882007-10-09 22:01:59 +000042void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
43 TUScope = S;
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +000044 CurContext = Context.getTranslationUnitDecl();
Chris Lattner2ae34ed2008-02-06 00:46:58 +000045 if (!PP.getLangOptions().ObjC1) return;
46
Steve Naroff69d63752008-02-24 16:25:02 +000047 // Synthesize "typedef struct objc_selector *SEL;"
Anders Carlssonc3036062008-08-23 22:20:38 +000048 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000049 PushOnScopeChains(SelTag, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000050
51 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattner0ed844b2008-04-04 06:12:32 +000052 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
53 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000054 &Context.Idents.get("SEL"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000055 SelT, 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000056 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000057 Context.setObjCSelType(SelTypedef);
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000058
Chris Lattner27933c12008-06-21 22:44:51 +000059 // FIXME: Make sure these don't leak!
Anders Carlssonc3036062008-08-23 22:20:38 +000060 RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class");
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000061 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
62 TypedefDecl *ClassTypedef =
63 TypedefDecl::Create(Context, CurContext, SourceLocation(),
64 &Context.Idents.get("Class"), ClassT, 0);
65 PushOnScopeChains(ClassTag, TUScope);
66 PushOnScopeChains(ClassTypedef, TUScope);
67 Context.setObjCClassType(ClassTypedef);
68 // Synthesize "@class Protocol;
69 ObjCInterfaceDecl *ProtocolDecl =
Chris Lattnerb752f282008-07-21 07:06:49 +000070 ObjCInterfaceDecl::Create(Context, SourceLocation(),
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000071 &Context.Idents.get("Protocol"),
72 SourceLocation(), true);
73 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
74 PushOnScopeChains(ProtocolDecl, TUScope);
75
76 // Synthesize "typedef struct objc_object { Class isa; } *id;"
Anders Carlssonc3036062008-08-23 22:20:38 +000077 RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object");
78
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000079 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
80 PushOnScopeChains(ObjectTag, TUScope);
81 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
82 SourceLocation(),
83 &Context.Idents.get("id"),
84 ObjT, 0);
85 PushOnScopeChains(IdTypedef, TUScope);
86 Context.setObjCIdType(IdTypedef);
Steve Naroff3b950172007-10-10 21:53:07 +000087}
88
Chris Lattner2ae34ed2008-02-06 00:46:58 +000089Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000090 : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0) {
Chris Lattner59907c42007-08-10 20:18:51 +000091
92 // Get IdentifierInfo objects for known functions for which we
93 // do extra checking.
Chris Lattner2ae34ed2008-02-06 00:46:58 +000094 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner59907c42007-08-10 20:18:51 +000095
Chris Lattner2ae34ed2008-02-06 00:46:58 +000096 KnownFunctionIDs[id_printf] = &IT.get("printf");
97 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
98 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
99 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
100 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
Ted Kremenek7ff22b22008-06-16 18:00:42 +0000101 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000102 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
103 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
104 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
105 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
106 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroff8ee529b2007-10-31 18:42:27 +0000107
Daniel Dunbar662e8b52008-08-14 22:04:54 +0000108 SuperID = &IT.get("super");
109
Steve Naroff3b950172007-10-10 21:53:07 +0000110 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000111 if (getLangOptions().CPlusPlus)
112 FieldCollector.reset(new CXXFieldCollector());
Reid Spencer5f016e22007-07-11 17:01:13 +0000113}
114
Chris Lattner1e0a3902008-01-16 19:17:22 +0000115/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
116/// If there is already an implicit cast, merge into the existing one.
117void Sema::ImpCastExprToType(Expr *&Expr, QualType Type) {
Chris Lattnerb77792e2008-07-26 22:17:49 +0000118 if (Context.getCanonicalType(Expr->getType()) ==
119 Context.getCanonicalType(Type)) return;
Chris Lattner1e0a3902008-01-16 19:17:22 +0000120
121 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr))
122 ImpCast->setType(Type);
123 else
124 Expr = new ImplicitCastExpr(Type, Expr);
125}
126
Chris Lattner394a3fd2007-08-31 04:53:24 +0000127void Sema::DeleteExpr(ExprTy *E) {
128 delete static_cast<Expr*>(E);
129}
130void Sema::DeleteStmt(StmtTy *S) {
131 delete static_cast<Stmt*>(S);
132}
133
Chris Lattner9299f3f2008-08-23 03:19:52 +0000134/// ActOnEndOfTranslationUnit - This is called at the very end of the
135/// translation unit when EOF is reached and all but the top-level scope is
136/// popped.
137void Sema::ActOnEndOfTranslationUnit() {
138
139}
140
141
Reid Spencer5f016e22007-07-11 17:01:13 +0000142//===----------------------------------------------------------------------===//
143// Helper functions.
144//===----------------------------------------------------------------------===//
145
146bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000147 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 return true;
149}
150
151bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000152 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 return true;
154}
155
156bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
157 const std::string &Msg2) {
158 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000159 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 return true;
161}
162
Argyrios Kyrtzidisa88b5092008-08-24 13:14:02 +0000163bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const SourceRange& Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000164 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 return true;
166}
167
168bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
Argyrios Kyrtzidisa88b5092008-08-24 13:14:02 +0000169 const SourceRange& Range) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000170 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, &Range,1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 return true;
172}
173
174bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
Argyrios Kyrtzidisa88b5092008-08-24 13:14:02 +0000175 const std::string &Msg2, const SourceRange& Range) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 std::string MsgArr[] = { Msg1, Msg2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000177 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 2, &Range, 1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 return true;
179}
180
Chris Lattner4667ac32008-01-03 23:38:43 +0000181bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
182 const std::string &Msg2, const std::string &Msg3,
Argyrios Kyrtzidisa88b5092008-08-24 13:14:02 +0000183 const SourceRange& R1) {
Chris Lattner4667ac32008-01-03 23:38:43 +0000184 std::string MsgArr[] = { Msg1, Msg2, Msg3 };
185 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, MsgArr, 3, &R1, 1);
186 return true;
187}
188
Reid Spencer5f016e22007-07-11 17:01:13 +0000189bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
Argyrios Kyrtzidisa88b5092008-08-24 13:14:02 +0000190 const SourceRange& R1, const SourceRange& R2) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000192 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, 0, 0, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 return true;
194}
195
196bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
Argyrios Kyrtzidisa88b5092008-08-24 13:14:02 +0000197 const SourceRange& R1, const SourceRange& R2) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000199 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID, &Msg, 1, RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 return true;
201}
202
203bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
Argyrios Kyrtzidisa88b5092008-08-24 13:14:02 +0000204 const std::string &Msg2, const SourceRange& R1,
205 const SourceRange& R2) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 std::string MsgArr[] = { Msg1, Msg2 };
207 SourceRange RangeArr[] = { R1, R2 };
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000208 PP.getDiagnostics().Report(PP.getFullLoc(Range),DiagID, MsgArr,2,RangeArr, 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 return true;
210}
211
212const LangOptions &Sema::getLangOptions() const {
213 return PP.getLangOptions();
214}
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000215
216ObjCMethodDecl *Sema::getCurMethodDecl() {
217 return dyn_cast<ObjCMethodDecl>(CurContext);
218}