blob: c9e4cd98338ac3fee4baddd0ae1f9069b7ef428c [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"
21using namespace clang;
22
Chris Lattner0a14eee2008-11-18 07:04:44 +000023static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlssonc3036062008-08-23 22:20:38 +000024 if (C.getLangOptions().CPlusPlus)
25 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
26 C.getTranslationUnitDecl(),
Ted Kremenekdf042e62008-09-05 01:34:33 +000027 SourceLocation(), &C.Idents.get(Name));
Chris Lattnerfa25bbb2008-11-19 05:08:23 +000028
29 return RecordDecl::Create(C, TagDecl::TK_struct,
30 C.getTranslationUnitDecl(),
31 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonc3036062008-08-23 22:20:38 +000032}
33
Steve Naroffb216c882007-10-09 22:01:59 +000034void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
35 TUScope = S;
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000036 PushDeclContext(Context.getTranslationUnitDecl());
Chris Lattner2ae34ed2008-02-06 00:46:58 +000037 if (!PP.getLangOptions().ObjC1) return;
38
Steve Naroff69d63752008-02-24 16:25:02 +000039 // Synthesize "typedef struct objc_selector *SEL;"
Anders Carlssonc3036062008-08-23 22:20:38 +000040 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000041 PushOnScopeChains(SelTag, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000042
43 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattner0ed844b2008-04-04 06:12:32 +000044 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
45 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000046 &Context.Idents.get("SEL"),
Chris Lattnerc63e6602008-03-15 21:32:50 +000047 SelT, 0);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000048 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000049 Context.setObjCSelType(SelTypedef);
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000050
Chris Lattner27933c12008-06-21 22:44:51 +000051 // FIXME: Make sure these don't leak!
Anders Carlssonc3036062008-08-23 22:20:38 +000052 RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class");
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000053 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
54 TypedefDecl *ClassTypedef =
55 TypedefDecl::Create(Context, CurContext, SourceLocation(),
56 &Context.Idents.get("Class"), ClassT, 0);
57 PushOnScopeChains(ClassTag, TUScope);
58 PushOnScopeChains(ClassTypedef, TUScope);
59 Context.setObjCClassType(ClassTypedef);
60 // Synthesize "@class Protocol;
61 ObjCInterfaceDecl *ProtocolDecl =
Chris Lattnerb752f282008-07-21 07:06:49 +000062 ObjCInterfaceDecl::Create(Context, SourceLocation(),
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000063 &Context.Idents.get("Protocol"),
64 SourceLocation(), true);
65 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
66 PushOnScopeChains(ProtocolDecl, TUScope);
67
68 // Synthesize "typedef struct objc_object { Class isa; } *id;"
Anders Carlssonc3036062008-08-23 22:20:38 +000069 RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object");
70
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000071 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
72 PushOnScopeChains(ObjectTag, TUScope);
73 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
74 SourceLocation(),
75 &Context.Idents.get("id"),
76 ObjT, 0);
77 PushOnScopeChains(IdTypedef, TUScope);
78 Context.setObjCIdType(IdTypedef);
Steve Naroff3b950172007-10-10 21:53:07 +000079}
80
Chris Lattner2ae34ed2008-02-06 00:46:58 +000081Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Argyrios Kyrtzidisef6e6472008-11-08 17:17:31 +000082 : PP(pp), Context(ctxt), Consumer(consumer), CurContext(0),PreDeclaratorDC(0),
83 CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()) {
Chris Lattner59907c42007-08-10 20:18:51 +000084
85 // Get IdentifierInfo objects for known functions for which we
86 // do extra checking.
Chris Lattner2ae34ed2008-02-06 00:46:58 +000087 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner59907c42007-08-10 20:18:51 +000088
Daniel Dunbarde454282008-10-02 18:44:07 +000089 KnownFunctionIDs[id_printf] = &IT.get("printf");
90 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
91 KnownFunctionIDs[id_sprintf] = &IT.get("sprintf");
92 KnownFunctionIDs[id_sprintf_chk] = &IT.get("__builtin___sprintf_chk");
93 KnownFunctionIDs[id_snprintf] = &IT.get("snprintf");
94 KnownFunctionIDs[id_snprintf_chk] = &IT.get("__builtin___snprintf_chk");
95 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
96 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
97 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
98 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
99 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
100 KnownFunctionIDs[id_vsprintf] = &IT.get("vsprintf");
101 KnownFunctionIDs[id_vsprintf_chk] = &IT.get("__builtin___vsprintf_chk");
102 KnownFunctionIDs[id_vsnprintf] = &IT.get("vsnprintf");
103 KnownFunctionIDs[id_vsnprintf_chk] = &IT.get("__builtin___vsnprintf_chk");
104 KnownFunctionIDs[id_vprintf] = &IT.get("vprintf");
Steve Naroff8ee529b2007-10-31 18:42:27 +0000105
Sebastian Redlc42e1182008-11-11 11:37:55 +0000106 StdNamespace = 0;
Steve Naroff3b950172007-10-10 21:53:07 +0000107 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000108 if (getLangOptions().CPlusPlus)
109 FieldCollector.reset(new CXXFieldCollector());
Reid Spencer5f016e22007-07-11 17:01:13 +0000110}
111
Chris Lattner1e0a3902008-01-16 19:17:22 +0000112/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
113/// If there is already an implicit cast, merge into the existing one.
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000114 /// If isLvalue, the result of the cast is an lvalue.
115void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000116 QualType ExprTy = Context.getCanonicalType(Expr->getType());
117 QualType TypeTy = Context.getCanonicalType(Ty);
118
119 if (ExprTy == TypeTy)
120 return;
121
122 if (Expr->getType().getTypePtr()->isPointerType() &&
123 Ty.getTypePtr()->isPointerType()) {
124 QualType ExprBaseType =
125 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
126 QualType BaseType =
127 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
128 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000129 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
130 << Expr->getSourceRange();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000131 }
132 }
Chris Lattner1e0a3902008-01-16 19:17:22 +0000133
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000134 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000135 ImpCast->setType(Ty);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000136 ImpCast->setLvalueCast(isLvalue);
137 } else
138 Expr = new ImplicitCastExpr(Ty, Expr, isLvalue);
Chris Lattner1e0a3902008-01-16 19:17:22 +0000139}
140
Chris Lattner394a3fd2007-08-31 04:53:24 +0000141void Sema::DeleteExpr(ExprTy *E) {
142 delete static_cast<Expr*>(E);
143}
144void Sema::DeleteStmt(StmtTy *S) {
145 delete static_cast<Stmt*>(S);
146}
147
Chris Lattner9299f3f2008-08-23 03:19:52 +0000148/// ActOnEndOfTranslationUnit - This is called at the very end of the
149/// translation unit when EOF is reached and all but the top-level scope is
150/// popped.
151void Sema::ActOnEndOfTranslationUnit() {
152
153}
154
155
Reid Spencer5f016e22007-07-11 17:01:13 +0000156//===----------------------------------------------------------------------===//
157// Helper functions.
158//===----------------------------------------------------------------------===//
159
Chris Lattnerebf5ddf2008-11-18 21:53:24 +0000160DiagnosticInfo Sema::Diag(SourceLocation Loc, unsigned DiagID) {
161 return PP.getDiagnostics().Report(FullSourceLoc(Loc, PP.getSourceManager()),
162 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000163}
164
165bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
Chris Lattner0a14eee2008-11-18 07:04:44 +0000166 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID) << Msg;
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 return true;
168}
169
170bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
171 const std::string &Msg2) {
Chris Lattner0a14eee2008-11-18 07:04:44 +0000172 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID) << Msg1 << Msg2;
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 return true;
174}
175
Reid Spencer5f016e22007-07-11 17:01:13 +0000176bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
Argyrios Kyrtzidisa88b5092008-08-24 13:14:02 +0000177 const SourceRange& Range) {
Chris Lattner0a14eee2008-11-18 07:04:44 +0000178 PP.getDiagnostics().Report(PP.getFullLoc(Loc), DiagID) << Msg << Range;
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 return true;
180}
181
Reid Spencer5f016e22007-07-11 17:01:13 +0000182const LangOptions &Sema::getLangOptions() const {
183 return PP.getLangOptions();
184}
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000185
186ObjCMethodDecl *Sema::getCurMethodDecl() {
Steve Naroffd7612e12008-11-17 16:28:52 +0000187 DeclContext *DC = CurContext;
188 while (isa<BlockDecl>(DC))
189 DC = DC->getParent();
190 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000191}