blob: 1ffaf939eff11550cf05b885cb0973e80fcf2cb3 [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 Lattnerd0344a42009-02-19 23:45:49 +000025 const char *Modifier, unsigned ModLen,
26 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) {
Chris Lattnerd0344a42009-02-19 23:45:49 +000031 assert(ModLen == 0 && ArgLen == 0 &&
32 "Invalid modifier for QualType argument");
33
Chris Lattner011bb4e2008-11-23 20:28:15 +000034 QualType Ty(QualType::getFromOpaquePtr(reinterpret_cast<void*>(Val)));
Chris Lattner077bf5e2008-11-24 03:33:13 +000035
Chris Lattner011bb4e2008-11-23 20:28:15 +000036 // FIXME: Playing with std::string is really slow.
37 S = Ty.getAsString();
Chris Lattnerd0344a42009-02-19 23:45:49 +000038
39 // If this is a sugared type (like a typedef, typeof, etc), then unwrap one
40 // level of the sugar so that the type is more obvious to the user.
41 QualType DesugaredTy = Ty->getDesugaredType();
42 DesugaredTy.setCVRQualifiers(DesugaredTy.getCVRQualifiers() |
43 Ty.getCVRQualifiers());
Chris Lattner077bf5e2008-11-24 03:33:13 +000044
Chris Lattnerd0344a42009-02-19 23:45:49 +000045 if (Ty != DesugaredTy &&
46 // If the desugared type is a vector type, we don't want to expand it,
47 // it will turn into an attribute mess. People want their "vec4".
48 !isa<VectorType>(DesugaredTy) &&
49
50 // Don't desugar objc types. FIXME: THIS IS A HACK.
51 S != "id" && S != "Class") {
52 S = "'"+S+"' (aka '";
53 S += DesugaredTy.getAsString();
54 S += "')";
55 Output.append(S.begin(), S.end());
56 return;
57 }
Chris Lattner077bf5e2008-11-24 03:33:13 +000058
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000059 } else if (Kind == Diagnostic::ak_declarationname) {
Chris Lattner011bb4e2008-11-23 20:28:15 +000060
61 DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
62 S = N.getAsString();
Chris Lattner077bf5e2008-11-24 03:33:13 +000063
64 if (ModLen == 9 && !memcmp(Modifier, "objcclass", 9) && ArgLen == 0)
65 S = '+' + S;
66 else if (ModLen == 12 && !memcmp(Modifier, "objcinstance", 12) && ArgLen==0)
67 S = '-' + S;
68 else
69 assert(ModLen == 0 && ArgLen == 0 &&
70 "Invalid modifier for DeclarationName argument");
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000071 } else {
72 assert(Kind == Diagnostic::ak_nameddecl);
Douglas Gregoreeb15d42009-02-04 22:46:25 +000073 if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
74 S = reinterpret_cast<NamedDecl*>(Val)->getQualifiedNameAsString();
75 else {
76 assert(ModLen == 0 && ArgLen == 0 &&
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000077 "Invalid modifier for NamedDecl* argument");
Douglas Gregoreeb15d42009-02-04 22:46:25 +000078 S = reinterpret_cast<NamedDecl*>(Val)->getNameAsString();
79 }
Chris Lattner011bb4e2008-11-23 20:28:15 +000080 }
Chris Lattnerd0344a42009-02-19 23:45:49 +000081
82 Output.push_back('\'');
Chris Lattner22caddc2008-11-23 09:13:29 +000083 Output.append(S.begin(), S.end());
Chris Lattnerd0344a42009-02-19 23:45:49 +000084 Output.push_back('\'');
Chris Lattner22caddc2008-11-23 09:13:29 +000085}
86
87
Chris Lattner0a14eee2008-11-18 07:04:44 +000088static inline RecordDecl *CreateStructDecl(ASTContext &C, const char *Name) {
Anders Carlssonc3036062008-08-23 22:20:38 +000089 if (C.getLangOptions().CPlusPlus)
90 return CXXRecordDecl::Create(C, TagDecl::TK_struct,
91 C.getTranslationUnitDecl(),
Ted Kremenekdf042e62008-09-05 01:34:33 +000092 SourceLocation(), &C.Idents.get(Name));
Chris Lattnerfa25bbb2008-11-19 05:08:23 +000093
94 return RecordDecl::Create(C, TagDecl::TK_struct,
95 C.getTranslationUnitDecl(),
96 SourceLocation(), &C.Idents.get(Name));
Anders Carlssonc3036062008-08-23 22:20:38 +000097}
98
Steve Naroffb216c882007-10-09 22:01:59 +000099void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
100 TUScope = S;
Douglas Gregor44b43212008-12-11 16:49:14 +0000101 PushDeclContext(S, Context.getTranslationUnitDecl());
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000102 if (!PP.getLangOptions().ObjC1) return;
103
Steve Naroff69d63752008-02-24 16:25:02 +0000104 // Synthesize "typedef struct objc_selector *SEL;"
Anders Carlssonc3036062008-08-23 22:20:38 +0000105 RecordDecl *SelTag = CreateStructDecl(Context, "objc_selector");
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +0000106 PushOnScopeChains(SelTag, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +0000107
108 QualType SelT = Context.getPointerType(Context.getTagDeclType(SelTag));
Chris Lattner0ed844b2008-04-04 06:12:32 +0000109 TypedefDecl *SelTypedef = TypedefDecl::Create(Context, CurContext,
110 SourceLocation(),
Chris Lattner6c2b6eb2008-03-15 06:12:44 +0000111 &Context.Idents.get("SEL"),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000112 SelT);
Argyrios Kyrtzidis87f3ff02008-04-12 00:47:19 +0000113 PushOnScopeChains(SelTypedef, TUScope);
Steve Naroff69d63752008-02-24 16:25:02 +0000114 Context.setObjCSelType(SelTypedef);
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000115
Chris Lattner27933c12008-06-21 22:44:51 +0000116 // FIXME: Make sure these don't leak!
Anders Carlssonc3036062008-08-23 22:20:38 +0000117 RecordDecl *ClassTag = CreateStructDecl(Context, "objc_class");
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000118 QualType ClassT = Context.getPointerType(Context.getTagDeclType(ClassTag));
119 TypedefDecl *ClassTypedef =
120 TypedefDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000121 &Context.Idents.get("Class"), ClassT);
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000122 PushOnScopeChains(ClassTag, TUScope);
123 PushOnScopeChains(ClassTypedef, TUScope);
124 Context.setObjCClassType(ClassTypedef);
125 // Synthesize "@class Protocol;
126 ObjCInterfaceDecl *ProtocolDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000127 ObjCInterfaceDecl::Create(Context, CurContext, SourceLocation(),
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000128 &Context.Idents.get("Protocol"),
129 SourceLocation(), true);
130 Context.setObjCProtoType(Context.getObjCInterfaceType(ProtocolDecl));
131 PushOnScopeChains(ProtocolDecl, TUScope);
132
133 // Synthesize "typedef struct objc_object { Class isa; } *id;"
Anders Carlssonc3036062008-08-23 22:20:38 +0000134 RecordDecl *ObjectTag = CreateStructDecl(Context, "objc_object");
135
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000136 QualType ObjT = Context.getPointerType(Context.getTagDeclType(ObjectTag));
137 PushOnScopeChains(ObjectTag, TUScope);
138 TypedefDecl *IdTypedef = TypedefDecl::Create(Context, CurContext,
139 SourceLocation(),
140 &Context.Idents.get("id"),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000141 ObjT);
Chris Lattner6ee1f9c2008-06-21 20:20:39 +0000142 PushOnScopeChains(IdTypedef, TUScope);
143 Context.setObjCIdType(IdTypedef);
Steve Naroff3b950172007-10-10 21:53:07 +0000144}
145
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000146Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer)
Chris Lattner53ebff32009-01-22 19:21:44 +0000147 : LangOpts(pp.getLangOptions()), PP(pp), Context(ctxt), Consumer(consumer),
148 Diags(PP.getDiagnostics()),
Chris Lattner3cfa9282008-11-22 08:28:49 +0000149 SourceMgr(PP.getSourceManager()), CurContext(0), PreDeclaratorDC(0),
Sebastian Redlb5a57a62008-12-03 20:26:15 +0000150 CurBlock(0), PackContext(0), IdResolver(pp.getLangOptions()),
151 GlobalNewDeleteDeclared(false) {
Chris Lattner59907c42007-08-10 20:18:51 +0000152
153 // Get IdentifierInfo objects for known functions for which we
154 // do extra checking.
Chris Lattner2ae34ed2008-02-06 00:46:58 +0000155 IdentifierTable &IT = PP.getIdentifierTable();
Chris Lattner59907c42007-08-10 20:18:51 +0000156
Daniel Dunbarde454282008-10-02 18:44:07 +0000157 KnownFunctionIDs[id_NSLog] = &IT.get("NSLog");
Douglas Gregor3c385e52009-02-14 18:57:46 +0000158 KnownFunctionIDs[id_NSLogv] = &IT.get("NSLogv");
Douglas Gregora316e7b2009-02-14 00:32:47 +0000159 KnownFunctionIDs[id_asprintf] = &IT.get("asprintf");
Daniel Dunbarde454282008-10-02 18:44:07 +0000160 KnownFunctionIDs[id_vasprintf] = &IT.get("vasprintf");
Steve Naroff8ee529b2007-10-31 18:42:27 +0000161
Sebastian Redlc42e1182008-11-11 11:37:55 +0000162 StdNamespace = 0;
Steve Naroff3b950172007-10-10 21:53:07 +0000163 TUScope = 0;
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000164 if (getLangOptions().CPlusPlus)
165 FieldCollector.reset(new CXXFieldCollector());
Chris Lattner22caddc2008-11-23 09:13:29 +0000166
167 // Tell diagnostics how to render things from the AST library.
Chris Lattner3fdf4b02008-11-23 09:21:17 +0000168 PP.getDiagnostics().SetArgToStringFn(ConvertArgToStringFn);
Reid Spencer5f016e22007-07-11 17:01:13 +0000169}
170
Chris Lattner1e0a3902008-01-16 19:17:22 +0000171/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
172/// If there is already an implicit cast, merge into the existing one.
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000173/// If isLvalue, the result of the cast is an lvalue.
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000174void Sema::ImpCastExprToType(Expr *&Expr, QualType Ty, bool isLvalue) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000175 QualType ExprTy = Context.getCanonicalType(Expr->getType());
176 QualType TypeTy = Context.getCanonicalType(Ty);
177
178 if (ExprTy == TypeTy)
179 return;
180
181 if (Expr->getType().getTypePtr()->isPointerType() &&
182 Ty.getTypePtr()->isPointerType()) {
183 QualType ExprBaseType =
184 cast<PointerType>(ExprTy.getUnqualifiedType())->getPointeeType();
185 QualType BaseType =
186 cast<PointerType>(TypeTy.getUnqualifiedType())->getPointeeType();
187 if (ExprBaseType.getAddressSpace() != BaseType.getAddressSpace()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000188 Diag(Expr->getExprLoc(), diag::err_implicit_pointer_address_space_cast)
189 << Expr->getSourceRange();
Mon P Wang3a2c7442008-09-04 08:38:01 +0000190 }
191 }
Chris Lattner1e0a3902008-01-16 19:17:22 +0000192
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000193 if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(Expr)) {
Mon P Wang3a2c7442008-09-04 08:38:01 +0000194 ImpCast->setType(Ty);
Douglas Gregoreb8f3062008-11-12 17:17:38 +0000195 ImpCast->setLvalueCast(isLvalue);
196 } else
Ted Kremenek8189cde2009-02-07 01:47:29 +0000197 Expr = new (Context) ImplicitCastExpr(Ty, Expr, isLvalue);
Chris Lattner1e0a3902008-01-16 19:17:22 +0000198}
199
Chris Lattner394a3fd2007-08-31 04:53:24 +0000200void Sema::DeleteExpr(ExprTy *E) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000201 if (E) static_cast<Expr*>(E)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000202}
203void Sema::DeleteStmt(StmtTy *S) {
Douglas Gregor05c13a32009-01-22 00:58:24 +0000204 if (S) static_cast<Stmt*>(S)->Destroy(Context);
Chris Lattner394a3fd2007-08-31 04:53:24 +0000205}
206
Chris Lattner9299f3f2008-08-23 03:19:52 +0000207/// ActOnEndOfTranslationUnit - This is called at the very end of the
208/// translation unit when EOF is reached and all but the top-level scope is
209/// popped.
210void Sema::ActOnEndOfTranslationUnit() {
211
212}
213
214
Reid Spencer5f016e22007-07-11 17:01:13 +0000215//===----------------------------------------------------------------------===//
216// Helper functions.
217//===----------------------------------------------------------------------===//
218
Chris Lattner371f2582008-12-04 23:50:19 +0000219/// getCurFunctionDecl - If inside of a function body, this returns a pointer
220/// to the function decl for the function being parsed. If we're currently
221/// in a 'block', this returns the containing context.
222FunctionDecl *Sema::getCurFunctionDecl() {
223 DeclContext *DC = CurContext;
224 while (isa<BlockDecl>(DC))
225 DC = DC->getParent();
226 return dyn_cast<FunctionDecl>(DC);
227}
228
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000229ObjCMethodDecl *Sema::getCurMethodDecl() {
Steve Naroffd7612e12008-11-17 16:28:52 +0000230 DeclContext *DC = CurContext;
231 while (isa<BlockDecl>(DC))
232 DC = DC->getParent();
233 return dyn_cast<ObjCMethodDecl>(DC);
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +0000234}
Chris Lattner371f2582008-12-04 23:50:19 +0000235
236NamedDecl *Sema::getCurFunctionOrMethodDecl() {
237 DeclContext *DC = CurContext;
238 while (isa<BlockDecl>(DC))
239 DC = DC->getParent();
240 if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000241 return cast<NamedDecl>(DC);
Chris Lattner371f2582008-12-04 23:50:19 +0000242 return 0;
243}
244