blob: 45ef03506f296a123671d325bebad0d891a394b8 [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Chris Lattner22caddc2008-11-23 09:13:29 +000022/// ConvertQualTypeToStringFn - This function is used to pretty print the
23/// specified QualType as a string in diagnostics.
Chris Lattner011bb4e2008-11-23 20:28:15 +000024static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val,
Chris Lattner077bf5e2008-11-24 03:33:13 +000025 const char *Modifier, unsigned ModLen,
Chris Lattner22caddc2008-11-23 09:13:29 +000026 const char *Argument, unsigned ArgLen,
27 llvm::SmallVectorImpl<char> &Output) {
Chris Lattner3fdf4b02008-11-23 09:21:17 +000028
Chris Lattner011bb4e2008-11-23 20:28:15 +000029 std::string S;
30 if (Kind == Diagnostic::ak_qualtype) {
31 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
Chris Lattner077bf5e2008-11-24 03:33:13 +000032
Chris Lattner011bb4e2008-11-23 20:28:15 +000033 // FIXME: Playing with std::string is really slow.
34 S = Ty.getAsString();
Chris Lattner077bf5e2008-11-24 03:33:13 +000035
36 assert(ModLen == 0 && ArgLen == 0 &&
37 "Invalid modifier for QualType argument");
38
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000039 } else if (Kind == Diagnostic::ak_declarationname) {
Chris Lattner011bb4e2008-11-23 20:28:15 +000040
41 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
42 S = N.getAsString();
Chris Lattner077bf5e2008-11-24 03:33:13 +000043
44 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
45 S = '+' + S;
46 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) && ArgLen==0)
47 S = '-' + S;
48 else
49 assert(ModLen == 0 && ArgLen == 0 &&
50 "Invalid modifier for DeclarationName argument");
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000051 } else {
52 assert(Kind == Diagnostic::ak_nameddecl);
Douglas Gregoreeb15d42009-02-04 22:46:25 +000053 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
54 S = reinterpret_cast<NamedDecl*>(Val)->getQualifiedNameAsString();
55 else {
56 assert(ModLen == 0 && ArgLen == 0 &&
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000057 "Invalid modifier for NamedDecl* argument");
Douglas Gregoreeb15d42009-02-04 22:46:25 +000058 S = reinterpret_cast<NamedDecl*>(Val)->getNameAsString();
59 }
Chris Lattner011bb4e2008-11-23 20:28:15 +000060 }
Chris Lattner22caddc2008-11-23 09:13:29 +000061 Output.append(S.begin(), S.end());
62}
63
64
Chris Lattner0a14eee2008-11-18 07:04:44 +000065static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlssonc3036062008-08-23 22:20:38 +000066 if (C.getLangOptions().CPlusPlus)
67 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
68 C.getTranslationUnitDecl(),
Ted Kremenekdf042e62008-09-05 01:34:33 +000069 SourceLocation(), &C.Idents.get(Name));
Chris Lattnerfa25bbb2008-11-19 05:08:23 +000070
71 return RecordDecl::Create(C, TagDecl::TK_struct,
72 C.getTranslationUnitDecl(),
73 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonc3036062008-08-23 22:20:38 +000074}
75
Steve Naroffb216c882007-10-09 22:01:59 +000076void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
77 TUScope = S;
Douglas Gregor44b43212008-12-11 16:49:14 +000078 PushDeclContext(S, Context.getTranslationUnitDecl());
Chris Lattner2ae34ed2008-02-06 00:46:58 +000079 if (!PP.getLangOptions().ObjC1) return;
80
Steve Naroff69d63752008-02-24 16:25:02 +000081 // Synthesize "typedef struct objc_selector *SEL;"
Anders Carlssonc3036062008-08-23 22:20:38 +000082 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000083 PushOnScopeChains(SelTag, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000084
85 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattner0ed844b2008-04-04 06:12:32 +000086 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
87 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000088 &Context.Idents.get("SEL"),
Douglas Gregor4afa39d2009-01-20 01:17:11 +000089 SelT);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +000090 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +000091 Context.setObjCSelType(SelTypedef);
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000092
Chris Lattner27933c12008-06-21 22:44:51 +000093 // FIXME: Make sure these don't leak!
Anders Carlssonc3036062008-08-23 22:20:38 +000094 RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class");
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000095 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
96 TypedefDecl *ClassTypedef =
97 TypedefDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +000098 &Context.Idents.get("Class"), ClassT);
Chris Lattner6ee1f9c2008-06-21 20:20:39 +000099 PushOnScopeChains(ClassTag, TUScope);
100 PushOnScopeChains(ClassTypedef, TUScope);
101 Context.setObjCClassType(ClassTypedef);
102 // Synthesize "@class Protocol;
103 ObjCInterfaceDecl *ProtocolDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000104 ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000105 &Context.Idents.get("Protocol"),
106 SourceLocation(), true);
107 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
108 PushOnScopeChains(ProtocolDecl, TUScope);
109
110 // Synthesize "typedef struct objc_object { Class isa; } *id;"
Anders Carlssonc3036062008-08-23 22:20:38 +0000111 RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object");
112
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000113 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
114 PushOnScopeChains(ObjectTag, TUScope);
115 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
116 SourceLocation(),
117 &Context.Idents.get("id"),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000118 ObjT);
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000119 PushOnScopeChains(IdTypedef, TUScope);
120 Context.setObjCIdType(IdTypedef);
Steve Naroff3b950172007-10-10 21:53:07 +0000121}
122
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000123Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Chris Lattner53ebff32009-01-22 19:21:44 +0000124 : LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
125 Diags(PP.getDiagnostics()),
Chris Lattner3cfa9282008-11-22 08:28:49 +0000126 SourceMgr(PP.getSourceManager()), CurContext(0), PreDeclaratorDC(0),
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000127 CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()),
128 GlobalNewDeleteDeclared(false) {
Chris Lattner59907c42007-08-10 20:18:51 +0000129
130 // Get IdentifierInfo objects for known functions for which we
131 // do extra checking.
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000132 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner59907c42007-08-10 20:18:51 +0000133
Daniel Dunbarde454282008-10-02 18:44:07 +0000134 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
Douglas Gregora316e7b2009-02-14 00:32:47 +0000135 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
136 KnownFunctionIDs[id_fprintf] = &IT.get("fprintf");
Daniel Dunbarde454282008-10-02 18:44:07 +0000137 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
138 KnownFunctionIDs[id_vfprintf] = &IT.get("vfprintf");
Steve Naroff8ee529b2007-10-31 18:42:27 +0000139
Sebastian Redlc42e1182008-11-11 11:37:55 +0000140 StdNamespace = 0;
Steve Naroff3b950172007-10-10 21:53:07 +0000141 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000142 if (getLangOptions().CPlusPlus)
143 FieldCollector.reset(new CXXFieldCollector());
Chris Lattner22caddc2008-11-23 09:13:29 +0000144
145 // Tell diagnostics how to render things from the AST library.
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000146 PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn);
Reid Spencer5f016e22007-07-11 17:01:13 +0000147}
148
Chris Lattner1e0a3902008-01-16 19:17:22 +0000149/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
150/// If there is already an implicit cast, merge into the existing one.
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000151/// If isLvalue, the result of the cast is an lvalue.
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000152void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000153 QualType ExprTy = Context.getCanonicalType(Expr->getType());
154 QualType TypeTy = Context.getCanonicalType(Ty);
155
156 if (ExprTy == TypeTy)
157 return;
158
159 if (Expr->getType().getTypePtr()->isPointerType() &&
160 Ty.getTypePtr()->isPointerType()) {
161 QualType ExprBaseType =
162 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
163 QualType BaseType =
164 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
165 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000166 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
167 << Expr->getSourceRange();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000168 }
169 }
Chris Lattner1e0a3902008-01-16 19:17:22 +0000170
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000171 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000172 ImpCast->setType(Ty);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000173 ImpCast->setLvalueCast(isLvalue);
174 } else
Ted Kremenek8189cde2009-02-07 01:47:29 +0000175 Expr = new (Context) ImplicitCastExpr(Ty, Expr, isLvalue);
Chris Lattner1e0a3902008-01-16 19:17:22 +0000176}
177
Chris Lattner394a3fd2007-08-31 04:53:24 +0000178void Sema::DeleteExpr(ExprTy *E) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000179 if (E) static_cast<Expr*>(E)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000180}
181void Sema::DeleteStmt(StmtTy *S) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000182 if (S) static_cast<Stmt*>(S)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000183}
184
Chris Lattner9299f3f2008-08-23 03:19:52 +0000185/// ActOnEndOfTranslationUnit - This is called at the very end of the
186/// translation unit when EOF is reached and all but the top-level scope is
187/// popped.
188void Sema::ActOnEndOfTranslationUnit() {
189
190}
191
192
Reid Spencer5f016e22007-07-11 17:01:13 +0000193//===----------------------------------------------------------------------===//
194// Helper functions.
195//===----------------------------------------------------------------------===//
196
Chris Lattner371f2582008-12-04 23:50:19 +0000197/// getCurFunctionDecl - If inside of a function body, this returns a pointer
198/// to the function decl for the function being parsed. If we're currently
199/// in a 'block', this returns the containing context.
200FunctionDecl *Sema::getCurFunctionDecl() {
201 DeclContext *DC = CurContext;
202 while (isa<BlockDecl>(DC))
203 DC = DC->getParent();
204 return dyn_cast<FunctionDecl>(DC);
205}
206
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000207ObjCMethodDecl *Sema::getCurMethodDecl() {
Steve Naroffd7612e12008-11-17 16:28:52 +0000208 DeclContext *DC = CurContext;
209 while (isa<BlockDecl>(DC))
210 DC = DC->getParent();
211 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000212}
Chris Lattner371f2582008-12-04 23:50:19 +0000213
214NamedDecl *Sema::getCurFunctionOrMethodDecl() {
215 DeclContext *DC = CurContext;
216 while (isa<BlockDecl>(DC))
217 DC = DC->getParent();
218 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000219 return cast<NamedDecl>(DC);
Chris Lattner371f2582008-12-04 23:50:19 +0000220 return 0;
221}
222