 |
|
 |
|
| Files: |
1 |
|
Branches Taken: |
82.6% |
3122 / 3781 |
| Generated: |
2010-02-10 01:31 |
|
Branches Executed: |
97.8% |
3697 / 3781 |
| |
|
Line Coverage: |
92.7% |
3044 / 3282 |
| |
 |
|
 |
1 : //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 : //
10 : // This file implements semantic analysis for expressions.
11 : //
12 : //===----------------------------------------------------------------------===//
13 :
14 : #include "Sema.h"
15 : #include "SemaInit.h"
16 : #include "Lookup.h"
17 : #include "clang/Analysis/AnalysisContext.h"
18 : #include "clang/AST/ASTContext.h"
19 : #include "clang/AST/DeclObjC.h"
20 : #include "clang/AST/DeclTemplate.h"
21 : #include "clang/AST/ExprCXX.h"
22 : #include "clang/AST/ExprObjC.h"
23 : #include "clang/Basic/PartialDiagnostic.h"
24 : #include "clang/Basic/SourceManager.h"
25 : #include "clang/Basic/TargetInfo.h"
26 : #include "clang/Lex/LiteralSupport.h"
27 : #include "clang/Lex/Preprocessor.h"
28 : #include "clang/Parse/DeclSpec.h"
29 : #include "clang/Parse/Designator.h"
30 : #include "clang/Parse/Scope.h"
31 : #include "clang/Parse/Template.h"
32 : using namespace clang;
33 :
34 :
35 : /// \brief Determine whether the use of this declaration is valid, and
36 : /// emit any corresponding diagnostics.
37 : ///
38 : /// This routine diagnoses various problems with referencing
39 : /// declarations that can occur when using a declaration. For example,
40 : /// it might warn if a deprecated or unavailable declaration is being
41 : /// used, or produce an error (and return true) if a C++0x deleted
42 : /// function is being used.
43 : ///
44 : /// If IgnoreDeprecated is set to true, this should not want about deprecated
45 : /// decls.
46 : ///
47 : /// \returns true if there was an error (this declaration cannot be
48 : /// referenced), false otherwise.
49 : ///
50 57319: bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc) {
51 : // See if the decl is deprecated.
73: branch 1 taken
57246: branch 2 taken
52 57319: if (D->getAttr<DeprecatedAttr>()) {
53 73: EmitDeprecationWarning(D, Loc);
54 : }
55 :
56 : // See if the decl is unavailable
6: branch 1 taken
57313: branch 2 taken
57 57319: if (D->getAttr<UnavailableAttr>()) {
58 6: Diag(Loc, diag::warn_unavailable) << D->getDeclName();
59 6: Diag(D->getLocation(), diag::note_unavailable_here) << 0;
60 : }
61 :
62 : // See if this is a deleted function.
5144: branch 1 taken
52175: branch 2 taken
63 57319: if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1: branch 1 taken
5143: branch 2 taken
64 5144: if (FD->isDeleted()) {
65 1: Diag(Loc, diag::err_deleted_function_use);
66 1: Diag(D->getLocation(), diag::note_unavailable_here) << true;
67 1: return true;
68 : }
69 : }
70 :
71 57318: return false;
72 : }
73 :
74 : /// DiagnoseSentinelCalls - This routine checks on method dispatch calls
75 : /// (and other functions in future), which have been declared with sentinel
76 : /// attribute. It warns if call does not have the sentinel argument.
77 : ///
78 : void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc,
79 6223: Expr **Args, unsigned NumArgs) {
80 6223: const SentinelAttr *attr = D->getAttr<SentinelAttr>();
6190: branch 0 taken
33: branch 1 taken
81 6223: if (!attr)
82 6190: return;
83 33: int sentinelPos = attr->getSentinel();
84 33: int nullPos = attr->getNullPos();
85 :
86 : // FIXME. ObjCMethodDecl and FunctionDecl need be derived from the same common
87 : // base class. Then we won't be needing two versions of the same code.
88 33: unsigned int i = 0;
89 33: bool warnNotEnoughArgs = false;
90 33: int isMethod = 0;
10: branch 1 taken
23: branch 2 taken
91 33: if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
92 : // skip over named parameters.
93 10: ObjCMethodDecl::param_iterator P, E = MD->param_end();
10: branch 1 taken
10: branch 2 taken
10: branch 3 taken
0: branch 4 not taken
94 20: for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) {
0: branch 0 not taken
10: branch 1 taken
95 10: if (nullPos)
96 0: --nullPos;
97 : else
98 10: ++i;
99 : }
10: branch 0 taken
0: branch 1 not taken
2: branch 2 taken
8: branch 3 taken
100 10: warnNotEnoughArgs = (P != E || i >= NumArgs);
101 10: isMethod = 1;
11: branch 1 taken
12: branch 2 taken
102 23: } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
103 : // skip over named parameters.
104 11: ObjCMethodDecl::param_iterator P, E = FD->param_end();
11: branch 1 taken
11: branch 2 taken
11: branch 3 taken
0: branch 4 not taken
105 22: for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) {
0: branch 0 not taken
11: branch 1 taken
106 11: if (nullPos)
107 0: --nullPos;
108 : else
109 11: ++i;
110 : }
11: branch 0 taken
0: branch 1 not taken
2: branch 2 taken
9: branch 3 taken
111 11: warnNotEnoughArgs = (P != E || i >= NumArgs);
0: branch 1 not taken
12: branch 2 taken
112 12: } else if (VarDecl *V = dyn_cast<VarDecl>(D)) {
113 : // block or function pointer call.
114 12: QualType Ty = V->getType();
6: branch 2 taken
6: branch 3 taken
6: branch 6 taken
0: branch 7 not taken
0: branch 8 not taken
12: branch 9 taken
115 12: if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
116 : const FunctionType *FT = Ty->isFunctionPointerType()
117 : ? Ty->getAs<PointerType>()->getPointeeType()->getAs<FunctionType>()
6: branch 2 taken
6: branch 3 taken
118 12: : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
12: branch 1 taken
0: branch 2 not taken
119 12: if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) {
120 12: unsigned NumArgsInProto = Proto->getNumArgs();
121 : unsigned k;
24: branch 0 taken
12: branch 1 taken
24: branch 2 taken
0: branch 3 not taken
122 36: for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) {
0: branch 0 not taken
24: branch 1 taken
123 24: if (nullPos)
124 0: --nullPos;
125 : else
126 24: ++i;
127 : }
12: branch 0 taken
0: branch 1 not taken
0: branch 2 not taken
12: branch 3 taken
128 12: warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs);
129 : }
6: branch 2 taken
6: branch 3 taken
130 12: if (Ty->isBlockPointerType())
131 6: isMethod = 2;
132 : } else
133 0: return;
134 : } else
135 0: return;
136 :
4: branch 0 taken
29: branch 1 taken
137 33: if (warnNotEnoughArgs) {
138 4: Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName();
139 4: Diag(D->getLocation(), diag::note_sentinel_here) << isMethod;
140 4: return;
141 : }
142 29: int sentinel = i;
54: branch 0 taken
29: branch 1 taken
54: branch 2 taken
0: branch 3 not taken
143 112: while (sentinelPos > 0 && i < NumArgs-1) {
144 54: --sentinelPos;
145 54: ++i;
146 : }
0: branch 0 not taken
29: branch 1 taken
147 29: if (sentinelPos > 0) {
148 0: Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName();
149 0: Diag(D->getLocation(), diag::note_sentinel_here) << isMethod;
150 0: return;
151 : }
7: branch 0 taken
29: branch 1 taken
152 65: while (i < NumArgs-1) {
153 7: ++i;
154 7: ++sentinel;
155 : }
156 29: Expr *sentinelExpr = Args[sentinel];
29: branch 0 taken
0: branch 1 not taken
28: branch 3 taken
1: branch 4 taken
16: branch 8 taken
12: branch 9 taken
0: branch 11 not taken
16: branch 12 taken
12: branch 13 taken
17: branch 14 taken
157 29: if (sentinelExpr && (!isa<GNUNullExpr>(sentinelExpr) &&
158 : (!sentinelExpr->getType()->isPointerType() ||
159 : !sentinelExpr->isNullPointerConstant(Context,
160 : Expr::NPC_ValueDependentIsNull)))) {
161 12: Diag(Loc, diag::warn_missing_sentinel) << isMethod;
162 12: Diag(D->getLocation(), diag::note_sentinel_here) << isMethod;
163 : }
164 29: return;
165 : }
166 :
167 6: SourceRange Sema::getExprRange(ExprTy *E) const {
168 6: Expr *Ex = (Expr *)E;
6: branch 0 taken
0: branch 1 not taken
169 6: return Ex? Ex->getSourceRange() : SourceRange();
170 : }
171 :
172 : //===----------------------------------------------------------------------===//
173 : // Standard Promotions and Conversions
174 : //===----------------------------------------------------------------------===//
175 :
176 : /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
177 51936: void Sema::DefaultFunctionArrayConversion(Expr *&E) {
178 51936: QualType Ty = E->getType();
51936: branch 1 taken
0: branch 2 not taken
179 51936: assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type");
180 :
5958: branch 2 taken
45978: branch 3 taken
181 51936: if (Ty->isFunctionType())
182 : ImpCastExprToType(E, Context.getPointerType(Ty),
183 5958: CastExpr::CK_FunctionToPointerDecay);
1711: branch 2 taken
44267: branch 3 taken
184 45978: else if (Ty->isArrayType()) {
185 : // In C90 mode, arrays only promote to pointers if the array expression is
186 : // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has
187 : // type 'array of type' is converted to an expression that has type 'pointer
188 : // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression
189 : // that has type 'array of type' ...". The relevant change is "an lvalue"
190 : // (C90) to "an expression" (C99).
191 : //
192 : // C++ 4.2p1:
193 : // An lvalue or rvalue of type "array of N T" or "array of unknown bound of
194 : // T" can be converted to an rvalue of type "pointer to T".
195 : //
353: branch 1 taken
1358: branch 2 taken
6: branch 4 taken
347: branch 5 taken
1: branch 7 taken
5: branch 8 taken
1706: branch 9 taken
5: branch 10 taken
196 1711: if (getLangOptions().C99 || getLangOptions().CPlusPlus ||
197 : E->isLvalue(Context) == Expr::LV_Valid)
198 : ImpCastExprToType(E, Context.getArrayDecayedType(Ty),
199 1706: CastExpr::CK_ArrayToPointerDecay);
200 : }
201 51936: }
202 :
203 49755: void Sema::DefaultFunctionArrayLvalueConversion(Expr *&E) {
204 49755: DefaultFunctionArrayConversion(E);
205 :
206 49755: QualType Ty = E->getType();
49755: branch 1 taken
0: branch 2 not taken
207 49755: assert(!Ty.isNull() && "DefaultFunctionArrayLvalueConversion - missing type");
49752: branch 2 taken
3: branch 3 taken
518: branch 5 taken
49234: branch 6 taken
189: branch 8 taken
329: branch 9 taken
173: branch 12 taken
16: branch 13 taken
498: branch 15 taken
4: branch 16 taken
498: branch 17 taken
49257: branch 18 taken
208 99507: if (!Ty->isDependentType() && Ty.hasQualifiers() &&
209 : (!getLangOptions().CPlusPlus || !Ty->isRecordType()) &&
210 : E->isLvalue(Context) == Expr::LV_Valid) {
211 : // C++ [conv.lval]p1:
212 : // [...] If T is a non-class type, the type of the rvalue is the
213 : // cv-unqualified version of T. Otherwise, the type of the
214 : // rvalue is T
215 : //
216 : // C99 6.3.2.1p2:
217 : // If the lvalue has qualified type, the value has the unqualified
218 : // version of the type of the lvalue; otherwise, the value has the
219 : // type of the lvalue.
220 498: ImpCastExprToType(E, Ty.getUnqualifiedType(), CastExpr::CK_NoOp);
221 : }
222 49755: }
223 :
224 :
225 : /// UsualUnaryConversions - Performs various conversions that are common to most
226 : /// operators (C99 6.3). The conversions of array and function types are
227 : /// sometimes surpressed. For example, the array->pointer conversion doesn't
228 : /// apply if the array is an argument to the sizeof or address (&) operators.
229 : /// In these instances, this routine should *not* be called.
230 22777: Expr *Sema::UsualUnaryConversions(Expr *&Expr) {
231 22777: QualType Ty = Expr->getType();
22777: branch 1 taken
0: branch 2 not taken
232 22777: assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
233 :
234 : // C99 6.3.1.1p2:
235 : //
236 : // The following may be used in an expression wherever an int or
237 : // unsigned int may be used:
238 : // - an object or expression with an integer type whose integer
239 : // conversion rank is less than or equal to the rank of int
240 : // and unsigned int.
241 : // - A bit-field of type _Bool, int, signed int, or unsigned int.
242 : //
243 : // If an int can represent all values of the original type, the
244 : // value is converted to an int; otherwise, it is converted to an
245 : // unsigned int. These are called the integer promotions. All
246 : // other types are unchanged by the integer promotions.
247 22777: QualType PTy = Context.isPromotableBitField(Expr);
44: branch 1 taken
22733: branch 2 taken
248 22777: if (!PTy.isNull()) {
249 44: ImpCastExprToType(Expr, PTy, CastExpr::CK_IntegralCast);
250 44: return Expr;
251 : }
635: branch 2 taken
22098: branch 3 taken
252 22733: if (Ty->isPromotableIntegerType()) {
253 635: QualType PT = Context.getPromotedIntegerType(Ty);
254 635: ImpCastExprToType(Expr, PT, CastExpr::CK_IntegralCast);
255 635: return Expr;
256 : }
257 :
258 22098: DefaultFunctionArrayLvalueConversion(Expr);
259 22098: return Expr;
260 : }
261 :
262 : /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
263 : /// do not have a prototype. Arguments that have type float are promoted to
264 : /// double. All other argument types are converted by UsualUnaryConversions().
265 2161: void Sema::DefaultArgumentPromotion(Expr *&Expr) {
266 2161: QualType Ty = Expr->getType();
2161: branch 1 taken
0: branch 2 not taken
267 2161: assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type");
268 :
269 : // If this is a 'float' (CVR qualified or typedef) promote to double.
1277: branch 2 taken
884: branch 3 taken
270 2161: if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
131: branch 1 taken
1146: branch 2 taken
271 1277: if (BT->getKind() == BuiltinType::Float)
272 : return ImpCastExprToType(Expr, Context.DoubleTy,
273 131: CastExpr::CK_FloatingCast);
274 :
275 2030: UsualUnaryConversions(Expr);
276 : }
277 :
278 : /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but
279 : /// will warn if the resulting type is not a POD type, and rejects ObjC
280 : /// interfaces passed by value. This returns true if the argument type is
281 : /// completely illegal.
282 1195: bool Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT) {
283 1195: DefaultArgumentPromotion(Expr);
284 :
1: branch 3 taken
1194: branch 4 taken
1: branch 11 taken
0: branch 12 not taken
1: branch 13 taken
1194: branch 14 taken
1: branch 16 taken
1194: branch 17 taken
285 1195: if (Expr->getType()->isObjCInterfaceType() &&
286 : DiagRuntimeBehavior(Expr->getLocStart(),
287 : PDiag(diag::err_cannot_pass_objc_interface_to_vararg)
288 : << Expr->getType() << CT))
289 1: return true;
290 :
15: branch 3 taken
1179: branch 4 taken
12: branch 11 taken
3: branch 12 taken
15: branch 13 taken
1179: branch 14 taken
12: branch 16 taken
1182: branch 17 taken
291 1194: if (!Expr->getType()->isPODType() &&
292 : DiagRuntimeBehavior(Expr->getLocStart(),
293 : PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg)
294 : << Expr->getType() << CT))
295 12: return true;
296 :
297 1182: return false;
298 : }
299 :
300 :
301 : /// UsualArithmeticConversions - Performs various conversions that are common to
302 : /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
303 : /// routine returns the first non-arithmetic type found. The client is
304 : /// responsible for emitting appropriate error diagnostics.
305 : /// FIXME: verify the conversion rules for "complex int" are consistent with
306 : /// GCC.
307 : QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
308 5054: bool isCompAssign) {
4782: branch 0 taken
272: branch 1 taken
309 5054: if (!isCompAssign)
310 4782: UsualUnaryConversions(lhsExpr);
311 :
312 5054: UsualUnaryConversions(rhsExpr);
313 :
314 : // For conversion purposes, we ignore any qualifiers.
315 : // For example, "const float" and "float" are equivalent.
316 : QualType lhs =
317 5054: Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType();
318 : QualType rhs =
319 5054: Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType();
320 :
321 : // If both types are identical, no conversion is needed.
3151: branch 1 taken
1903: branch 2 taken
322 5054: if (lhs == rhs)
323 3151: return lhs;
324 :
325 : // If either side is a non-arithmetic type (e.g. a pointer), we are done.
326 : // The caller can deal with this (e.g. pointer + int).
1715: branch 2 taken
188: branch 3 taken
15: branch 6 taken
1700: branch 7 taken
203: branch 8 taken
1700: branch 9 taken
327 1903: if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
328 203: return lhs;
329 :
330 : // Perform bitfield promotions.
331 1700: QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr);
6: branch 1 taken
1694: branch 2 taken
332 1700: if (!LHSBitfieldPromoteTy.isNull())
333 6: lhs = LHSBitfieldPromoteTy;
334 1700: QualType RHSBitfieldPromoteTy = Context.isPromotableBitField(rhsExpr);
0: branch 1 not taken
1700: branch 2 taken
335 1700: if (!RHSBitfieldPromoteTy.isNull())
336 0: rhs = RHSBitfieldPromoteTy;
337 :
338 1700: QualType destType = Context.UsualArithmeticConversionsType(lhs, rhs);
1636: branch 0 taken
64: branch 1 taken
339 1700: if (!isCompAssign)
340 1636: ImpCastExprToType(lhsExpr, destType, CastExpr::CK_Unknown);
341 1700: ImpCastExprToType(rhsExpr, destType, CastExpr::CK_Unknown);
342 1700: return destType;
343 : }
344 :
345 : //===----------------------------------------------------------------------===//
346 : // Semantic Analysis for various Expression Types
347 : //===----------------------------------------------------------------------===//
348 :
349 :
350 : /// ActOnStringLiteral - The specified tokens were lexed as pasted string
351 : /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
352 : /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
353 : /// multiple tokens. However, the common case is that StringToks points to one
354 : /// string.
355 : ///
356 : Action::OwningExprResult
357 1785: Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
0: branch 0 not taken
1785: branch 1 taken
358 1785: assert(NumStringToks && "Must have at least one string!");
359 :
360 1785: StringLiteralParser Literal(StringToks, NumStringToks, PP);
4: branch 0 taken
1781: branch 1 taken
361 1785: if (Literal.hadError)
362 4: return ExprError();
363 :
364 1781: llvm::SmallVector<SourceLocation, 4> StringTokLocs;
1956: branch 0 taken
1781: branch 1 taken
365 3737: for (unsigned i = 0; i != NumStringToks; ++i)
366 1956: StringTokLocs.push_back(StringToks[i].getLocation());
367 :
368 1781: QualType StrTy = Context.CharTy;
30: branch 0 taken
1751: branch 1 taken
369 1781: if (Literal.AnyWide) StrTy = Context.getWCharType();
2: branch 0 taken
1779: branch 1 taken
370 1781: if (Literal.Pascal) StrTy = Context.UnsignedCharTy;
371 :
372 : // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
445: branch 1 taken
1336: branch 2 taken
373 1781: if (getLangOptions().CPlusPlus)
374 445: StrTy.addConst();
375 :
376 : // Get an array type for the string, according to C99 6.4.5. This includes
377 : // the nul terminator character as well as the string length for pascal
378 : // strings.
379 : StrTy = Context.getConstantArrayType(StrTy,
380 : llvm::APInt(32, Literal.GetNumStringChars()+1),
381 1781: ArrayType::Normal, 0);
382 :
383 : // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
384 : return Owned(StringLiteral::Create(Context, Literal.GetString(),
385 : Literal.GetStringLength(),
386 : Literal.AnyWide, StrTy,
387 : &StringTokLocs[0],
388 1781: StringTokLocs.size()));
389 : }
390 :
391 : /// ShouldSnapshotBlockValueReference - Return true if a reference inside of
392 : /// CurBlock to VD should cause it to be snapshotted (as we do for auto
393 : /// variables defined outside the block) or false if this is not needed (e.g.
394 : /// for values inside the block or for globals).
395 : ///
396 : /// This also keeps the 'hasBlockDeclRefExprs' in the BlockSemaInfo records
397 : /// up-to-date.
398 : ///
399 : static bool ShouldSnapshotBlockValueReference(BlockSemaInfo *CurBlock,
400 306: ValueDecl *VD) {
401 : // If the value is defined inside the block, we couldn't snapshot it even if
402 : // we wanted to.
306: branch 0 taken
0: branch 1 not taken
76: branch 3 taken
230: branch 4 taken
403 306: if (CurBlock->TheDecl == VD->getDeclContext())
404 76: return false;
405 :
406 : // If this is an enum constant or function, it is constant, don't snapshot.
228: branch 1 taken
2: branch 2 taken
33: branch 4 taken
195: branch 5 taken
35: branch 6 taken
195: branch 7 taken
407 230: if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD))
408 35: return false;
409 :
410 : // If this is a reference to an extern, static, or global variable, no need to
411 : // snapshot it.
412 : // FIXME: What about 'const' variables in C++?
195: branch 1 taken
0: branch 2 not taken
413 195: if (const VarDecl *Var = dyn_cast<VarDecl>(VD))
17: branch 1 taken
178: branch 2 taken
414 195: if (!Var->hasLocalStorage())
415 17: return false;
416 :
417 : // Blocks that have these can't be constant.
418 178: CurBlock->hasBlockDeclRefExprs = true;
419 :
420 : // If we have nested blocks, the decl may be declared in an outer block (in
421 : // which case that outer block doesn't get "hasBlockDeclRefExprs") or it may
422 : // be defined outside all of the current blocks (in which case the blocks do
423 : // all get the bit). Walk the nesting chain.
11: branch 0 taken
174: branch 1 taken
424 185: for (BlockSemaInfo *NextBlock = CurBlock->PrevBlockInfo; NextBlock;
425 : NextBlock = NextBlock->PrevBlockInfo) {
426 : // If we found the defining block for the variable, don't mark the block as
427 : // having a reference outside it.
11: branch 0 taken
0: branch 1 not taken
4: branch 3 taken
7: branch 4 taken
428 11: if (NextBlock->TheDecl == VD->getDeclContext())
429 4: break;
430 :
431 : // Otherwise, the DeclRef from the inner block causes the outer one to need
432 : // a snapshot as well.
433 7: NextBlock->hasBlockDeclRefExprs = true;
434 : }
435 :
436 178: return true;
437 : }
438 :
439 :
440 :
441 : /// BuildDeclRefExpr - Build a DeclRefExpr.
442 : Sema::OwningExprResult
443 : Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, SourceLocation Loc,
444 27991: const CXXScopeSpec *SS) {
2: branch 2 taken
27989: branch 3 taken
445 27991: if (Context.getCanonicalType(Ty) == Context.UndeducedAutoTy) {
446 : Diag(Loc,
447 : diag::err_auto_variable_cannot_appear_in_own_initializer)
448 2: << D->getDeclName();
449 2: return ExprError();
450 : }
451 :
22399: branch 1 taken
5590: branch 2 taken
452 27989: if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1093: branch 1 taken
21306: branch 2 taken
453 22399: if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) {
10: branch 2 taken
1083: branch 3 taken
454 1093: if (const FunctionDecl *FD = MD->getParent()->isLocalClass()) {
3: branch 1 taken
7: branch 2 taken
2: branch 4 taken
1: branch 5 taken
2: branch 6 taken
8: branch 7 taken
455 10: if (VD->hasLocalStorage() && VD->getDeclContext() != CurContext) {
456 : Diag(Loc, diag::err_reference_to_local_var_in_enclosing_function)
457 2: << D->getIdentifier() << FD->getDeclName();
458 : Diag(D->getLocation(), diag::note_local_variable_declared_here)
459 2: << D->getIdentifier();
460 2: return ExprError();
461 : }
462 : }
463 : }
464 : }
465 :
466 27987: MarkDeclarationReferenced(Loc, D);
467 :
468 : return Owned(DeclRefExpr::Create(Context,
469 : SS? (NestedNameSpecifier *)SS->getScopeRep() : 0,
470 : SS? SS->getRange() : SourceRange(),
27976: branch 0 taken
11: branch 1 taken
27976: branch 4 taken
11: branch 5 taken
471 27987: D, Loc, Ty));
472 : }
473 :
474 : /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or
475 : /// variable corresponding to the anonymous union or struct whose type
476 : /// is Record.
477 : static Decl *getObjectForAnonymousRecordDecl(ASTContext &Context,
478 82: RecordDecl *Record) {
479 : assert(Record->isAnonymousStructOrUnion() &&
82: branch 1 taken
0: branch 2 not taken
480 82: "Record must be an anonymous struct or union!");
481 :
482 : // FIXME: Once Decls are directly linked together, this will be an O(1)
483 : // operation rather than a slow walk through DeclContext's vector (which
484 : // itself will be eliminated). DeclGroups might make this even better.
485 82: DeclContext *Ctx = Record->getDeclContext();
368: branch 3 taken
0: branch 4 not taken
486 450: for (DeclContext::decl_iterator D = Ctx->decls_begin(),
487 82: DEnd = Ctx->decls_end();
488 : D != DEnd; ++D) {
82: branch 1 taken
286: branch 2 taken
489 368: if (*D == Record) {
490 : // The object for the anonymous struct/union directly
491 : // follows its type in the list of declarations.
492 82: ++D;
82: branch 1 taken
0: branch 2 not taken
493 82: assert(D != DEnd && "Missing object for anonymous record");
82: branch 4 taken
0: branch 5 not taken
494 82: assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed");
495 82: return *D;
496 : }
497 : }
498 :
499 0: assert(false && "Missing object for anonymous record");
500 : return 0;
501 : }
502 :
503 : /// \brief Given a field that represents a member of an anonymous
504 : /// struct/union, build the path from that field's context to the
505 : /// actual member.
506 : ///
507 : /// Construct the sequence of field member references we'll have to
508 : /// perform to get to the field in the anonymous union/struct. The
509 : /// list of members is built from the field outward, so traverse it
510 : /// backwards to go from an object in the current context to the field
511 : /// we found.
512 : ///
513 : /// \returns The variable from which the field access should begin,
514 : /// for an anonymous struct/union that is not a member of another
515 : /// class. Otherwise, returns NULL.
516 : VarDecl *Sema::BuildAnonymousStructUnionMemberPath(FieldDecl *Field,
517 69: llvm::SmallVectorImpl<FieldDecl *> &Path) {
518 : assert(Field->getDeclContext()->isRecord() &&
519 : cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion()
69: branch 2 taken
0: branch 3 not taken
69: branch 7 taken
0: branch 8 not taken
520 69: && "Field must be stored inside an anonymous struct or union");
521 :
522 69: Path.push_back(Field);
523 69: VarDecl *BaseObject = 0;
524 69: DeclContext *Ctx = Field->getDeclContext();
78: branch 1 taken
0: branch 2 not taken
13: branch 5 taken
65: branch 6 taken
13: branch 7 taken
65: branch 8 taken
525 156: do {
526 82: RecordDecl *Record = cast<RecordDecl>(Ctx);
527 82: Decl *AnonObject = getObjectForAnonymousRecordDecl(Context, Record);
78: branch 1 taken
4: branch 2 taken
528 82: if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject))
529 78: Path.push_back(AnonField);
530 : else {
531 4: BaseObject = cast<VarDecl>(AnonObject);
532 4: break;
533 : }
534 78: Ctx = Ctx->getParent();
535 : } while (Ctx->isRecord() &&
536 : cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion());
537 :
538 69: return BaseObject;
539 : }
540 :
541 : Sema::OwningExprResult
542 : Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc,
543 : FieldDecl *Field,
544 : Expr *BaseObjectExpr,
545 66: SourceLocation OpLoc) {
546 66: llvm::SmallVector<FieldDecl *, 4> AnonFields;
547 : VarDecl *BaseObject = BuildAnonymousStructUnionMemberPath(Field,
548 66: AnonFields);
549 :
550 : // Build the expression that refers to the base object, from
551 : // which we will build a sequence of member references to each
552 : // of the anonymous union objects and, eventually, the field we
553 : // found via name lookup.
554 66: bool BaseObjectIsPointer = false;
555 66: Qualifiers BaseQuals;
4: branch 0 taken
62: branch 1 taken
556 66: if (BaseObject) {
557 : // BaseObject is an anonymous struct/union variable (and is,
558 : // therefore, not part of another non-anonymous record).
0: branch 0 not taken
4: branch 1 taken
559 4: if (BaseObjectExpr) BaseObjectExpr->Destroy(Context);
560 4: MarkDeclarationReferenced(Loc, BaseObject);
561 : BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(),
4: branch 3 taken
0: branch 4 not taken
562 4: SourceLocation());
563 : BaseQuals
564 4: = Context.getCanonicalType(BaseObject->getType()).getQualifiers();
32: branch 0 taken
30: branch 1 taken
565 62: } else if (BaseObjectExpr) {
566 : // The caller provided the base object expression. Determine
567 : // whether its a pointer and whether it adds any qualifiers to the
568 : // anonymous struct/union fields we're looking into.
569 32: QualType ObjectType = BaseObjectExpr->getType();
6: branch 2 taken
26: branch 3 taken
570 32: if (const PointerType *ObjectPtr = ObjectType->getAs<PointerType>()) {
571 6: BaseObjectIsPointer = true;
572 6: ObjectType = ObjectPtr->getPointeeType();
573 : }
574 : BaseQuals
575 32: = Context.getCanonicalType(ObjectType).getQualifiers();
576 : } else {
577 : // We've found a member of an anonymous struct/union that is
578 : // inside a non-anonymous struct/union, so in a well-formed
579 : // program our base object expression is "this".
30: branch 1 taken
0: branch 2 not taken
580 30: if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) {
30: branch 1 taken
0: branch 2 not taken
581 30: if (!MD->isStatic()) {
582 : QualType AnonFieldType
583 : = Context.getTagDeclType(
584 30: cast<RecordDecl>(AnonFields.back()->getDeclContext()));
585 30: QualType ThisType = Context.getTagDeclType(MD->getParent());
3: branch 3 taken
27: branch 4 taken
3: branch 6 taken
0: branch 7 not taken
30: branch 8 taken
0: branch 9 not taken
586 30: if ((Context.getCanonicalType(AnonFieldType)
587 : == Context.getCanonicalType(ThisType)) ||
588 : IsDerivedFrom(ThisType, AnonFieldType)) {
589 : // Our base object expression is "this".
590 : BaseObjectExpr = new (Context) CXXThisExpr(Loc,
591 : MD->getThisType(Context),
30: branch 2 taken
0: branch 3 not taken
592 30: /*isImplicit=*/true);
593 30: BaseObjectIsPointer = true;
594 : }
595 : } else {
596 : return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method)
597 0: << Field->getDeclName());
598 : }
599 30: BaseQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
600 : }
601 :
0: branch 0 not taken
30: branch 1 taken
602 30: if (!BaseObjectExpr)
603 : return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use)
604 0: << Field->getDeclName());
605 : }
606 :
607 : // Build the implicit member references to the field of the
608 : // anonymous struct/union.
609 66: Expr *Result = BaseObjectExpr;
610 66: Qualifiers ResultQuals = BaseQuals;
138: branch 2 taken
66: branch 3 taken
611 204: for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator
612 66: FI = AnonFields.rbegin(), FIEnd = AnonFields.rend();
613 : FI != FIEnd; ++FI) {
614 138: QualType MemberType = (*FI)->getType();
615 : Qualifiers MemberTypeQuals =
616 138: Context.getCanonicalType(MemberType).getQualifiers();
617 :
618 : // CVR attributes from the base are picked up by members,
619 : // except that 'mutable' members don't pick up 'const'.
4: branch 2 taken
134: branch 3 taken
620 138: if ((*FI)->isMutable())
621 4: ResultQuals.removeConst();
622 :
623 : // GC attributes are never picked up by members.
624 138: ResultQuals.removeObjCGCAttr();
625 :
626 : // TR 18037 does not allow fields to be declared with address spaces.
0: branch 1 not taken
138: branch 2 taken
627 138: assert(!MemberTypeQuals.hasAddressSpace());
628 :
629 138: Qualifiers NewQuals = ResultQuals + MemberTypeQuals;
24: branch 1 taken
114: branch 2 taken
630 138: if (NewQuals != MemberTypeQuals)
631 24: MemberType = Context.getQualifiedType(MemberType, NewQuals);
632 :
633 138: MarkDeclarationReferenced(Loc, *FI);
634 138: PerformObjectMemberConversion(Result, *FI);
635 : // FIXME: Might this end up being a qualified name?
636 : Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI,
138: branch 2 taken
0: branch 3 not taken
637 138: OpLoc, MemberType);
638 138: BaseObjectIsPointer = false;
639 138: ResultQuals = NewQuals;
640 : }
641 :
642 66: return Owned(Result);
643 : }
644 :
645 : /// Decomposes the given name into a DeclarationName, its location, and
646 : /// possibly a list of template arguments.
647 : ///
648 : /// If this produces template arguments, it is permitted to call
649 : /// DecomposeTemplateName.
650 : ///
651 : /// This actually loses a lot of source location information for
652 : /// non-standard name kinds; we should consider preserving that in
653 : /// some way.
654 : static void DecomposeUnqualifiedId(Sema &SemaRef,
655 : const UnqualifiedId &Id,
656 : TemplateArgumentListInfo &Buffer,
657 : DeclarationName &Name,
658 : SourceLocation &NameLoc,
659 31706: const TemplateArgumentListInfo *&TemplateArgs) {
158: branch 1 taken
31548: branch 2 taken
660 31706: if (Id.getKind() == UnqualifiedId::IK_TemplateId) {
661 158: Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc);
662 158: Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc);
663 :
664 : ASTTemplateArgsPtr TemplateArgsPtr(SemaRef,
665 : Id.TemplateId->getTemplateArgs(),
666 158: Id.TemplateId->NumArgs);
667 158: SemaRef.translateTemplateArguments(TemplateArgsPtr, Buffer);
668 158: TemplateArgsPtr.release();
669 :
670 : TemplateName TName =
671 158: Sema::TemplateTy::make(Id.TemplateId->Template).getAsVal<TemplateName>();
672 :
673 158: Name = SemaRef.Context.getNameForTemplate(TName);
674 158: NameLoc = Id.TemplateId->TemplateNameLoc;
675 158: TemplateArgs = &Buffer;
676 : } else {
677 31548: Name = SemaRef.GetNameFromUnqualifiedId(Id);
678 31548: NameLoc = Id.StartLocation;
679 31548: TemplateArgs = 0;
680 : }
681 31706: }
682 :
683 : /// Decompose the given template name into a list of lookup results.
684 : ///
685 : /// The unqualified ID must name a non-dependent template, which can
686 : /// be more easily tested by checking whether DecomposeUnqualifiedId
687 : /// found template arguments.
688 150: static void DecomposeTemplateName(LookupResult &R, const UnqualifiedId &Id) {
0: branch 1 not taken
150: branch 2 taken
689 150: assert(Id.getKind() == UnqualifiedId::IK_TemplateId);
690 : TemplateName TName =
691 150: Sema::TemplateTy::make(Id.TemplateId->Template).getAsVal<TemplateName>();
692 :
130: branch 1 taken
20: branch 2 taken
693 150: if (TemplateDecl *TD = TName.getAsTemplateDecl())
694 130: R.addDecl(TD);
20: branch 1 taken
0: branch 2 not taken
695 20: else if (OverloadedTemplateStorage *OT = TName.getAsOverloadedTemplate())
48: branch 2 taken
20: branch 3 taken
696 68: for (OverloadedTemplateStorage::iterator I = OT->begin(), E = OT->end();
697 : I != E; ++I)
698 48: R.addDecl(*I);
699 :
700 150: R.resolveKind();
701 150: }
702 :
703 : /// Determines whether the given record is "fully-formed" at the given
704 : /// location, i.e. whether a qualified lookup into it is assured of
705 : /// getting consistent results already.
706 501: static bool IsFullyFormedScope(Sema &SemaRef, CXXRecordDecl *Record) {
1: branch 1 taken
500: branch 2 taken
707 501: if (!Record->hasDefinition())
708 1: return false;
709 :
99: branch 1 taken
499: branch 2 taken
710 1098: for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(),
711 500: E = Record->bases_end(); I != E; ++I) {
712 99: CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType());
713 99: CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>();
1: branch 1 taken
98: branch 2 taken
714 99: if (!BaseRT) return false;
715 :
716 98: CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl());
98: branch 1 taken
0: branch 2 not taken
0: branch 4 not taken
98: branch 5 taken
0: branch 6 not taken
98: branch 7 taken
717 98: if (!BaseRecord->hasDefinition() ||
718 : !IsFullyFormedScope(SemaRef, BaseRecord))
719 0: return false;
720 : }
721 :
722 499: return true;
723 : }
724 :
725 : /// Determines whether we can lookup this id-expression now or whether
726 : /// we have to wait until template instantiation is complete.
727 516: static bool IsDependentIdExpression(Sema &SemaRef, const CXXScopeSpec &SS) {
728 516: DeclContext *DC = SemaRef.computeDeclContext(SS, false);
729 :
730 : // If the qualifier scope isn't computable, it's definitely dependent.
50: branch 0 taken
466: branch 1 taken
731 516: if (!DC) return true;
732 :
733 : // If the qualifier scope doesn't name a record, we can always look into it.
63: branch 1 taken
403: branch 2 taken
734 466: if (!isa<CXXRecordDecl>(DC)) return false;
735 :
736 : // We can't look into record types unless they're fully-formed.
2: branch 2 taken
401: branch 3 taken
737 403: if (!IsFullyFormedScope(SemaRef, cast<CXXRecordDecl>(DC))) return true;
738 :
739 401: return false;
740 : }
741 :
742 : /// Determines if the given class is provably not derived from all of
743 : /// the prospective base classes.
744 : static bool IsProvablyNotDerivedFrom(Sema &SemaRef,
745 : CXXRecordDecl *Record,
746 1211: const llvm::SmallPtrSet<CXXRecordDecl*, 4> &Bases) {
976: branch 2 taken
235: branch 3 taken
747 1211: if (Bases.count(Record->getCanonicalDecl()))
748 976: return false;
749 :
750 235: RecordDecl *RD = Record->getDefinition(SemaRef.Context);
0: branch 0 not taken
235: branch 1 taken
751 235: if (!RD) return false;
752 235: Record = cast<CXXRecordDecl>(RD);
753 :
225: branch 1 taken
66: branch 2 taken
754 526: for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(),
755 235: E = Record->bases_end(); I != E; ++I) {
756 225: CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType());
757 225: CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>();
2: branch 1 taken
223: branch 2 taken
758 225: if (!BaseRT) return false;
759 :
760 223: CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl());
167: branch 1 taken
56: branch 2 taken
761 223: if (!IsProvablyNotDerivedFrom(SemaRef, BaseRecord, Bases))
762 167: return false;
763 : }
764 :
765 66: return true;
766 : }
767 :
768 : /// Determines if this is an instance member of a class.
769 907: static bool IsInstanceMember(NamedDecl *D) {
770 : assert(D->isCXXClassMember() &&
907: branch 1 taken
0: branch 2 not taken
771 907: "checking whether non-member is instance member");
772 :
386: branch 1 taken
521: branch 2 taken
773 1293: if (isa<FieldDecl>(D)) return true;
774 :
206: branch 1 taken
315: branch 2 taken
775 521: if (isa<CXXMethodDecl>(D))
776 206: return !cast<CXXMethodDecl>(D)->isStatic();
777 :
21: branch 1 taken
294: branch 2 taken
778 315: if (isa<FunctionTemplateDecl>(D)) {
779 21: D = cast<FunctionTemplateDecl>(D)->getTemplatedDecl();
780 21: return !cast<CXXMethodDecl>(D)->isStatic();
781 : }
782 :
783 294: return false;
784 : }
785 :
786 : enum IMAKind {
787 : /// The reference is definitely not an instance member access.
788 : IMA_Static,
789 :
790 : /// The reference may be an implicit instance member access.
791 : IMA_Mixed,
792 :
793 : /// The reference may be to an instance member, but it is invalid if
794 : /// so, because the context is not an instance method.
795 : IMA_Mixed_StaticContext,
796 :
797 : /// The reference may be to an instance member, but it is invalid if
798 : /// so, because the context is from an unrelated class.
799 : IMA_Mixed_Unrelated,
800 :
801 : /// The reference is definitely an implicit instance member access.
802 : IMA_Instance,
803 :
804 : /// The reference may be to an unresolved using declaration.
805 : IMA_Unresolved,
806 :
807 : /// The reference may be to an unresolved using declaration and the
808 : /// context is not an instance method.
809 : IMA_Unresolved_StaticContext,
810 :
811 : /// The reference is to a member of an anonymous structure in a
812 : /// non-class context.
813 : IMA_AnonymousMember,
814 :
815 : /// All possible referrents are instance members and the current
816 : /// context is not an instance method.
817 : IMA_Error_StaticContext,
818 :
819 : /// All possible referrents are instance members of an unrelated
820 : /// class.
821 : IMA_Error_Unrelated
822 : };
823 :
824 : /// The given lookup names class member(s) and is not being used for
825 : /// an address-of-member expression. Classify the type of access
826 : /// according to whether it's possible that this reference names an
827 : /// instance member. This is best-effort; it is okay to
828 : /// conservatively answer "yes", in which case some errors will simply
829 : /// not be caught until template-instantiation.
830 : static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef,
831 802: const LookupResult &R) {
802: branch 1 taken
0: branch 2 not taken
0: branch 6 not taken
802: branch 7 taken
832 802: assert(!R.empty() && (*R.begin())->isCXXClassMember());
833 :
834 : bool isStaticContext =
835 : (!isa<CXXMethodDecl>(SemaRef.CurContext) ||
539: branch 1 taken
263: branch 2 taken
8: branch 5 taken
531: branch 6 taken
836 802: cast<CXXMethodDecl>(SemaRef.CurContext)->isStatic());
837 :
5: branch 1 taken
797: branch 2 taken
838 802: if (R.isUnresolvableResult())
1: branch 0 taken
4: branch 1 taken
839 5: return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved;
840 :
841 : // Collect all the declaring classes of instance members we find.
842 797: bool hasNonInstance = false;
843 797: llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes;
873: branch 4 taken
793: branch 5 taken
844 1666: for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
845 873: NamedDecl *D = (*I)->getUnderlyingDecl();
543: branch 1 taken
330: branch 2 taken
846 873: if (IsInstanceMember(D)) {
847 543: CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext());
848 :
849 : // If this is a member of an anonymous record, move out to the
850 : // innermost non-anonymous struct or union. If there isn't one,
851 : // that's a special case.
38: branch 1 taken
539: branch 2 taken
852 1120: while (R->isAnonymousStructOrUnion()) {
853 38: R = dyn_cast<CXXRecordDecl>(R->getParent());
4: branch 0 taken
34: branch 1 taken
854 38: if (!R) return IMA_AnonymousMember;
855 : }
856 539: Classes.insert(R->getCanonicalDecl());
857 : }
858 : else
859 330: hasNonInstance = true;
860 : }
861 :
862 : // If we didn't find any instance members, it can't be an implicit
863 : // member reference.
315: branch 1 taken
478: branch 2 taken
864 793: if (Classes.empty())
865 315: return IMA_Static;
866 :
867 : // If the current context is not an instance method, it can't be
868 : // an implicit member reference.
12: branch 0 taken
466: branch 1 taken
869 478: if (isStaticContext)
4: branch 0 taken
8: branch 1 taken
870 12: return (hasNonInstance ? IMA_Mixed_StaticContext : IMA_Error_StaticContext);
871 :
872 : // If we can prove that the current context is unrelated to all the
873 : // declaring classes, it can't be an implicit member reference (in
874 : // which case it's an error if any of those members are selected).
3: branch 3 taken
463: branch 4 taken
875 466: if (IsProvablyNotDerivedFrom(SemaRef,
876 : cast<CXXMethodDecl>(SemaRef.CurContext)->getParent(),
877 : Classes))
1: branch 0 taken
2: branch 1 taken
878 3: return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated);
879 :
6: branch 0 taken
457: branch 1 taken
880 463: return (hasNonInstance ? IMA_Mixed : IMA_Instance);
881 : }
882 :
883 : /// Diagnose a reference to a field with no object available.
884 : static void DiagnoseInstanceReference(Sema &SemaRef,
885 : const CXXScopeSpec &SS,
886 12: const LookupResult &R) {
887 12: SourceLocation Loc = R.getNameLoc();
888 12: SourceRange Range(Loc);
4: branch 1 taken
8: branch 2 taken
889 12: if (SS.isSet()) Range.setBegin(SS.getRange().getBegin());
890 :
8: branch 1 taken
4: branch 2 taken
891 12: if (R.getAsSingle<FieldDecl>()) {
3: branch 1 taken
5: branch 2 taken
892 8: if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) {
2: branch 1 taken
1: branch 2 taken
893 3: if (MD->isStatic()) {
894 : // "invalid use of member 'x' in static member function"
895 : SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method)
896 2: << Range << R.getLookupName();
897 2: return;
898 : }
899 : }
900 :
901 : SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use)
902 6: << R.getLookupName() << Range;
903 6: return;
904 : }
905 :
906 4: SemaRef.Diag(Loc, diag::err_member_call_without_object) << Range;
907 : }
908 :
909 : /// Diagnose an empty lookup.
910 : ///
911 : /// \return false if new lookup candidates were found
912 : bool Sema::DiagnoseEmptyLookup(Scope *S, const CXXScopeSpec &SS,
913 66: LookupResult &R) {
914 66: DeclarationName Name = R.getLookupName();
915 :
916 66: unsigned diagnostic = diag::err_undeclared_var_use;
917 66: unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest;
66: branch 1 taken
0: branch 2 not taken
66: branch 4 taken
0: branch 5 not taken
1: branch 7 taken
65: branch 8 taken
1: branch 9 taken
65: branch 10 taken
918 66: if (Name.getNameKind() == DeclarationName::CXXOperatorName ||
919 : Name.getNameKind() == DeclarationName::CXXLiteralOperatorName ||
920 : Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
921 1: diagnostic = diag::err_undeclared_use;
922 1: diagnostic_suggest = diag::err_undeclared_use_suggest;
923 : }
924 :
925 : // If the original lookup was an unqualified lookup, fake an
926 : // unqualified lookup. This is useful when (for example) the
927 : // original lookup would not have found something because it was a
928 : // dependent name.
56: branch 1 taken
10: branch 2 taken
123: branch 4 taken
65: branch 5 taken
929 188: for (DeclContext *DC = SS.isEmpty()? CurContext : 0;
930 : DC; DC = DC->getParent()) {
10: branch 1 taken
113: branch 2 taken
931 123: if (isa<CXXRecordDecl>(DC)) {
932 10: LookupQualifiedName(R, DC);
933 :
1: branch 1 taken
9: branch 2 taken
934 10: if (!R.empty()) {
935 : // Don't give errors about ambiguities in this lookup.
936 1: R.suppressDiagnostics();
937 :
938 1: CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext);
939 : bool isInstance = CurMethod &&
940 : CurMethod->isInstance() &&
1: branch 0 taken
0: branch 1 not taken
1: branch 3 taken
0: branch 4 not taken
1: branch 6 taken
0: branch 7 not taken
1: branch 8 taken
0: branch 9 not taken
941 1: DC == CurMethod->getParent();
942 :
943 : // Give a code modification hint to insert 'this->'.
944 : // TODO: fixit for inserting 'Base<T>::' in the other cases.
945 : // Actually quite difficult!
1: branch 0 taken
0: branch 1 not taken
946 1: if (isInstance)
947 : Diag(R.getNameLoc(), diagnostic) << Name
948 : << CodeModificationHint::CreateInsertion(R.getNameLoc(),
949 1: "this->");
950 : else
951 0: Diag(R.getNameLoc(), diagnostic) << Name;
952 :
953 : // Do we really want to note all of these?
1: branch 4 taken
1: branch 5 taken
954 2: for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I)
955 1: Diag((*I)->getLocation(), diag::note_dependent_var_use);
956 :
957 : // Tell the callee to try to recover.
958 1: return false;
959 : }
960 : }
961 : }
962 :
963 : // We didn't find anything, so try to correct for a typo.
58: branch 0 taken
7: branch 1 taken
19: branch 3 taken
39: branch 4 taken
19: branch 5 taken
46: branch 6 taken
964 65: if (S && CorrectTypo(R, S, &SS)) {
5: branch 3 taken
14: branch 4 taken
0: branch 8 not taken
5: branch 9 taken
14: branch 10 taken
5: branch 11 taken
965 19: if (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin())) {
12: branch 1 taken
2: branch 2 taken
966 14: if (SS.isEmpty())
967 : Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName()
968 : << CodeModificationHint::CreateReplacement(R.getNameLoc(),
969 12: R.getLookupName().getAsString());
970 : else
971 : Diag(R.getNameLoc(), diag::err_no_member_suggest)
972 : << Name << computeDeclContext(SS, false) << R.getLookupName()
973 : << SS.getRange()
974 : << CodeModificationHint::CreateReplacement(R.getNameLoc(),
975 2: R.getLookupName().getAsString());
14: branch 1 taken
0: branch 2 not taken
976 14: if (NamedDecl *ND = R.getAsSingle<NamedDecl>())
977 : Diag(ND->getLocation(), diag::note_previous_decl)
978 14: << ND->getDeclName();
979 :
980 : // Tell the callee to try to recover.
981 14: return false;
982 : }
983 :
5: branch 3 taken
0: branch 4 not taken
5: branch 8 taken
0: branch 9 not taken
5: branch 10 taken
0: branch 11 not taken
984 5: if (isa<TypeDecl>(*R.begin()) || isa<ObjCInterfaceDecl>(*R.begin())) {
985 : // FIXME: If we ended up with a typo for a type name or
986 : // Objective-C class name, we're in trouble because the parser
987 : // is in the wrong place to recover. Suggest the typo
988 : // correction, but don't make it a fix-it since we're not going
989 : // to recover well anyway.
5: branch 1 taken
0: branch 2 not taken
990 5: if (SS.isEmpty())
991 5: Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName();
992 : else
993 : Diag(R.getNameLoc(), diag::err_no_member_suggest)
994 : << Name << computeDeclContext(SS, false) << R.getLookupName()
995 0: << SS.getRange();
996 :
997 : // Don't try to recover; it won't work.
998 5: return true;
999 : }
1000 :
1001 0: R.clear();
1002 : }
1003 :
1004 : // Emit a special diagnostic for failed member lookups.
1005 : // FIXME: computing the declaration context might fail here (?)
8: branch 1 taken
38: branch 2 taken
1006 46: if (!SS.isEmpty()) {
1007 : Diag(R.getNameLoc(), diag::err_no_member)
1008 : << Name << computeDeclContext(SS, false)
1009 8: << SS.getRange();
1010 8: return true;
1011 : }
1012 :
1013 : // Give up, we can't recover.
1014 38: Diag(R.getNameLoc(), diagnostic) << Name;
1015 38: return true;
1016 : }
1017 :
1018 : Sema::OwningExprResult Sema::ActOnIdExpression(Scope *S,
1019 : const CXXScopeSpec &SS,
1020 : UnqualifiedId &Id,
1021 : bool HasTrailingLParen,
1022 29564: bool isAddressOfOperand) {
1023 : assert(!(isAddressOfOperand && HasTrailingLParen) &&
124: branch 0 taken
29440: branch 1 taken
0: branch 2 not taken
124: branch 3 taken
1024 29564: "cannot be direct & operand and have a trailing lparen");
1025 :
8: branch 1 taken
29556: branch 2 taken
1026 29564: if (SS.isInvalid())
1027 8: return ExprError();
1028 :
1029 29556: TemplateArgumentListInfo TemplateArgsBuffer;
1030 :
1031 : // Decompose the UnqualifiedId into the following data.
1032 29556: DeclarationName Name;
1033 29556: SourceLocation NameLoc;
1034 : const TemplateArgumentListInfo *TemplateArgs;
1035 : DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer,
1036 29556: Name, NameLoc, TemplateArgs);
1037 :
1038 29556: IdentifierInfo *II = Name.getAsIdentifierInfo();
1039 :
1040 : // C++ [temp.dep.expr]p3:
1041 : // An id-expression is type-dependent if it contains:
1042 : // -- an identifier that was declared with a dependent type,
1043 : // (note: handled after lookup)
1044 : // -- a template-id that is dependent,
1045 : // (note: handled in BuildTemplateIdExpr)
1046 : // -- a conversion-function-id that specifies a dependent type,
1047 : // -- a nested-name-specifier that contains a class-name that
1048 : // names a dependent type.
1049 : // Determine whether this is a member of an unknown specialization;
1050 : // we need to handle these differently.
4: branch 1 taken
29552: branch 2 taken
3: branch 6 taken
1: branch 7 taken
516: branch 9 taken
29039: branch 10 taken
52: branch 12 taken
464: branch 13 taken
53: branch 14 taken
29503: branch 15 taken
1051 29556: if ((Name.getNameKind() == DeclarationName::CXXConversionFunctionName &&
1052 : Name.getCXXNameType()->isDependentType()) ||
1053 : (SS.isSet() && IsDependentIdExpression(*this, SS))) {
1054 : return ActOnDependentIdExpression(SS, Name, NameLoc,
1055 : isAddressOfOperand,
1056 53: TemplateArgs);
1057 : }
1058 :
1059 : // Perform the required lookup.
1060 29503: LookupResult R(*this, Name, NameLoc, LookupOrdinaryName);
129: branch 0 taken
29374: branch 1 taken
1061 29503: if (TemplateArgs) {
1062 : // Just re-use the lookup done by isTemplateName.
1063 129: DecomposeTemplateName(R, Id);
1064 : } else {
28920: branch 1 taken
454: branch 2 taken
28910: branch 3 taken
10: branch 4 taken
2122: branch 6 taken
26788: branch 7 taken
1065 29374: bool IvarLookupFollowUp = (!SS.isSet() && II && getCurMethodDecl());
1066 29374: LookupParsedName(R, S, &SS, !IvarLookupFollowUp);
1067 :
1068 : // If this reference is in an Objective-C method, then we need to do
1069 : // some special Objective-C lookup, too.
2122: branch 0 taken
27252: branch 1 taken
1070 29374: if (IvarLookupFollowUp) {
1071 2122: OwningExprResult E(LookupInObjCMethod(R, S, II, true));
3: branch 1 taken
2119: branch 2 taken
1072 2122: if (E.isInvalid())
1073 3: return ExprError();
1074 :
1075 2119: Expr *Ex = E.takeAs<Expr>();
367: branch 0 taken
1752: branch 1 taken
1752: branch 7 taken
370: branch 8 taken
1076 2119: if (Ex) return Owned(Ex);
1077 : }
1078 : }
1079 :
13: branch 1 taken
29120: branch 2 taken
1080 29133: if (R.isAmbiguous())
1081 13: return ExprError();
1082 :
1083 : // Determine whether this name might be a candidate for
1084 : // argument-dependent lookup.
1085 29120: bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen);
1086 :
109: branch 1 taken
29011: branch 2 taken
90: branch 3 taken
19: branch 4 taken
90: branch 5 taken
29030: branch 6 taken
1087 29120: if (R.empty() && !ADL) {
1088 : // Otherwise, this could be an implicitly declared function reference (legal
1089 : // in C90, extension in C99, forbidden in C++).
32: branch 0 taken
58: branch 1 taken
32: branch 2 taken
0: branch 3 not taken
32: branch 5 taken
0: branch 6 not taken
32: branch 7 taken
58: branch 8 taken
1090 90: if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) {
1091 32: NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S);
32: branch 0 taken
0: branch 1 not taken
1092 32: if (D) R.addDecl(D);
1093 : }
1094 :
1095 : // If this name wasn't predeclared and if this is not a function
1096 : // call, diagnose the problem.
58: branch 1 taken
32: branch 2 taken
1097 90: if (R.empty()) {
44: branch 1 taken
14: branch 2 taken
1098 58: if (DiagnoseEmptyLookup(S, SS, R))
1099 44: return ExprError();
1100 :
1101 : assert(!R.empty() &&
14: branch 1 taken
0: branch 2 not taken
1102 14: "DiagnoseEmptyLookup returned false but added no results");
1103 :
1104 : // If we found an Objective-C instance variable, let
1105 : // LookupInObjCMethod build the appropriate expression to
1106 : // reference the ivar.
4: branch 1 taken
10: branch 2 taken
1107 14: if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) {
1108 4: R.clear();
1109 4: OwningExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier()));
4: branch 1 taken
0: branch 2 not taken
0: branch 4 not taken
4: branch 5 taken
1110 4: assert(E.isInvalid() || E.get());
1111 4: return move(E);
1112 : }
1113 : }
1114 : }
1115 :
1116 : // This is guaranteed from this point on.
19: branch 1 taken
29053: branch 2 taken
0: branch 3 not taken
19: branch 4 taken
1117 29072: assert(!R.empty() || ADL);
1118 :
22021: branch 1 taken
7051: branch 2 taken
1119 29072: if (VarDecl *Var = R.getAsSingle<VarDecl>()) {
1120 : // Warn about constructs like:
1121 : // if (void *X = foo()) { ... } else { X }.
1122 : // In the else block, the pointer is always false.
1123 :
28: branch 1 taken
21993: branch 2 taken
27: branch 6 taken
1: branch 7 taken
27: branch 8 taken
21994: branch 9 taken
1124 22021: if (Var->isDeclaredInCondition() && Var->getType()->isScalarType()) {
1125 27: Scope *CheckS = S;
89: branch 0 taken
0: branch 1 not taken
74: branch 3 taken
15: branch 4 taken
74: branch 5 taken
15: branch 6 taken
1126 116: while (CheckS && CheckS->getControlParent()) {
16: branch 1 taken
58: branch 2 taken
12: branch 6 taken
4: branch 7 taken
12: branch 8 taken
62: branch 9 taken
1127 74: if (CheckS->isWithinElse() &&
1128 : CheckS->getControlParent()->isDeclScope(DeclPtrTy::make(Var))) {
1129 : ExprError(Diag(NameLoc, diag::warn_value_always_zero)
1130 : << Var->getDeclName()
1131 : << (Var->getType()->isPointerType()? 2 :
3: branch 3 taken
9: branch 4 taken
6: branch 8 taken
3: branch 9 taken
1132 12: Var->getType()->isBooleanType()? 1 : 0));
1133 12: break;
1134 : }
1135 :
1136 : // Move to the parent of this scope.
1137 62: CheckS = CheckS->getParent();
1138 : }
1139 : }
5428: branch 1 taken
1623: branch 2 taken
1140 7051: } else if (FunctionDecl *Func = R.getAsSingle<FunctionDecl>()) {
4176: branch 1 taken
1252: branch 2 taken
643: branch 4 taken
3533: branch 5 taken
643: branch 6 taken
4785: branch 7 taken
1141 5428: if (!getLangOptions().CPlusPlus && !Func->hasPrototype()) {
1142 : // C99 DR 316 says that, if a function type comes from a
1143 : // function definition (without a prototype), that type is only
1144 : // used for checking compatibility. Therefore, when referencing
1145 : // the function, we pretend that we don't have the full function
1146 : // type.
0: branch 1 not taken
643: branch 2 taken
1147 643: if (DiagnoseUseOfDecl(Func, NameLoc))
1148 0: return ExprError();
1149 :
1150 643: QualType T = Func->getType();
1151 643: QualType NoProtoType = T;
9: branch 2 taken
634: branch 3 taken
1152 643: if (const FunctionProtoType *Proto = T->getAs<FunctionProtoType>())
1153 9: NoProtoType = Context.getFunctionNoProtoType(Proto->getResultType());
1154 643: return BuildDeclRefExpr(Func, NoProtoType, NameLoc, &SS);
1155 : }
1156 : }
1157 :
1158 : // Check whether this might be a C++ implicit instance member access.
1159 : // C++ [expr.prim.general]p6:
1160 : // Within the definition of a non-static member function, an
1161 : // identifier that names a non-static member is transformed to a
1162 : // class member access expression.
1163 : // But note that &SomeClass::foo is grammatically distinct, even
1164 : // though we don't parse it that way.
28410: branch 1 taken
19: branch 2 taken
904: branch 6 taken
27506: branch 7 taken
904: branch 8 taken
27525: branch 9 taken
1165 28429: if (!R.empty() && (*R.begin())->isCXXClassMember()) {
103: branch 0 taken
801: branch 1 taken
103: branch 3 taken
0: branch 4 not taken
1166 904: bool isAbstractMemberPointer = (isAddressOfOperand && !SS.isEmpty());
801: branch 0 taken
103: branch 1 taken
1167 904: if (!isAbstractMemberPointer)
1168 801: return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs);
1169 : }
1170 :
122: branch 0 taken
27506: branch 1 taken
1171 27628: if (TemplateArgs)
1172 122: return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs);
1173 :
1174 27506: return BuildDeclarationNameExpr(SS, R, ADL);
1175 : }
1176 :
1177 : /// Builds an expression which might be an implicit member expression.
1178 : Sema::OwningExprResult
1179 : Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS,
1180 : LookupResult &R,
1181 802: const TemplateArgumentListInfo *TemplateArgs) {
457: branch 1 taken
4: branch 2 taken
11: branch 3 taken
320: branch 4 taken
10: branch 5 taken
0: branch 6 not taken
1182 802: switch (ClassifyImplicitMemberAccess(*this, R)) {
1183 : case IMA_Instance:
1184 457: return BuildImplicitMemberExpr(SS, R, TemplateArgs, true);
1185 :
1186 : case IMA_AnonymousMember:
0: branch 1 not taken
4: branch 2 taken
1187 4: assert(R.isSingleResult());
1188 : return BuildAnonymousStructUnionMemberReference(R.getNameLoc(),
1189 4: R.getAsSingle<FieldDecl>());
1190 :
1191 : case IMA_Mixed:
1192 : case IMA_Mixed_Unrelated:
1193 : case IMA_Unresolved:
1194 11: return BuildImplicitMemberExpr(SS, R, TemplateArgs, false);
1195 :
1196 : case IMA_Static:
1197 : case IMA_Mixed_StaticContext:
1198 : case IMA_Unresolved_StaticContext:
2: branch 0 taken
318: branch 1 taken
1199 320: if (TemplateArgs)
1200 2: return BuildTemplateIdExpr(SS, R, false, *TemplateArgs);
1201 318: return BuildDeclarationNameExpr(SS, R, false);
1202 :
1203 : case IMA_Error_StaticContext:
1204 : case IMA_Error_Unrelated:
1205 10: DiagnoseInstanceReference(*this, SS, R);
1206 10: return ExprError();
1207 : }
1208 :
1209 0: llvm_unreachable("unexpected instance member access kind");
1210 : return ExprError();
1211 : }
1212 :
1213 : /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified
1214 : /// declaration name, generally during template instantiation.
1215 : /// There's a large number of things which don't need to be done along
1216 : /// this path.
1217 : Sema::OwningExprResult
1218 : Sema::BuildQualifiedDeclarationNameExpr(const CXXScopeSpec &SS,
1219 : DeclarationName Name,
1220 486: SourceLocation NameLoc) {
1221 : DeclContext *DC;
485: branch 1 taken
1: branch 2 taken
485: branch 4 taken
0: branch 5 not taken
0: branch 7 not taken
485: branch 8 taken
1: branch 9 taken
485: branch 10 taken
1222 486: if (!(DC = computeDeclContext(SS, false)) ||
1223 : DC->isDependentContext() ||
1224 : RequireCompleteDeclContext(SS))
1225 1: return BuildDependentDeclRefExpr(SS, Name, NameLoc, 0);
1226 :
1227 485: LookupResult R(*this, Name, NameLoc, LookupOrdinaryName);
1228 485: LookupQualifiedName(R, DC);
1229 :
0: branch 1 not taken
485: branch 2 taken
1230 485: if (R.isAmbiguous())
1231 0: return ExprError();
1232 :
2: branch 1 taken
483: branch 2 taken
1233 485: if (R.empty()) {
1234 2: Diag(NameLoc, diag::err_no_member) << Name << DC << SS.getRange();
1235 2: return ExprError();
1236 : }
1237 :
1238 483: return BuildDeclarationNameExpr(SS, R, /*ADL*/ false);
1239 : }
1240 :
1241 : /// LookupInObjCMethod - The parser has read a name in, and Sema has
1242 : /// detected that we're currently inside an ObjC method. Perform some
1243 : /// additional lookup.
1244 : ///
1245 : /// Ideally, most of this would be done by lookup, but there's
1246 : /// actually quite a lot of extra work involved.
1247 : ///
1248 : /// Returns a null sentinel to indicate trivial success.
1249 : Sema::OwningExprResult
1250 : Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S,
1251 : IdentifierInfo *II,
1252 2132: bool AllowBuiltinCreation) {
1253 2132: SourceLocation Loc = Lookup.getNameLoc();
1254 :
1255 : // There are two cases to handle here. 1) scoped lookup could have failed,
1256 : // in which case we should look for an ivar. 2) scoped lookup could have
1257 : // found a decl, but that decl is outside the current instance method (i.e.
1258 : // a global variable). In these two cases, we do a lookup for an ivar with
1259 : // this name, if the lookup sucedes, we replace it our current decl.
1260 :
1261 : // If we're in a class method, we don't normally want to look for
1262 : // ivars. But if we don't find anything else, and there's an
1263 : // ivar, that's an error.
1264 2132: bool IsClassMethod = getCurMethodDecl()->isClassMethod();
1265 :
1266 : bool LookForIvars;
394: branch 1 taken
1738: branch 2 taken
1267 2132: if (Lookup.empty())
1268 394: LookForIvars = true;
168: branch 0 taken
1570: branch 1 taken
1269 1738: else if (IsClassMethod)
1270 168: LookForIvars = false;
1271 : else
1272 : LookForIvars = (Lookup.isSingleResult() &&
1570: branch 1 taken
0: branch 2 not taken
249: branch 5 taken
1321: branch 6 taken
1273 1570: Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod());
1274 2132: ObjCInterfaceDecl *IFace = 0;
643: branch 0 taken
1489: branch 1 taken
1275 2132: if (LookForIvars) {
1276 643: IFace = getCurMethodDecl()->getClassInterface();
1277 : ObjCInterfaceDecl *ClassDeclared;
351: branch 1 taken
292: branch 2 taken
1278 643: if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) {
1279 : // Diagnose using an ivar in a class method.
2: branch 0 taken
349: branch 1 taken
1280 351: if (IsClassMethod)
1281 : return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method)
1282 2: << IV->getDeclName());
1283 :
1284 : // If we're referencing an invalid decl, just return this as a silent
1285 : // error node. The error diagnostic was already emitted on the decl.
1: branch 1 taken
348: branch 2 taken
1286 349: if (IV->isInvalidDecl())
1287 1: return ExprError();
1288 :
1289 : // Check if referencing a field with __attribute__((deprecated)).
0: branch 1 not taken
348: branch 2 taken
1290 348: if (DiagnoseUseOfDecl(IV, Loc))
1291 0: return ExprError();
1292 :
1293 : // Diagnose the use of an ivar outside of the declaring class.
11: branch 1 taken
337: branch 2 taken
0: branch 3 not taken
11: branch 4 taken
0: branch 5 not taken
348: branch 6 taken
1294 348: if (IV->getAccessControl() == ObjCIvarDecl::Private &&
1295 : ClassDeclared != IFace)
1296 0: Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName();
1297 :
1298 : // FIXME: This should use a new expr for a direct reference, don't
1299 : // turn this into Self->ivar, just return a BareIVarExpr or something.
1300 348: IdentifierInfo &II = Context.Idents.get("self");
1301 348: UnqualifiedId SelfName;
1302 348: SelfName.setIdentifier(&II, SourceLocation());
1303 348: CXXScopeSpec SelfScopeSpec;
1304 : OwningExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec,
1305 348: SelfName, false, false);
1306 348: MarkDeclarationReferenced(Loc, IV);
1307 : return Owned(new (Context)
1308 : ObjCIvarRefExpr(IV, IV->getType(), Loc,
348: branch 3 taken
0: branch 4 not taken
1309 348: SelfExpr.takeAs<Expr>(), true, true));
1310 : }
1321: branch 2 taken
168: branch 3 taken
1311 1489: } else if (getCurMethodDecl()->isInstanceMethod()) {
1312 : // We should warn if a local variable hides an ivar.
1313 1321: ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface();
1314 : ObjCInterfaceDecl *ClassDeclared;
2: branch 1 taken
1319: branch 2 taken
1315 1321: if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) {
0: branch 1 not taken
2: branch 2 taken
2: branch 3 taken
2: branch 4 taken
2: branch 5 taken
0: branch 6 not taken
1316 2: if (IV->getAccessControl() != ObjCIvarDecl::Private ||
1317 : IFace == ClassDeclared)
1318 2: Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName();
1319 : }
1320 : }
1321 :
1322 : // Needed to implement property "super.method" notation.
45: branch 1 taken
1736: branch 2 taken
23: branch 4 taken
22: branch 5 taken
23: branch 6 taken
1758: branch 7 taken
1323 1781: if (Lookup.empty() && II->isStr("super")) {
1324 23: QualType T;
1325 :
16: branch 2 taken
7: branch 3 taken
1326 23: if (getCurMethodDecl()->isInstanceMethod())
1327 : T = Context.getObjCObjectPointerType(Context.getObjCInterfaceType(
1328 16: getCurMethodDecl()->getClassInterface()));
1329 : else
1330 7: T = Context.getObjCClassType();
23: branch 1 taken
0: branch 2 not taken
1331 23: return Owned(new (Context) ObjCSuperExpr(Loc, T));
1332 : }
22: branch 1 taken
1736: branch 2 taken
22: branch 3 taken
0: branch 4 not taken
22: branch 5 taken
0: branch 6 not taken
22: branch 7 taken
1736: branch 8 taken
1333 1758: if (Lookup.empty() && II && AllowBuiltinCreation) {
1334 : // FIXME. Consolidate this with similar code in LookupName.
7: branch 1 taken
15: branch 2 taken
1335 22: if (unsigned BuiltinID = II->getBuiltinID()) {
0: branch 1 not taken
7: branch 2 taken
0: branch 4 not taken
0: branch 5 not taken
7: branch 6 taken
0: branch 7 not taken
1336 7: if (!(getLangOptions().CPlusPlus &&
1337 : Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) {
1338 : NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID,
1339 : S, Lookup.isForRedeclaration(),
1340 7: Lookup.getNameLoc());
7: branch 0 taken
0: branch 1 not taken
1341 7: if (D) Lookup.addDecl(D);
1342 : }
1343 : }
1344 : }
18: branch 0 taken
1740: branch 1 taken
6: branch 2 taken
12: branch 3 taken
6: branch 5 taken
0: branch 6 not taken
6: branch 7 taken
1752: branch 8 taken
1345 1758: if (LangOpts.ObjCNonFragileABI2 && LookForIvars && Lookup.empty()) {
1346 6: ObjCIvarDecl *Ivar = SynthesizeNewPropertyIvar(IFace, II);
6: branch 0 taken
0: branch 1 not taken
1347 6: if (Ivar)
1348 6: return LookupInObjCMethod(Lookup, S, II, AllowBuiltinCreation);
1349 : }
1350 : // Sentinel value saying that we didn't do anything special.
1351 1752: return Owned((Expr*) 0);
1352 : }
1353 :
1354 : /// \brief Cast member's object to its own class if necessary.
1355 : bool
1356 1672: Sema::PerformObjectMemberConversion(Expr *&From, NamedDecl *Member) {
1672: branch 1 taken
0: branch 2 not taken
1357 1672: if (FieldDecl *FD = dyn_cast<FieldDecl>(Member))
800: branch 0 taken
872: branch 1 taken
1358 1672: if (CXXRecordDecl *RD =
1359 1672: dyn_cast<CXXRecordDecl>(FD->getDeclContext())) {
1360 : QualType DestType =
1361 800: Context.getCanonicalType(Context.getTypeDeclType(RD));
738: branch 2 taken
62: branch 3 taken
5: branch 7 taken
733: branch 8 taken
67: branch 9 taken
733: branch 10 taken
1362 800: if (DestType->isDependentType() || From->getType()->isDependentType())
1363 67: return false;
1364 733: QualType FromRecordType = From->getType();
1365 733: QualType DestRecordType = DestType;
447: branch 2 taken
286: branch 3 taken
1366 733: if (FromRecordType->getAs<PointerType>()) {
1367 447: DestType = Context.getPointerType(DestType);
1368 447: FromRecordType = FromRecordType->getPointeeType();
1369 : }
74: branch 1 taken
659: branch 2 taken
7: branch 7 taken
67: branch 8 taken
7: branch 9 taken
726: branch 10 taken
1370 733: if (!Context.hasSameUnqualifiedType(FromRecordType, DestRecordType) &&
1371 : CheckDerivedToBaseConversion(FromRecordType,
1372 : DestRecordType,
1373 : From->getSourceRange().getBegin(),
1374 : From->getSourceRange()))
1375 7: return true;
1376 : ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase,
1377 726: /*isLvalue=*/true);
1378 : }
1379 1598: return false;
1380 : }
1381 :
1382 : /// \brief Build a MemberExpr AST node.
1383 : static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow,
1384 : const CXXScopeSpec &SS, ValueDecl *Member,
1385 : SourceLocation Loc, QualType Ty,
1386 1875: const TemplateArgumentListInfo *TemplateArgs = 0) {
1387 1875: NestedNameSpecifier *Qualifier = 0;
1388 1875: SourceRange QualifierRange;
84: branch 1 taken
1791: branch 2 taken
1389 1875: if (SS.isSet()) {
1390 84: Qualifier = (NestedNameSpecifier *) SS.getScopeRep();
1391 84: QualifierRange = SS.getRange();
1392 : }
1393 :
1394 : return MemberExpr::Create(C, Base, isArrow, Qualifier, QualifierRange,
1395 1875: Member, Loc, TemplateArgs, Ty);
1396 : }
1397 :
1398 : /// Builds an implicit member access expression. The current context
1399 : /// is known to be an instance method, and the given unqualified lookup
1400 : /// set is known to contain only instance members, at least one of which
1401 : /// is from an appropriate type.
1402 : Sema::OwningExprResult
1403 : Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS,
1404 : LookupResult &R,
1405 : const TemplateArgumentListInfo *TemplateArgs,
1406 468: bool IsKnownInstance) {
468: branch 1 taken
0: branch 2 not taken
0: branch 4 not taken
468: branch 5 taken
1407 468: assert(!R.empty() && !R.isAmbiguous());
1408 :
1409 468: SourceLocation Loc = R.getNameLoc();
1410 :
1411 : // We may have found a field within an anonymous union or struct
1412 : // (C++ [class.union]).
1413 : // FIXME: This needs to happen post-isImplicitMemberReference?
1414 : // FIXME: template-ids inside anonymous structs?
370: branch 1 taken
98: branch 2 taken
1415 468: if (FieldDecl *FD = R.getAsSingle<FieldDecl>())
30: branch 3 taken
340: branch 4 taken
1416 370: if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion())
1417 30: return BuildAnonymousStructUnionMemberReference(Loc, FD);
1418 :
1419 : // If this is known to be an instance access, go ahead and build a
1420 : // 'this' expression now.
1421 438: QualType ThisType = cast<CXXMethodDecl>(CurContext)->getThisType(Context);
1422 438: Expr *This = 0; // null signifies implicit access
427: branch 0 taken
11: branch 1 taken
1423 438: if (IsKnownInstance) {
1424 427: SourceLocation Loc = R.getNameLoc();
36: branch 2 taken
391: branch 3 taken
1425 427: if (SS.getRange().isValid())
1426 36: Loc = SS.getRange().getBegin();
427: branch 1 taken
0: branch 2 not taken
1427 427: This = new (Context) CXXThisExpr(Loc, ThisType, /*isImplicit=*/true);
1428 : }
1429 :
1430 : return BuildMemberReferenceExpr(ExprArg(*this, This), ThisType,
1431 : /*OpLoc*/ SourceLocation(),
1432 : /*IsArrow*/ true,
1433 : SS,
1434 : /*FirstQualifierInScope*/ 0,
1435 438: R, TemplateArgs);
1436 : }
1437 :
1438 : bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS,
1439 : const LookupResult &R,
1440 29120: bool HasTrailingLParen) {
1441 : // Only when used directly as the postfix-expression of a call.
23280: branch 0 taken
5840: branch 1 taken
1442 29120: if (!HasTrailingLParen)
1443 23280: return false;
1444 :
1445 : // Never if a scope specifier was provided.
66: branch 1 taken
5774: branch 2 taken
1446 5840: if (SS.isSet())
1447 66: return false;
1448 :
1449 : // Only in C++ or ObjC++.
4145: branch 1 taken
1629: branch 2 taken
1450 5774: if (!getLangOptions().CPlusPlus)
1451 4145: return false;
1452 :
1453 : // Turn off ADL when we find certain kinds of declarations during
1454 : // normal lookup:
1953: branch 4 taken
1225: branch 5 taken
1455 3178: for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
1456 1953: NamedDecl *D = *I;
1457 :
1458 : // C++0x [basic.lookup.argdep]p3:
1459 : // -- a declaration of a class member
1460 : // Since using decls preserve this property, we check this on the
1461 : // original decl.
84: branch 1 taken
1869: branch 2 taken
1462 1953: if (D->isCXXClassMember())
1463 84: return false;
1464 :
1465 : // C++0x [basic.lookup.argdep]p3:
1466 : // -- a block-scope function declaration that is not a
1467 : // using-declaration
1468 : // NOTE: we also trigger this for function templates (in fact, we
1469 : // don't check the decl type at all, since all other decl types
1470 : // turn off ADL anyway).
15: branch 1 taken
1854: branch 2 taken
1471 1869: if (isa<UsingShadowDecl>(D))
1472 15: D = cast<UsingShadowDecl>(D)->getTargetDecl();
67: branch 2 taken
1787: branch 3 taken
1473 1854: else if (D->getDeclContext()->isFunctionOrMethod())
1474 67: return false;
1475 :
1476 : // C++0x [basic.lookup.argdep]p3:
1477 : // -- a declaration that is neither a function or a function
1478 : // template
1479 : // And also for builtin functions.
1503: branch 1 taken
299: branch 2 taken
1480 1802: if (isa<FunctionDecl>(D)) {
1481 1503: FunctionDecl *FDecl = cast<FunctionDecl>(D);
1482 :
1483 : // But also builtin functions.
444: branch 1 taken
1059: branch 2 taken
245: branch 4 taken
199: branch 5 taken
245: branch 6 taken
1258: branch 7 taken
1484 1503: if (FDecl->getBuiltinID() && FDecl->isImplicit())
1485 245: return false;
8: branch 1 taken
291: branch 2 taken
1486 299: } else if (!isa<FunctionTemplateDecl>(D))
1487 8: return false;
1488 : }
1489 :
1490 1225: return true;
1491 : }
1492 :
1493 :
1494 : /// Diagnoses obvious problems with the use of the given declaration
1495 : /// as an expression. This is only actually called for lookups that
1496 : /// were not overloaded, and it doesn't promise that the declaration
1497 : /// will in fact be used.
1498 28336: static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) {
0: branch 1 not taken
28336: branch 2 taken
1499 28336: if (isa<TypedefDecl>(D)) {
1500 0: S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName();
1501 0: return true;
1502 : }
1503 :
0: branch 1 not taken
28336: branch 2 taken
1504 28336: if (isa<ObjCInterfaceDecl>(D)) {
1505 0: S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName();
1506 0: return true;
1507 : }
1508 :
1: branch 1 taken
28335: branch 2 taken
1509 28336: if (isa<NamespaceDecl>(D)) {
1510 1: S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName();
1511 1: return true;
1512 : }
1513 :
1514 28335: return false;
1515 : }
1516 :
1517 : Sema::OwningExprResult
1518 : Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS,
1519 : LookupResult &R,
1520 28400: bool NeedsADL) {
1521 : // If this is a single, fully-resolved result and we don't need ADL,
1522 : // just build an ordinary singleton decl ref.
27175: branch 0 taken
1225: branch 1 taken
27002: branch 3 taken
173: branch 4 taken
27001: branch 6 taken
1: branch 7 taken
27001: branch 8 taken
1399: branch 9 taken
1523 28400: if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>())
1524 27001: return BuildDeclarationNameExpr(SS, R.getNameLoc(), R.getFoundDecl());
1525 :
1526 : // We only need to check the declaration if there's exactly one
1527 : // result, because in the overloaded case the results can only be
1528 : // functions and function templates.
805: branch 1 taken
594: branch 2 taken
0: branch 6 not taken
805: branch 7 taken
0: branch 8 not taken
1399: branch 9 taken
1529 1399: if (R.isSingleResult() &&
1530 : CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl()))
1531 0: return ExprError();
1532 :
1533 : // Otherwise, just build an unresolved lookup expression. Suppress
1534 : // any lookup-related diagnostics; we'll hash these out later, when
1535 : // we've picked a target.
1536 1399: R.suppressDiagnostics();
1537 :
1538 : bool Dependent
1539 1399: = UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), 0);
1540 : UnresolvedLookupExpr *ULE
1541 : = UnresolvedLookupExpr::Create(Context, Dependent, R.getNamingClass(),
1542 : (NestedNameSpecifier*) SS.getScopeRep(),
1543 : SS.getRange(),
1544 : R.getLookupName(), R.getNameLoc(),
1545 1399: NeedsADL, R.isOverloadedResult());
1546 1399: ULE->addDecls(R.begin(), R.end());
1547 :
1548 1399: return Owned(ULE);
1549 : }
1550 :
1551 :
1552 : /// \brief Complete semantic analysis for a reference to the given declaration.
1553 : Sema::OwningExprResult
1554 : Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS,
1555 27531: SourceLocation Loc, NamedDecl *D) {
0: branch 0 not taken
27531: branch 1 taken
1556 27531: assert(D && "Cannot refer to a NULL declaration");
1557 : assert(!isa<FunctionTemplateDecl>(D) &&
27531: branch 1 taken
0: branch 2 not taken
1558 27531: "Cannot refer unambiguously to a function template");
1559 :
1: branch 1 taken
27530: branch 2 taken
1560 27531: if (CheckDeclInExpr(*this, Loc, D))
1561 1: return ExprError();
1562 :
1: branch 1 taken
27529: branch 2 taken
1563 27530: if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
1564 : // Specifically diagnose references to class templates that are missing
1565 : // a template argument list.
1566 : Diag(Loc, diag::err_template_decl_ref)
1567 1: << Template << SS.getRange();
1568 1: Diag(Template->getLocation(), diag::note_template_decl_here);
1569 1: return ExprError();
1570 : }
1571 :
1572 : // Make sure that we're referring to a value.
1573 27529: ValueDecl *VD = dyn_cast<ValueDecl>(D);
1: branch 0 taken
27528: branch 1 taken
1574 27529: if (!VD) {
1575 : Diag(Loc, diag::err_ref_non_value)
1576 1: << D << SS.getRange();
1577 1: Diag(D->getLocation(), diag::note_declared_at);
1578 1: return ExprError();
1579 : }
1580 :
1581 : // Check whether this declaration can be used. Note that we suppress
1582 : // this check when we're going to perform argument-dependent lookup
1583 : // on this function name, because this might not be the function
1584 : // that overload resolution actually selects.
0: branch 1 not taken
27528: branch 2 taken
1585 27528: if (DiagnoseUseOfDecl(VD, Loc))
1586 0: return ExprError();
1587 :
1588 : // Only create DeclRefExpr's for valid Decl's.
17: branch 1 taken
27511: branch 2 taken
1589 27528: if (VD->isInvalidDecl())
1590 17: return ExprError();
1591 :
1592 : // If the identifier reference is inside a block, and it refers to a value
1593 : // that is outside the block, create a BlockDeclRefExpr instead of a
1594 : // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when
1595 : // the block is formed.
1596 : //
1597 : // We do not do this for things like enum constants, global variables, etc,
1598 : // as they do not get snapshotted.
1599 : //
306: branch 0 taken
27205: branch 1 taken
178: branch 3 taken
128: branch 4 taken
178: branch 5 taken
27333: branch 6 taken
1600 27511: if (CurBlock && ShouldSnapshotBlockValueReference(CurBlock, VD)) {
2: branch 3 taken
176: branch 4 taken
1601 178: if (VD->getType().getTypePtr()->isVariablyModifiedType()) {
1602 2: Diag(Loc, diag::err_ref_vm_type);
1603 2: Diag(D->getLocation(), diag::note_declared_at);
1604 2: return ExprError();
1605 : }
1606 :
1: branch 3 taken
175: branch 4 taken
1607 176: if (VD->getType()->isArrayType()) {
1608 1: Diag(Loc, diag::err_ref_array_type);
1609 1: Diag(D->getLocation(), diag::note_declared_at);
1610 1: return ExprError();
1611 : }
1612 :
1613 175: MarkDeclarationReferenced(Loc, VD);
1614 175: QualType ExprTy = VD->getType().getNonReferenceType();
1615 : // The BlocksAttr indicates the variable is bound by-reference.
103: branch 1 taken
72: branch 2 taken
1616 175: if (VD->getAttr<BlocksAttr>())
103: branch 1 taken
0: branch 2 not taken
1617 103: return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, true));
1618 : // This is to record that a 'const' was actually synthesize and added.
1619 72: bool constAdded = !ExprTy.isConstQualified();
1620 : // Variable will be bound by-copy, make it const within the closure.
1621 :
1622 72: ExprTy.addConst();
1623 : return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, false,
72: branch 1 taken
0: branch 2 not taken
1624 72: constAdded));
1625 : }
1626 : // If this reference is not in a block or if the referenced variable is
1627 : // within the block, create a normal DeclRefExpr.
1628 :
1629 27333: return BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), Loc, &SS);
1630 : }
1631 :
1632 : Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc,
1633 228: tok::TokenKind Kind) {
1634 : PredefinedExpr::IdentType IT;
1635 :
0: branch 0 not taken
78: branch 1 taken
72: branch 2 taken
78: branch 3 taken
1636 228: switch (Kind) {
1637 0: default: assert(0 && "Unknown simple primary expr!");
1638 78: case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2]
1639 72: case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break;
1640 78: case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break;
1641 : }
1642 :
1643 : // Pre-defined identifiers are of type char[x], where x is the length of the
1644 : // string.
1645 :
1646 228: Decl *currentDecl = getCurFunctionOrMethodDecl();
5: branch 0 taken
223: branch 1 taken
1647 228: if (!currentDecl) {
1648 5: Diag(Loc, diag::ext_predef_outside_function);
1649 5: currentDecl = Context.getTranslationUnitDecl();
1650 : }
1651 :
1652 228: QualType ResTy;
9: branch 2 taken
219: branch 3 taken
1653 228: if (cast<DeclContext>(currentDecl)->isDependentContext()) {
1654 9: ResTy = Context.DependentTy;
1655 : } else {
1656 : unsigned Length =
1657 219: PredefinedExpr::ComputeName(Context, IT, currentDecl).length();
1658 :
1659 219: llvm::APInt LengthI(32, Length + 1);
1660 219: ResTy = Context.CharTy.withConst();
1661 219: ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0);
1662 : }
228: branch 1 taken
0: branch 2 not taken
1663 228: return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT));
1664 : }
1665 :
1666 190: Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
1667 190: llvm::SmallString<16> CharBuffer;
1668 190: CharBuffer.resize(Tok.getLength());
1669 190: const char *ThisTokBegin = &CharBuffer[0];
1670 190: unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
1671 :
1672 : CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
1673 190: Tok.getLocation(), PP);
0: branch 1 not taken
190: branch 2 taken
1674 190: if (Literal.hadError())
1675 0: return ExprError();
1676 :
1677 190: QualType Ty;
166: branch 1 taken
24: branch 2 taken
1678 190: if (!getLangOptions().CPlusPlus)
1679 166: Ty = Context.IntTy; // 'x' and L'x' -> int in C.
4: branch 1 taken
20: branch 2 taken
1680 24: else if (Literal.isWide())
1681 4: Ty = Context.WCharTy; // L'x' -> wchar_t in C++.
1: branch 1 taken
19: branch 2 taken
1682 20: else if (Literal.isMultiChar())
1683 1: Ty = Context.IntTy; // 'wxyz' -> int in C++.
1684 : else
1685 19: Ty = Context.CharTy; // 'x' -> char in C++
1686 :
1687 : return Owned(new (Context) CharacterLiteral(Literal.getValue(),
1688 : Literal.isWide(),
190: branch 4 taken
0: branch 5 not taken
1689 190: Ty, Tok.getLocation()));
1690 : }
1691 :
1692 17433: Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) {
1693 : // Fast path for a single digit (which is quite common). A single digit
1694 : // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
13151: branch 1 taken
4282: branch 2 taken
1695 17433: if (Tok.getLength() == 1) {
1696 13151: const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok);
1697 13151: unsigned IntSize = Context.Target.getIntWidth();
1698 : return Owned(new (Context) IntegerLiteral(llvm::APInt(IntSize, Val-'0'),
13151: branch 4 taken
0: branch 5 not taken
1699 13151: Context.IntTy, Tok.getLocation()));
1700 : }
1701 :
1702 4282: llvm::SmallString<512> IntegerBuffer;
1703 : // Add padding so that NumericLiteralParser can overread by one character.
1704 4282: IntegerBuffer.resize(Tok.getLength()+1);
1705 4282: const char *ThisTokBegin = &IntegerBuffer[0];
1706 :
1707 : // Get the spelling of the token, which eliminates trigraphs, etc.
1708 4282: unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
1709 :
1710 : NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
1711 4282: Tok.getLocation(), PP);
7: branch 0 taken
4275: branch 1 taken
1712 4282: if (Literal.hadError)
1713 7: return ExprError();
1714 :
1715 : Expr *Res;
1716 :
771: branch 1 taken
3504: branch 2 taken
1717 4275: if (Literal.isFloatingLiteral()) {
1718 771: QualType Ty;
153: branch 0 taken
618: branch 1 taken
1719 771: if (Literal.isFloat)
1720 153: Ty = Context.FloatTy;
611: branch 0 taken
7: branch 1 taken
1721 618: else if (!Literal.isLong)
1722 611: Ty = Context.DoubleTy;
1723 : else
1724 7: Ty = Context.LongDoubleTy;
1725 :
1726 771: const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty);
1727 :
1728 : using llvm::APFloat;
1729 771: APFloat Val(Format);
1730 :
1731 771: APFloat::opStatus result = Literal.GetFloatValue(Val);
1732 :
1733 : // Overflow is always an error, but underflow is only an error if
1734 : // we underflowed to zero (APFloat reports denormals as underflow).
767: branch 0 taken
4: branch 1 taken
4: branch 2 taken
763: branch 3 taken
4: branch 5 taken
0: branch 6 not taken
8: branch 7 taken
763: branch 8 taken
1735 771: if ((result & APFloat::opOverflow) ||
1736 : ((result & APFloat::opUnderflow) && Val.isZero())) {
1737 : unsigned diagnostic;
1738 8: llvm::SmallVector<char, 20> buffer;
4: branch 0 taken
4: branch 1 taken
1739 8: if (result & APFloat::opOverflow) {
1740 4: diagnostic = diag::err_float_overflow;
1741 4: APFloat::getLargest(Format).toString(buffer);
1742 : } else {
1743 4: diagnostic = diag::err_float_underflow;
1744 4: APFloat::getSmallest(Format).toString(buffer);
1745 : }
1746 :
1747 : Diag(Tok.getLocation(), diagnostic)
1748 : << Ty
1749 8: << llvm::StringRef(buffer.data(), buffer.size());
1750 : }
1751 :
1752 771: bool isExact = (result == APFloat::opOK);
771: branch 2 taken
0: branch 3 not taken
1753 771: Res = new (Context) FloatingLiteral(Val, isExact, Ty, Tok.getLocation());
1754 :
0: branch 1 not taken
3504: branch 2 taken
1755 3504: } else if (!Literal.isIntegerLiteral()) {
1756 0: return ExprError();
1757 : } else {
1758 3504: QualType Ty;
1759 :
1760 : // long long is a C99 feature.
746: branch 1 taken
2758: branch 2 taken
669: branch 4 taken
77: branch 5 taken
7: branch 6 taken
662: branch 7 taken
7: branch 8 taken
3497: branch 9 taken
1761 3504: if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
1762 : Literal.isLongLong)
1763 7: Diag(Tok.getLocation(), diag::ext_longlong);
1764 :
1765 : // Get the value in the widest-possible width.
1766 3504: llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0);
1767 :
1: branch 1 taken
3503: branch 2 taken
1768 3504: if (Literal.GetIntegerValue(ResultVal)) {
1769 : // If this value didn't fit into uintmax_t, warn and force to ull.
1770 1: Diag(Tok.getLocation(), diag::warn_integer_too_large);
1771 1: Ty = Context.UnsignedLongLongTy;
1772 : assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() &&
1: branch 2 taken
0: branch 3 not taken
1773 1: "long long is not intmax_t?");
1774 : } else {
1775 : // If this value fits into a ULL, try to figure out what else it fits into
1776 : // according to the rules of C99 6.4.4.1p5.
1777 :
1778 : // Octal, Hexadecimal, and integers with a U suffix are allowed to
1779 : // be an unsigned int.
3449: branch 0 taken
54: branch 1 taken
979: branch 3 taken
2470: branch 4 taken
1780 3503: bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
1781 :
1782 : // Check from smallest to largest, picking the smallest type we can.
1783 3503: unsigned Width = 0;
3425: branch 0 taken
78: branch 1 taken
3380: branch 2 taken
45: branch 3 taken
1784 3503: if (!Literal.isLong && !Literal.isLongLong) {
1785 : // Are int/unsigned possibilities?
1786 3380: unsigned IntSize = Context.Target.getIntWidth();
1787 :
1788 : // Does it fit in a unsigned int?
3378: branch 1 taken
2: branch 2 taken
1789 3380: if (ResultVal.isIntN(IntSize)) {
1790 : // Does it fit in a signed int?
3347: branch 0 taken
31: branch 1 taken
3162: branch 3 taken
185: branch 4 taken
3162: branch 5 taken
216: branch 6 taken
1791 3378: if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
1792 3162: Ty = Context.IntTy;
211: branch 0 taken
5: branch 1 taken
1793 216: else if (AllowUnsigned)
1794 211: Ty = Context.UnsignedIntTy;
1795 3378: Width = IntSize;
1796 : }
1797 : }
1798 :
1799 : // Are long/unsigned long possibilities?
130: branch 1 taken
3373: branch 2 taken
85: branch 3 taken
45: branch 4 taken
85: branch 5 taken
3418: branch 6 taken
1800 3503: if (Ty.isNull() && !Literal.isLongLong) {
1801 85: unsigned LongSize = Context.Target.getLongWidth();
1802 :
1803 : // Does it fit in a unsigned long?
83: branch 1 taken
2: branch 2 taken
1804 85: if (ResultVal.isIntN(LongSize)) {
1805 : // Does it fit in a signed long?
79: branch 0 taken
4: branch 1 taken
74: branch 3 taken
5: branch 4 taken
74: branch 5 taken
9: branch 6 taken
1806 83: if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
1807 74: Ty = Context.LongTy;
4: branch 0 taken
5: branch 1 taken
1808 9: else if (AllowUnsigned)
1809 4: Ty = Context.UnsignedLongTy;
1810 83: Width = LongSize;
1811 : }
1812 : }
1813 :
1814 : // Finally, check long long if needed.
52: branch 1 taken
3451: branch 2 taken
1815 3503: if (Ty.isNull()) {
1816 52: unsigned LongLongSize = Context.Target.getLongLongWidth();
1817 :
1818 : // Does it fit in a unsigned long long?
52: branch 1 taken
0: branch 2 not taken
1819 52: if (ResultVal.isIntN(LongLongSize)) {
1820 : // Does it fit in a signed long long?
33: branch 0 taken
19: branch 1 taken
33: branch 3 taken
0: branch 4 not taken
33: branch 5 taken
19: branch 6 taken
1821 52: if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
1822 33: Ty = Context.LongLongTy;
19: branch 0 taken
0: branch 1 not taken
1823 19: else if (AllowUnsigned)
1824 19: Ty = Context.UnsignedLongLongTy;
1825 52: Width = LongLongSize;
1826 : }
1827 : }
1828 :
1829 : // If we still couldn't decide a type, we probably have something that
1830 : // does not fit in a signed long long, but has no U suffix.
0: branch 1 not taken
3503: branch 2 taken
1831 3503: if (Ty.isNull()) {
1832 0: Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
1833 0: Ty = Context.UnsignedLongLongTy;
1834 0: Width = Context.Target.getLongLongWidth();
1835 : }
1836 :
3395: branch 1 taken
108: branch 2 taken
1837 3503: if (ResultVal.getBitWidth() != Width)
1838 3395: ResultVal.trunc(Width);
1839 : }
3504: branch 2 taken
0: branch 3 not taken
1840 3504: Res = new (Context) IntegerLiteral(ResultVal, Ty, Tok.getLocation());
1841 : }
1842 :
1843 : // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
55: branch 0 taken
4220: branch 1 taken
1844 4275: if (Literal.isImaginary)
1845 : Res = new (Context) ImaginaryLiteral(Res,
55: branch 3 taken
0: branch 4 not taken
1846 55: Context.getComplexType(Res->getType()));
1847 :
1848 4275: return Owned(Res);
1849 : }
1850 :
1851 : Action::OwningExprResult Sema::ActOnParenExpr(SourceLocation L,
1852 3557: SourceLocation R, ExprArg Val) {
1853 3557: Expr *E = Val.takeAs<Expr>();
0: branch 0 not taken
3557: branch 1 taken
1854 3557: assert((E != 0) && "ActOnParenExpr() missing expr");
3557: branch 1 taken
0: branch 2 not taken
1855 3557: return Owned(new (Context) ParenExpr(L, R, E));
1856 : }
1857 :
1858 : /// The UsualUnaryConversions() function is *not* called by this routine.
1859 : /// See C99 6.3.2.1p[2-4] for more details.
1860 : bool Sema::CheckSizeOfAlignOfOperand(QualType exprType,
1861 : SourceLocation OpLoc,
1862 : const SourceRange &ExprRange,
1863 602: bool isSizeof) {
0: branch 2 not taken
602: branch 3 taken
1864 602: if (exprType->isDependentType())
1865 0: return false;
1866 :
1867 : // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
1868 : // the result is the size of the referenced type."
1869 : // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
1870 : // result shall be the alignment of the referenced type."
1: branch 2 taken
601: branch 3 taken
1871 602: if (const ReferenceType *Ref = exprType->getAs<ReferenceType>())
1872 1: exprType = Ref->getPointeeType();
1873 :
1874 : // C99 6.5.3.4p1:
11: branch 2 taken
591: branch 3 taken
1875 602: if (exprType->isFunctionType()) {
1876 : // alignof(function) is allowed as an extension.
11: branch 0 taken
0: branch 1 not taken
1877 11: if (isSizeof)
1878 11: Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange;
1879 11: return false;
1880 : }
1881 :
1882 : // Allow sizeof(void)/alignof(void) as an extension.
22: branch 2 taken
569: branch 3 taken
1883 591: if (exprType->isVoidType()) {
1884 : Diag(OpLoc, diag::ext_sizeof_void_type)
19: branch 0 taken
3: branch 1 taken
1885 22: << (isSizeof ? "sizeof" : "__alignof") << ExprRange;
1886 22: return false;
1887 : }
1888 :
17: branch 10 taken
552: branch 11 taken
1889 569: if (RequireCompleteType(OpLoc, exprType,
1890 : PDiag(diag::err_sizeof_alignof_incomplete_type)
1891 : << int(!isSizeof) << ExprRange))
1892 17: return true;
1893 :
1894 : // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode.
3: branch 0 taken
549: branch 1 taken
3: branch 4 taken
0: branch 5 not taken
3: branch 6 taken
549: branch 7 taken
1895 552: if (LangOpts.ObjCNonFragileABI && exprType->isObjCInterfaceType()) {
1896 : Diag(OpLoc, diag::err_sizeof_nonfragile_interface)
1897 3: << exprType << isSizeof << ExprRange;
1898 3: return true;
1899 : }
1900 :
1901 549: return false;
1902 : }
1903 :
1904 : bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc,
1905 22: const SourceRange &ExprRange) {
1906 22: E = E->IgnoreParens();
1907 :
1908 : // alignof decl is always ok.
19: branch 1 taken
3: branch 2 taken
1909 22: if (isa<DeclRefExpr>(E))
1910 19: return false;
1911 :
1912 : // Cannot know anything else if the expression is dependent.
0: branch 1 not taken
3: branch 2 taken
1913 3: if (E->isTypeDependent())
1914 0: return false;
1915 :
1: branch 1 taken
2: branch 2 taken
1916 3: if (E->getBitField()) {
1917 1: Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange;
1918 1: return true;
1919 : }
1920 :
1921 : // Alignment of a field access is always okay, so long as it isn't a
1922 : // bit-field.
1: branch 1 taken
1: branch 2 taken
1923 2: if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
1: branch 2 taken
0: branch 3 not taken
1924 1: if (isa<FieldDecl>(ME->getMemberDecl()))
1925 1: return false;
1926 :
1927 1: return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false);
1928 : }
1929 :
1930 : /// \brief Build a sizeof or alignof expression given a type operand.
1931 : Action::OwningExprResult
1932 : Sema::CreateSizeOfAlignOfExpr(TypeSourceInfo *TInfo,
1933 : SourceLocation OpLoc,
1934 447: bool isSizeOf, SourceRange R) {
0: branch 0 not taken
447: branch 1 taken
1935 447: if (!TInfo)
1936 0: return ExprError();
1937 :
1938 447: QualType T = TInfo->getType();
1939 :
421: branch 2 taken
26: branch 3 taken
20: branch 5 taken
401: branch 6 taken
20: branch 7 taken
427: branch 8 taken
1940 447: if (!T->isDependentType() &&
1941 : CheckSizeOfAlignOfOperand(T, OpLoc, R, isSizeOf))
1942 20: return ExprError();
1943 :
1944 : // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
1945 : return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, TInfo,
1946 : Context.getSizeType(), OpLoc,
427: branch 4 taken
0: branch 5 not taken
1947 427: R.getEnd()));
1948 : }
1949 :
1950 : /// \brief Build a sizeof or alignof expression given an expression
1951 : /// operand.
1952 : Action::OwningExprResult
1953 : Sema::CreateSizeOfAlignOfExpr(Expr *E, SourceLocation OpLoc,
1954 224: bool isSizeOf, SourceRange R) {
1955 : // Verify that the operand is valid.
1956 224: bool isInvalid = false;
203: branch 1 taken
21: branch 2 taken
1957 224: if (E->isTypeDependent()) {
1958 : // Delay type-checking for type-dependent expressions.
22: branch 0 taken
181: branch 1 taken
1959 203: } else if (!isSizeOf) {
1960 22: isInvalid = CheckAlignOfExpr(E, OpLoc, R);
1: branch 1 taken
180: branch 2 taken
1961 181: } else if (E->getBitField()) { // C99 6.5.3.4p1.
1962 1: Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0;
1963 1: isInvalid = true;
1964 : } else {
1965 180: isInvalid = CheckSizeOfAlignOfOperand(E->getType(), OpLoc, R, true);
1966 : }
1967 :
2: branch 0 taken
222: branch 1 taken
1968 224: if (isInvalid)
1969 2: return ExprError();
1970 :
1971 : // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
1972 : return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, E,
1973 : Context.getSizeType(), OpLoc,
222: branch 4 taken
0: branch 5 not taken
1974 222: R.getEnd()));
1975 : }
1976 :
1977 : /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and
1978 : /// the same for @c alignof and @c __alignof
1979 : /// Note that the ArgRange is invalid if isType is false.
1980 : Action::OwningExprResult
1981 : Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType,
1982 616: void *TyOrEx, const SourceRange &ArgRange) {
1983 : // If error parsing type, ignore.
4: branch 0 taken
612: branch 1 taken
1984 616: if (TyOrEx == 0) return ExprError();
1985 :
418: branch 0 taken
194: branch 1 taken
1986 612: if (isType) {
1987 : TypeSourceInfo *TInfo;
1988 418: (void) GetTypeFromParser(TyOrEx, &TInfo);
1989 418: return CreateSizeOfAlignOfExpr(TInfo, OpLoc, isSizeof, ArgRange);
1990 : }
1991 :
1992 194: Expr *ArgEx = (Expr *)TyOrEx;
1993 : Action::OwningExprResult Result
1994 194: = CreateSizeOfAlignOfExpr(ArgEx, OpLoc, isSizeof, ArgEx->getSourceRange());
1995 :
2: branch 1 taken
192: branch 2 taken
1996 194: if (Result.isInvalid())
1997 2: DeleteExpr(ArgEx);
1998 :
1999 194: return move(Result);
2000 : }
2001 :
2002 126: QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc, bool isReal) {
0: branch 1 not taken
126: branch 2 taken
2003 126: if (V->isTypeDependent())
2004 0: return Context.DependentTy;
2005 :
2006 : // These operators return the element type of a complex type.
80: branch 3 taken
46: branch 4 taken
2007 126: if (const ComplexType *CT = V->getType()->getAs<ComplexType>())
2008 80: return CT->getElementType();
2009 :
2010 : // Otherwise they pass through real integer and floating point types here.
46: branch 3 taken
0: branch 4 not taken
2011 46: if (V->getType()->isArithmeticType())
2012 46: return V->getType();
2013 :
2014 : // Reject anything else.
2015 : Diag(Loc, diag::err_realimag_invalid_type) << V->getType()
0: branch 0 not taken
0: branch 1 not taken
2016 0: << (isReal ? "__real" : "__imag");
2017 0: return QualType();
2018 : }
2019 :
2020 :
2021 :
2022 : Action::OwningExprResult
2023 : Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc,
2024 346: tok::TokenKind Kind, ExprArg Input) {
2025 : UnaryOperator::Opcode Opc;
0: branch 0 not taken
321: branch 1 taken
25: branch 2 taken
2026 346: switch (Kind) {
2027 0: default: assert(0 && "Unknown unary op!");
2028 321: case tok::plusplus: Opc = UnaryOperator::PostInc; break;
2029 25: case tok::minusminus: Opc = UnaryOperator::PostDec; break;
2030 : }
2031 :
2032 346: return BuildUnaryOp(S, OpLoc, Opc, move(Input));
2033 : }
2034 :
2035 : Action::OwningExprResult
2036 : Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc,
2037 855: ExprArg Idx, SourceLocation RLoc) {
2038 : // Since this might be a postfix expression, get rid of ParenListExprs.
2039 855: Base = MaybeConvertParenListExprToParenExpr(S, move(Base));
2040 :
2041 855: Expr *LHSExp = static_cast<Expr*>(Base.get()),
2042 855: *RHSExp = static_cast<Expr*>(Idx.get());
2043 :
221: branch 1 taken
634: branch 2 taken
208: branch 4 taken
13: branch 5 taken
1: branch 7 taken
207: branch 8 taken
14: branch 9 taken
841: branch 10 taken
2044 855: if (getLangOptions().CPlusPlus &&
2045 : (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) {
2046 14: Base.release();
2047 14: Idx.release();
2048 : return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp,
14: branch 2 taken
0: branch 3 not taken
2049 14: Context.DependentTy, RLoc));
2050 : }
2051 :
207: branch 1 taken
634: branch 2 taken
185: branch 6 taken
22: branch 7 taken
185: branch 11 taken
0: branch 12 not taken
184: branch 16 taken
1: branch 17 taken
4: branch 21 taken
180: branch 22 taken
27: branch 23 taken
814: branch 24 taken
2052 841: if (getLangOptions().CPlusPlus &&
2053 : (LHSExp->getType()->isRecordType() ||
2054 : LHSExp->getType()->isEnumeralType() ||
2055 : RHSExp->getType()->isRecordType() ||
2056 : RHSExp->getType()->isEnumeralType())) {
2057 27: return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, move(Base),move(Idx));
2058 : }
2059 :
2060 814: return CreateBuiltinArraySubscriptExpr(move(Base), LLoc, move(Idx), RLoc);
2061 : }
2062 :
2063 :
2064 : Action::OwningExprResult
2065 : Sema::CreateBuiltinArraySubscriptExpr(ExprArg Base, SourceLocation LLoc,
2066 824: ExprArg Idx, SourceLocation RLoc) {
2067 824: Expr *LHSExp = static_cast<Expr*>(Base.get());
2068 824: Expr *RHSExp = static_cast<Expr*>(Idx.get());
2069 :
2070 : // Perform default conversions.
652: branch 3 taken
172: branch 4 taken
2071 824: if (!LHSExp->getType()->getAs<VectorType>())
2072 652: DefaultFunctionArrayLvalueConversion(LHSExp);
2073 824: DefaultFunctionArrayLvalueConversion(RHSExp);
2074 :
2075 824: QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
2076 :
2077 : // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
2078 : // to the expression *((e1)+(e2)). This means the array "Base" may actually be
2079 : // in the subscript position. As a result, we need to derive the array base
2080 : // and index from the expression types.
2081 : Expr *BaseExpr, *IndexExpr;
2082 824: QualType ResultType;
824: branch 2 taken
0: branch 3 not taken
0: branch 6 not taken
824: branch 7 taken
0: branch 8 not taken
824: branch 9 taken
2083 824: if (LHSTy->isDependentType() || RHSTy->isDependentType()) {
2084 0: BaseExpr = LHSExp;
2085 0: IndexExpr = RHSExp;
2086 0: ResultType = Context.DependentTy;
636: branch 2 taken
188: branch 3 taken
2087 824: } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) {
2088 636: BaseExpr = LHSExp;
2089 636: IndexExpr = RHSExp;
2090 636: ResultType = PTy->getPointeeType();
7: branch 2 taken
181: branch 3 taken
2091 188: } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) {
2092 : // Handle the uncommon case of "123[Ptr]".
2093 7: BaseExpr = RHSExp;
2094 7: IndexExpr = LHSExp;
2095 7: ResultType = PTy->getPointeeType();
7: branch 0 taken
174: branch 1 taken
2096 181: } else if (const ObjCObjectPointerType *PTy =
2097 181: LHSTy->getAs<ObjCObjectPointerType>()) {
2098 7: BaseExpr = LHSExp;
2099 7: IndexExpr = RHSExp;
2100 7: ResultType = PTy->getPointeeType();
0: branch 0 not taken
174: branch 1 taken
2101 174: } else if (const ObjCObjectPointerType *PTy =
2102 174: RHSTy->getAs<ObjCObjectPointerType>()) {
2103 : // Handle the uncommon case of "123[Ptr]".
2104 0: BaseExpr = RHSExp;
2105 0: IndexExpr = LHSExp;
2106 0: ResultType = PTy->getPointeeType();
172: branch 2 taken
2: branch 3 taken
2107 174: } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) {
2108 172: BaseExpr = LHSExp; // vectors: V[123]
2109 172: IndexExpr = RHSExp;
2110 :
2111 : // FIXME: need to deal with const...
2112 172: ResultType = VTy->getElementType();
1: branch 2 taken
1: branch 3 taken
2113 2: } else if (LHSTy->isArrayType()) {
2114 : // If we see an array that wasn't promoted by
2115 : // DefaultFunctionArrayLvalueConversion, it must be an array that
2116 : // wasn't promoted because of the C90 rule that doesn't
2117 : // allow promoting non-lvalue arrays. Warn, then
2118 : // force the promotion here.
2119 : Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) <<
2120 1: LHSExp->getSourceRange();
2121 : ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy),
2122 1: CastExpr::CK_ArrayToPointerDecay);
2123 1: LHSTy = LHSExp->getType();
2124 :
2125 1: BaseExpr = LHSExp;
2126 1: IndexExpr = RHSExp;
2127 1: ResultType = LHSTy->getAs<PointerType>()->getPointeeType();
1: branch 2 taken
0: branch 3 not taken
2128 1: } else if (RHSTy->isArrayType()) {
2129 : // Same as previous, except for 123[f().a] case
2130 : Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) <<
2131 1: RHSExp->getSourceRange();
2132 : ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy),
2133 1: CastExpr::CK_ArrayToPointerDecay);
2134 1: RHSTy = RHSExp->getType();
2135 :
2136 1: BaseExpr = RHSExp;
2137 1: IndexExpr = LHSExp;
2138 1: ResultType = RHSTy->getAs<PointerType>()->getPointeeType();
2139 : } else {
2140 : return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value)
2141 0: << LHSExp->getSourceRange() << RHSExp->getSourceRange());
2142 : }
2143 : // C99 6.5.2.1p1
824: branch 3 taken
0: branch 4 not taken
0: branch 8 not taken
824: branch 9 taken
0: branch 11 not taken
0: branch 12 not taken
0: branch 13 not taken
824: branch 14 taken
2144 824: if (!(IndexExpr->getType()->isIntegerType() &&
2145 : IndexExpr->getType()->isScalarType()) && !IndexExpr->isTypeDependent())
2146 : return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer)
2147 0: << IndexExpr->getSourceRange());
2148 :
815: branch 3 taken
9: branch 4 taken
0: branch 8 not taken
815: branch 9 taken
9: branch 11 taken
0: branch 12 not taken
9: branch 13 taken
815: branch 14 taken
2149 824: if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) ||
2150 : IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U))
2151 : && !IndexExpr->isTypeDependent())
2152 9: Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
2153 :
2154 : // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly,
2155 : // C++ [expr.sub]p1: The type "T" shall be a completely-defined object
2156 : // type. Note that Functions are not objects, and that (in C99 parlance)
2157 : // incomplete types are not object types.
0: branch 2 not taken
824: branch 3 taken
2158 824: if (ResultType->isFunctionType()) {
2159 : Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type)
2160 0: << ResultType << BaseExpr->getSourceRange();
2161 0: return ExprError();
2162 : }
2163 :
824: branch 2 taken
0: branch 3 not taken
2: branch 11 taken
822: branch 12 taken
824: branch 13 taken
0: branch 14 not taken
824: branch 16 taken
0: branch 17 not taken
824: branch 19 taken
0: branch 20 not taken
2: branch 22 taken
822: branch 23 taken
2164 824: if (!ResultType->isDependentType() &&
2165 : RequireCompleteType(LLoc, ResultType,
2166 : PDiag(diag::err_subscript_incomplete_type)
2167 : << BaseExpr->getSourceRange()))
2168 2: return ExprError();
2169 :
2170 : // Diagnose bad cases where we step over interface counts.
6: branch 2 taken
816: branch 3 taken
1: branch 4 taken
5: branch 5 taken
1: branch 6 taken
821: branch 7 taken
2171 822: if (ResultType->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) {
2172 : Diag(LLoc, diag::err_subscript_nonfragile_interface)
2173 1: << ResultType << BaseExpr->getSourceRange();
2174 1: return ExprError();
2175 : }
2176 :
2177 821: Base.release();
2178 821: Idx.release();
2179 : return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp,
821: branch 1 taken
0: branch 2 not taken
2180 821: ResultType, RLoc));
2181 : }
2182 :
2183 : QualType Sema::
2184 : CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc,
2185 : const IdentifierInfo *CompName,
2186 132: SourceLocation CompLoc) {
2187 : // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements,
2188 : // see FIXME there.
2189 : //
2190 : // FIXME: This logic can be greatly simplified by splitting it along
2191 : // halving/not halving and reworking the component checking.
2192 132: const ExtVectorType *vecType = baseType->getAs<ExtVectorType>();
2193 :
2194 : // The vector accessor can't exceed the number of elements.
2195 132: const char *compStr = CompName->getNameStart();
2196 :
2197 : // This flag determines whether or not the component is one of the four
2198 : // special names that indicate a subset of exactly half the elements are
2199 : // to be selected.
2200 132: bool HalvingSwizzle = false;
2201 :
2202 : // This flag determines whether or not CompName has an 's' char prefix,
2203 : // indicating that it is a string of hex values to be used as vector indices.
127: branch 0 taken
5: branch 1 taken
1: branch 2 taken
126: branch 3 taken
2204 132: bool HexSwizzle = *compStr == 's' || *compStr == 'S';
2205 :
2206 : // Check that we've found one of the special components, or that the component
2207 : // names must come from the same set.
120: branch 1 taken
12: branch 2 taken
108: branch 4 taken
12: branch 5 taken
96: branch 7 taken
12: branch 8 taken
1: branch 10 taken
95: branch 11 taken
2208 169: if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
2209 : !strcmp(compStr, "even") || !strcmp(compStr, "odd")) {
2210 37: HalvingSwizzle = true;
89: branch 1 taken
6: branch 2 taken
2211 95: } else if (vecType->getPointAccessorIdx(*compStr) != -1) {
95: branch 0 taken
88: branch 1 taken
94: branch 3 taken
1: branch 4 taken
94: branch 5 taken
89: branch 6 taken
2212 183: do
2213 183: compStr++;
2214 : while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
0: branch 0 not taken
6: branch 1 taken
0: branch 3 not taken
0: branch 4 not taken
6: branch 5 taken
0: branch 6 not taken
2215 6: } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) {
11: branch 0 taken
5: branch 1 taken
10: branch 3 taken
1: branch 4 taken
10: branch 5 taken
6: branch 6 taken
2216 16: do
2217 16: compStr++;
2218 : while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1);
2219 : }
2220 :
95: branch 0 taken
37: branch 1 taken
2: branch 2 taken
93: branch 3 taken
2221 132: if (!HalvingSwizzle && *compStr) {
2222 : // We didn't get to the end of the string. This means the component names
2223 : // didn't come from the same set *or* we encountered an illegal name.
2224 : Diag(OpLoc, diag::err_ext_vector_component_name_illegal)
2225 2: << std::string(compStr,compStr+1) << SourceRange(CompLoc);
2226 2: return QualType();
2227 : }
2228 :
2229 : // Ensure no component accessor exceeds the width of the vector type it
2230 : // operates on.
93: branch 0 taken
37: branch 1 taken
2231 130: if (!HalvingSwizzle) {
2232 93: compStr = CompName->getNameStart();
2233 :
5: branch 0 taken
88: branch 1 taken
2234 93: if (HexSwizzle)
2235 5: compStr++;
2236 :
187: branch 0 taken
88: branch 1 taken
2237 368: while (*compStr) {
5: branch 1 taken
182: branch 2 taken
2238 187: if (!vecType->isAccessorWithinNumElements(*compStr++)) {
2239 : Diag(OpLoc, diag::err_ext_vector_component_exceeds_length)
2240 5: << baseType << SourceRange(CompLoc);
2241 5: return QualType();
2242 : }
2243 : }
2244 : }
2245 :
2246 : // The component accessor looks fine - now we need to compute the actual type.
2247 : // The vector type is implied by the component accessor. For example,
2248 : // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
2249 : // vec4.s0 is a float, vec4.s23 is a vec3, etc.
2250 : // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2.
2251 : unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2
37: branch 0 taken
88: branch 1 taken
2252 125: : CompName->getLength();
4: branch 0 taken
121: branch 1 taken
2253 125: if (HexSwizzle)
2254 4: CompSize--;
2255 :
43: branch 0 taken
82: branch 1 taken
2256 125: if (CompSize == 1)
2257 43: return vecType->getElementType();
2258 :
2259 82: QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize);
2260 : // Now look up the TypeDefDecl from the vector type. Without this,
2261 : // diagostics look bad. We want extended vector types to appear built-in.
113: branch 1 taken
5: branch 2 taken
2262 118: for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) {
77: branch 3 taken
36: branch 4 taken
2263 113: if (ExtVectorDecls[i]->getUnderlyingType() == VT)
2264 77: return Context.getTypedefType(ExtVectorDecls[i]);
2265 : }
2266 5: return VT; // should never get here (a typedef type should always be found).
2267 : }
2268 :
2269 : static Decl *FindGetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl,
2270 : IdentifierInfo *Member,
2271 : const Selector &Sel,
2272 3: ASTContext &Context) {
2273 :
0: branch 1 not taken
3: branch 2 taken
2274 3: if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member))
2275 0: return PD;
1: branch 1 taken
2: branch 2 taken
2276 3: if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel))
2277 1: return OMD;
2278 :
1: branch 1 taken
1: branch 2 taken
2279 4: for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(),
2280 2: E = PDecl->protocol_end(); I != E; ++I) {
1: branch 0 taken
0: branch 1 not taken
2281 1: if (Decl *D = FindGetterNameDeclFromProtocolList(*I, Member, Sel,
2282 1: Context))
2283 1: return D;
2284 : }
2285 1: return 0;
2286 : }
2287 :
2288 : static Decl *FindGetterNameDecl(const ObjCObjectPointerType *QIdTy,
2289 : IdentifierInfo *Member,
2290 : const Selector &Sel,
2291 15: ASTContext &Context) {
2292 : // Check protocols on qualified interfaces.
2293 15: Decl *GDecl = 0;
10: branch 1 taken
7: branch 2 taken
2294 32: for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
2295 15: E = QIdTy->qual_end(); I != E; ++I) {
7: branch 1 taken
3: branch 2 taken
2296 10: if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
2297 7: GDecl = PD;
2298 7: break;
2299 : }
2300 : // Also must look for a getter name which uses property syntax.
1: branch 1 taken
2: branch 2 taken
2301 3: if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) {
2302 1: GDecl = OMD;
2303 1: break;
2304 : }
2305 : }
7: branch 0 taken
8: branch 1 taken
2306 15: if (!GDecl) {
2: branch 1 taken
6: branch 2 taken
2307 15: for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
2308 7: E = QIdTy->qual_end(); I != E; ++I) {
2309 : // Search in the protocol-qualifier list of current protocol.
2310 2: GDecl = FindGetterNameDeclFromProtocolList(*I, Member, Sel, Context);
1: branch 0 taken
1: branch 1 taken
2311 2: if (GDecl)
2312 1: return GDecl;
2313 : }
2314 : }
2315 14: return GDecl;
2316 : }
2317 :
2318 : Sema::OwningExprResult
2319 : Sema::ActOnDependentMemberExpr(ExprArg Base, QualType BaseType,
2320 : bool IsArrow, SourceLocation OpLoc,
2321 : const CXXScopeSpec &SS,
2322 : NamedDecl *FirstQualifierInScope,
2323 : DeclarationName Name, SourceLocation NameLoc,
2324 81: const TemplateArgumentListInfo *TemplateArgs) {
2325 81: Expr *BaseExpr = Base.takeAs<Expr>();
2326 :
2327 : // Even in dependent contexts, try to diagnose base expressions with
2328 : // obviously wrong types, e.g.:
2329 : //
2330 : // T* t;
2331 : // t.f;
2332 : //
2333 : // In Obj-C++, however, the above expression is valid, since it could be
2334 : // accessing the 'f' property if T is an Obj-C interface. The extra check
2335 : // allows this, while still reporting an error if T is a struct pointer.
58: branch 0 taken
23: branch 1 taken
2336 81: if (!IsArrow) {
2337 58: const PointerType *PT = BaseType->getAs<PointerType>();
4: branch 0 taken
54: branch 1 taken
2: branch 3 taken
2: branch 4 taken
0: branch 8 not taken
2: branch 9 taken
2: branch 10 taken
56: branch 11 taken
2338 58: if (PT && (!getLangOptions().ObjC1 ||
2339 : PT->getPointeeType()->isRecordType())) {
0: branch 0 not taken
2: branch 1 taken
2340 2: assert(BaseExpr && "cannot happen with implicit member accesses");
2341 : Diag(NameLoc, diag::err_typecheck_member_reference_struct_union)
2342 2: << BaseType << BaseExpr->getSourceRange();
2343 2: return ExprError();
2344 : }
2345 : }
2346 :
2: branch 2 taken
77: branch 3 taken
0: branch 5 not taken
2: branch 6 taken
2347 79: assert(BaseType->isDependentType() || Name.isDependentName());
2348 :
2349 : // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr
2350 : // must have pointer type, and the accessed type is the pointee.
2351 : return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType,
2352 : IsArrow, OpLoc,
2353 : static_cast<NestedNameSpecifier*>(SS.getScopeRep()),
2354 : SS.getRange(),
2355 : FirstQualifierInScope,
2356 : Name, NameLoc,
2357 79: TemplateArgs));
2358 : }
2359 :
2360 : /// We know that the given qualified member reference points only to
2361 : /// declarations which do not belong to the static type of the base
2362 : /// expression. Diagnose the problem.
2363 : static void DiagnoseQualifiedMemberReference(Sema &SemaRef,
2364 : Expr *BaseExpr,
2365 : QualType BaseType,
2366 : const CXXScopeSpec &SS,
2367 6: const LookupResult &R) {
2368 : // If this is an implicit member access, use a different set of
2369 : // diagnostics.
2: branch 0 taken
4: branch 1 taken
2370 6: if (!BaseExpr)
2371 2: return DiagnoseInstanceReference(SemaRef, SS, R);
2372 :
2373 : // FIXME: this is an exceedingly lame diagnostic for some of the more
2374 : // complicated cases here.
2375 4: DeclContext *DC = R.getRepresentativeDecl()->getDeclContext();
2376 : SemaRef.Diag(R.getNameLoc(), diag::err_not_direct_base_or_virtual)
2377 4: << SS.getRange() << DC << BaseType;
2378 : }
2379 :
2380 : // Check whether the declarations we found through a nested-name
2381 : // specifier in a member expression are actually members of the base
2382 : // type. The restriction here is:
2383 : //
2384 : // C++ [expr.ref]p2:
2385 : // ... In these cases, the id-expression shall name a
2386 : // member of the class or of one of its base classes.
2387 : //
2388 : // So it's perfectly legitimate for the nested-name specifier to name
2389 : // an unrelated class, and for us to find an overload set including
2390 : // decls from classes which are not superclasses, as long as the decl
2391 : // we actually pick through overload resolution is from a superclass.
2392 : bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr,
2393 : QualType BaseType,
2394 : const CXXScopeSpec &SS,
2395 611: const LookupResult &R) {
2396 611: const RecordType *BaseRT = BaseType->getAs<RecordType>();
78: branch 0 taken
533: branch 1 taken
2397 611: if (!BaseRT) {
2398 : // We can't check this yet because the base type is still
2399 : // dependent.
0: branch 2 not taken
78: branch 3 taken
2400 78: assert(BaseType->isDependentType());
2401 78: return false;
2402 : }
2403 533: CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl());
2404 :
7: branch 3 taken
515: branch 4 taken
534: branch 7 taken
6: branch 8 taken
2405 1595: for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
2406 : // If this is an implicit member reference and we find a
2407 : // non-instance member, it's not an error.
22: branch 0 taken
512: branch 1 taken
12: branch 5 taken
10: branch 6 taken
12: branch 7 taken
522: branch 8 taken
2408 534: if (!BaseExpr && !IsInstanceMember((*I)->getUnderlyingDecl()))
2409 12: return false;
2410 :
2411 : // Note that we use the DC of the decl, not the underlying decl.
2412 522: CXXRecordDecl *RecordD = cast<CXXRecordDecl>((*I)->getDeclContext());
0: branch 1 not taken
522: branch 2 taken
2413 1044: while (RecordD->isAnonymousStructOrUnion())
2414 0: RecordD = cast<CXXRecordDecl>(RecordD->getParent());
2415 :
2416 522: llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord;
2417 522: MemberRecord.insert(RecordD->getCanonicalDecl());
2418 :
515: branch 1 taken
7: branch 2 taken
2419 522: if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord))
2420 1030: return false;
2421 : }
2422 :
2423 6: DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS, R);
2424 6: return true;
2425 : }
2426 :
2427 : static bool
2428 : LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R,
2429 : SourceRange BaseRange, const RecordType *RTy,
2430 1560: SourceLocation OpLoc, const CXXScopeSpec &SS) {
2431 1560: RecordDecl *RDecl = RTy->getDecl();
1: branch 10 taken
1559: branch 11 taken
2432 1560: if (SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0),
2433 : PDiag(diag::err_typecheck_incomplete_tag)
2434 : << BaseRange))
2435 1: return true;
2436 :
1559: branch 0 taken
0: branch 1 not taken
2437 1559: DeclContext *DC = RDecl;
70: branch 1 taken
1489: branch 2 taken
2438 1559: if (SS.isSet()) {
2439 : // If the member name was a qualified-id, look into the
2440 : // nested-name-specifier.
2441 70: DC = SemaRef.computeDeclContext(SS, false);
2442 :
0: branch 1 not taken
70: branch 2 taken
2443 70: if (SemaRef.RequireCompleteDeclContext(SS)) {
2444 : SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag)
2445 0: << SS.getRange() << DC;
2446 0: return true;
2447 : }
2448 :
0: branch 0 not taken
70: branch 1 taken
2449 70: assert(DC && "Cannot handle non-computable dependent contexts in lookup");
2450 :
2: branch 1 taken
68: branch 2 taken
2451 70: if (!isa<TypeDecl>(DC)) {
2452 : SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass)
2453 2: << DC << SS.getRange();
2454 2: return true;
2455 : }
2456 : }
2457 :
2458 : // The record definition is complete, now look up the member.
2459 1557: SemaRef.LookupQualifiedName(R, DC);
2460 :
1540: branch 1 taken
17: branch 2 taken
2461 1557: if (!R.empty())
2462 1540: return false;
2463 :
2464 : // We didn't find anything with the given name, so try to correct
2465 : // for typos.
2466 17: DeclarationName Name = R.getLookupName();
2: branch 1 taken
15: branch 2 taken
0: branch 6 not taken
2: branch 7 taken
0: branch 11 not taken
0: branch 12 not taken
2: branch 13 taken
15: branch 14 taken
2467 17: if (SemaRef.CorrectTypo(R, 0, &SS, DC) &&
2468 : (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin()))) {
2469 : SemaRef.Diag(R.getNameLoc(), diag::err_no_member_suggest)
2470 : << Name << DC << R.getLookupName() << SS.getRange()
2471 : << CodeModificationHint::CreateReplacement(R.getNameLoc(),
2472 2: R.getLookupName().getAsString());
2: branch 1 taken
0: branch 2 not taken
2473 2: if (NamedDecl *ND = R.getAsSingle<NamedDecl>())
2474 : SemaRef.Diag(ND->getLocation(), diag::note_previous_decl)
2475 2: << ND->getDeclName();
2476 2: return false;
2477 : } else {
2478 15: R.clear();
2479 : }
2480 :
2481 15: return false;
2482 : }
2483 :
2484 : Sema::OwningExprResult
2485 : Sema::BuildMemberReferenceExpr(ExprArg BaseArg, QualType BaseType,
2486 : SourceLocation OpLoc, bool IsArrow,
2487 : const CXXScopeSpec &SS,
2488 : NamedDecl *FirstQualifierInScope,
2489 : DeclarationName Name, SourceLocation NameLoc,
2490 97: const TemplateArgumentListInfo *TemplateArgs) {
2491 97: Expr *Base = BaseArg.takeAs<Expr>();
2492 :
96: branch 2 taken
1: branch 3 taken
27: branch 5 taken
69: branch 6 taken
0: branch 8 not taken
27: branch 9 taken
1: branch 10 taken
96: branch 11 taken
2493 97: if (BaseType->isDependentType() ||
2494 : (SS.isSet() && isDependentScopeSpecifier(SS)))
2495 : return ActOnDependentMemberExpr(ExprArg(*this, Base), BaseType,
2496 : IsArrow, OpLoc,
2497 : SS, FirstQualifierInScope,
2498 : Name, NameLoc,
2499 1: TemplateArgs);
2500 :
2501 96: LookupResult R(*this, Name, NameLoc, LookupMemberName);
2502 :
2503 : // Implicit member accesses.
12: branch 0 taken
84: branch 1 taken
2504 96: if (!Base) {
2505 12: QualType RecordTy = BaseType;
12: branch 0 taken
0: branch 1 not taken
2506 12: if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType();
0: branch 4 not taken
12: branch 5 taken
2507 12: if (LookupMemberExprInRecord(*this, R, SourceRange(),
2508 : RecordTy->getAs<RecordType>(),
2509 : OpLoc, SS))
2510 0: return ExprError();
2511 :
2512 : // Explicit member accesses.
2513 : } else {
2514 : OwningExprResult Result =
2515 : LookupMemberExpr(R, Base, IsArrow, OpLoc,
2516 84: SS, /*ObjCImpDecl*/ DeclPtrTy());
2517 :
0: branch 1 not taken
84: branch 2 taken
2518 84: if (Result.isInvalid()) {
2519 0: Owned(Base);
2520 0: return ExprError();
2521 : }
2522 :
11: branch 1 taken
73: branch 2 taken
2523 84: if (Result.get())
73: branch 5 taken
11: branch 6 taken
2524 11: return move(Result);
2525 : }
2526 :
2527 : return BuildMemberReferenceExpr(ExprArg(*this, Base), BaseType,
2528 : OpLoc, IsArrow, SS, FirstQualifierInScope,
2529 85: R, TemplateArgs);
2530 : }
2531 :
2532 : Sema::OwningExprResult
2533 : Sema::BuildMemberReferenceExpr(ExprArg Base, QualType BaseExprType,
2534 : SourceLocation OpLoc, bool IsArrow,
2535 : const CXXScopeSpec &SS,
2536 : NamedDecl *FirstQualifierInScope,
2537 : LookupResult &R,
2538 2128: const TemplateArgumentListInfo *TemplateArgs) {
2539 2128: Expr *BaseExpr = Base.takeAs<Expr>();
2540 2128: QualType BaseType = BaseExprType;
996: branch 0 taken
1132: branch 1 taken
2541 2128: if (IsArrow) {
0: branch 2 not taken
996: branch 3 taken
2542 996: assert(BaseType->isPointerType());
2543 996: BaseType = BaseType->getAs<PointerType>()->getPointeeType();
2544 : }
2545 :
2546 : NestedNameSpecifier *Qualifier =
2547 2128: static_cast<NestedNameSpecifier*>(SS.getScopeRep());
2548 2128: DeclarationName MemberName = R.getLookupName();
2549 2128: SourceLocation MemberLoc = R.getNameLoc();
2550 :
7: branch 1 taken
2121: branch 2 taken
2551 2128: if (R.isAmbiguous())
2552 7: return ExprError();
2553 :
15: branch 1 taken
2106: branch 2 taken
2554 2121: if (R.empty()) {
2555 : // Rederive where we looked up.
2556 : DeclContext *DC = (SS.isSet()
2557 : ? computeDeclContext(SS, false)
3: branch 1 taken
12: branch 2 taken
12: branch 7 taken
0: branch 8 not taken
2558 15: : BaseType->getAs<RecordType>()->getDecl());
2559 :
2560 : Diag(R.getNameLoc(), diag::err_no_member)
2561 : << MemberName << DC
14: branch 0 taken
1: branch 1 taken
2562 15: << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange());
2563 15: return ExprError();
2564 : }
2565 :
2566 : // Diagnose lookups that find only declarations from a non-base
2567 : // type. This is possible for either qualified lookups (which may
2568 : // have been qualified with an unrelated type) or implicit member
2569 : // expressions (which were found with unqualified lookup and thus
2570 : // may have come from an enclosing scope). Note that it's okay for
2571 : // lookup to find declarations from a non-base type as long as those
2572 : // aren't the ones picked by overload resolution.
2002: branch 1 taken
104: branch 2 taken
1988: branch 3 taken
14: branch 4 taken
507: branch 6 taken
1481: branch 7 taken
493: branch 10 taken
14: branch 11 taken
6: branch 13 taken
605: branch 14 taken
6: branch 15 taken
2100: branch 16 taken
2573 2106: if ((SS.isSet() || !BaseExpr ||
2574 : (isa<CXXThisExpr>(BaseExpr) &&
2575 : cast<CXXThisExpr>(BaseExpr)->isImplicit())) &&
2576 : CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R))
2577 6: return ExprError();
2578 :
2579 : // Construct an unresolved result if we in fact got an unresolved
2580 : // result.
1924: branch 1 taken
176: branch 2 taken
4: branch 4 taken
1920: branch 5 taken
180: branch 6 taken
1920: branch 7 taken
2581 2100: if (R.isOverloadedResult() || R.isUnresolvableResult()) {
2582 : bool Dependent =
2583 : BaseExprType->isDependentType() ||
2584 : R.isUnresolvableResult() ||
170: branch 2 taken
10: branch 3 taken
170: branch 5 taken
0: branch 6 not taken
0: branch 10 not taken
170: branch 11 taken
2585 180: OverloadExpr::ComputeDependence(R.begin(), R.end(), TemplateArgs);
2586 :
2587 : // Suppress any lookup-related diagnostics; we'll do these when we
2588 : // pick a member.
2589 180: R.suppressDiagnostics();
2590 :
2591 : UnresolvedMemberExpr *MemExpr
2592 : = UnresolvedMemberExpr::Create(Context, Dependent,
2593 : R.isUnresolvableResult(),
2594 : BaseExpr, BaseExprType,
2595 : IsArrow, OpLoc,
2596 : Qualifier, SS.getRange(),
2597 : MemberName, MemberLoc,
2598 180: TemplateArgs);
2599 180: MemExpr->addDecls(R.begin(), R.end());
2600 :
2601 180: return Owned(MemExpr);
2602 : }
2603 :
0: branch 1 not taken
1920: branch 2 taken
2604 1920: assert(R.isSingleResult());
2605 1920: NamedDecl *MemberDecl = R.getFoundDecl();
2606 :
2607 : // FIXME: diagnose the presence of template arguments now.
2608 :
2609 : // If the decl being referenced had an error, return an error for this
2610 : // sub-expr without emitting another error, in order to avoid cascading
2611 : // error cases.
0: branch 1 not taken
1920: branch 2 taken
2612 1920: if (MemberDecl->isInvalidDecl())
2613 0: return ExprError();
2614 :
2615 : // Handle the implicit-member-access case.
12: branch 0 taken
1908: branch 1 taken
2616 1920: if (!BaseExpr) {
2617 : // If this is not an instance member, convert to a non-member access.
5: branch 1 taken
7: branch 2 taken
2618 12: if (!IsInstanceMember(MemberDecl))
2619 5: return BuildDeclarationNameExpr(SS, R.getNameLoc(), MemberDecl);
2620 :
2621 7: SourceLocation Loc = R.getNameLoc();
3: branch 2 taken
4: branch 3 taken
2622 7: if (SS.getRange().isValid())
2623 3: Loc = SS.getRange().getBegin();
7: branch 1 taken
0: branch 2 not taken
2624 7: BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true);
2625 : }
2626 :
2627 1915: bool ShouldCheckUse = true;
439: branch 1 taken
1476: branch 2 taken
2628 1915: if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) {
2629 : // Don't diagnose the use of a virtual member function unless it's
2630 : // explicitly qualified.
47: branch 1 taken
392: branch 2 taken
37: branch 4 taken
10: branch 5 taken
37: branch 6 taken
402: branch 7 taken
2631 439: if (MD->isVirtual() && !SS.isSet())
2632 37: ShouldCheckUse = false;
2633 : }
2634 :
2635 : // Check the use of this member.
1878: branch 0 taken
37: branch 1 taken
1: branch 3 taken
1877: branch 4 taken
1: branch 5 taken
1914: branch 6 taken
2636 1915: if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) {
2637 1: Owned(BaseExpr);
2638 1: return ExprError();
2639 : }
2640 :
1444: branch 1 taken
470: branch 2 taken
2641 1914: if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) {
2642 : // We may have found a field within an anonymous union or struct
2643 : // (C++ [class.union]).
31: branch 3 taken
1413: branch 4 taken
31: branch 9 taken
0: branch 10 not taken
31: branch 11 taken
1413: branch 12 taken
2644 1444: if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion() &&
2645 : !BaseType->getAs<RecordType>()->getDecl()->isAnonymousStructOrUnion())
2646 : return BuildAnonymousStructUnionMemberReference(MemberLoc, FD,
2647 31: BaseExpr, OpLoc);
2648 :
2649 : // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref]
2650 1413: QualType MemberType = FD->getType();
12: branch 2 taken
1401: branch 3 taken
2651 1413: if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>())
2652 12: MemberType = Ref->getPointeeType();
2653 : else {
2654 1401: Qualifiers BaseQuals = BaseType.getQualifiers();
2655 1401: BaseQuals.removeObjCGCAttr();
9: branch 1 taken
1392: branch 2 taken
2656 1401: if (FD->isMutable()) BaseQuals.removeConst();
2657 :
2658 : Qualifiers MemberQuals
2659 1401: = Context.getCanonicalType(MemberType).getQualifiers();
2660 :
2661 1401: Qualifiers Combined = BaseQuals + MemberQuals;
149: branch 1 taken
1252: branch 2 taken
2662 1401: if (Combined != MemberQuals)
2663 149: MemberType = Context.getQualifiedType(MemberType, Combined);
2664 : }
2665 :
2666 1413: MarkDeclarationReferenced(MemberLoc, FD);
6: branch 1 taken
1407: branch 2 taken
2667 1413: if (PerformObjectMemberConversion(BaseExpr, FD))
2668 6: return ExprError();
2669 : return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS,
2670 1407: FD, MemberLoc, MemberType));
2671 : }
2672 :
25: branch 1 taken
445: branch 2 taken
2673 470: if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) {
2674 25: MarkDeclarationReferenced(MemberLoc, Var);
2675 : return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS,
2676 : Var, MemberLoc,
2677 25: Var->getType().getNonReferenceType()));
2678 : }
2679 :
438: branch 1 taken
7: branch 2 taken
2680 445: if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) {
2681 438: MarkDeclarationReferenced(MemberLoc, MemberDecl);
2682 : return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS,
2683 : MemberFn, MemberLoc,
2684 438: MemberFn->getType()));
2685 : }
2686 :
5: branch 1 taken
2: branch 2 taken
2687 7: if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) {
2688 5: MarkDeclarationReferenced(MemberLoc, MemberDecl);
2689 : return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS,
2690 5: Enum, MemberLoc, Enum->getType()));
2691 : }
2692 :
2693 2: Owned(BaseExpr);
2694 :
2: branch 1 taken
0: branch 2 not taken
2695 2: if (isa<TypeDecl>(MemberDecl))
2696 : return ExprError(Diag(MemberLoc,diag::err_typecheck_member_reference_type)
2697 2: << MemberName << int(IsArrow));
2698 :
2699 : // We found a declaration kind that we didn't expect. This is a
2700 : // generic error message that tells the user that she can't refer
2701 : // to this member with '.' or '->'.
2702 : return ExprError(Diag(MemberLoc,
2703 : diag::err_typecheck_member_reference_unknown)
2704 0: << MemberName << int(IsArrow));
2705 : }
2706 :
2707 : /// Look up the given member of the given non-type-dependent
2708 : /// expression. This can return in one of two ways:
2709 : /// * If it returns a sentinel null-but-valid result, the caller will
2710 : /// assume that lookup was performed and the results written into
2711 : /// the provided structure. It will take over from there.
2712 : /// * Otherwise, the returned expression will be produced in place of
2713 : /// an ordinary member expression.
2714 : ///
2715 : /// The ObjCImpDecl bit is a gross hack that will need to be properly
2716 : /// fixed for ObjC++.
2717 : Sema::OwningExprResult
2718 : Sema::LookupMemberExpr(LookupResult &R, Expr *&BaseExpr,
2719 : bool &IsArrow, SourceLocation OpLoc,
2720 : const CXXScopeSpec &SS,
2721 2141: DeclPtrTy ObjCImpDecl) {
0: branch 0 not taken
2141: branch 1 taken
2722 2141: assert(BaseExpr && "no base expression");
2723 :
2724 : // Perform default conversions.
2725 2141: DefaultFunctionArrayConversion(BaseExpr);
2726 :
2727 2141: QualType BaseType = BaseExpr->getType();
0: branch 2 not taken
2141: branch 3 taken
2728 2141: assert(!BaseType->isDependentType());
2729 :
2730 2141: DeclarationName MemberName = R.getLookupName();
2731 2141: SourceLocation MemberLoc = R.getNameLoc();
2732 :
2733 : // If the user is trying to apply -> or . to a function pointer
2734 : // type, it's probably because they forgot parentheses to call that
2735 : // function. Suggest the addition of those parentheses, build the
2736 : // call, and continue on.
455: branch 2 taken
1686: branch 3 taken
2737 2141: if (const PointerType *Ptr = BaseType->getAs<PointerType>()) {
1: branch 0 taken
454: branch 1 taken
2738 455: if (const FunctionProtoType *Fun
2739 455: = Ptr->getPointeeType()->getAs<FunctionProtoType>()) {
2740 1: QualType ResultTy = Fun->getResultType();
1: branch 1 taken
0: branch 2 not taken
0: branch 3 not taken
1: branch 4 taken
0: branch 7 not taken
0: branch 8 not taken
1: branch 9 taken
0: branch 10 not taken
1: branch 13 taken
0: branch 14 not taken
1: branch 20 taken
0: branch 21 not taken
1: branch 22 taken
0: branch 23 not taken
2741 1: if (Fun->getNumArgs() == 0 &&
2742 : ((!IsArrow && ResultTy->isRecordType()) ||
2743 : (IsArrow && ResultTy->isPointerType() &&
2744 : ResultTy->getAs<PointerType>()->getPointeeType()
2745 : ->isRecordType()))) {
2746 1: SourceLocation Loc = PP.getLocForEndOfToken(BaseExpr->getLocEnd());
2747 : Diag(Loc, diag::err_member_reference_needs_call)
2748 : << QualType(Fun, 0)
2749 1: << CodeModificationHint::CreateInsertion(Loc, "()");
2750 :
2751 : OwningExprResult NewBase
2752 : = ActOnCallExpr(0, ExprArg(*this, BaseExpr), Loc,
2753 1: MultiExprArg(*this, 0, 0), 0, Loc);
0: branch 1 not taken
1: branch 2 taken
2754 1: if (NewBase.isInvalid())
2755 0: return ExprError();
2756 :
2757 1: BaseExpr = NewBase.takeAs<Expr>();
2758 1: DefaultFunctionArrayConversion(BaseExpr);
1: branch 2 taken
0: branch 3 not taken
2759 1: BaseType = BaseExpr->getType();
2760 : }
2761 : }
2762 : }
2763 :
2764 : // If this is an Objective-C pseudo-builtin and a definition is provided then
2765 : // use that.
13: branch 2 taken
2128: branch 3 taken
2766 2141: if (BaseType->isObjCIdType()) {
8: branch 0 taken
5: branch 1 taken
2767 13: if (IsArrow) {
2768 : // Handle the following exceptional case PObj->isa.
8: branch 0 taken
0: branch 1 not taken
2769 8: if (const ObjCObjectPointerType *OPT =
2770 8: BaseType->getAs<ObjCObjectPointerType>()) {
8: branch 3 taken
0: branch 4 not taken
8: branch 7 taken
0: branch 8 not taken
8: branch 9 taken
0: branch 10 not taken
2771 8: if (OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCId) &&
2772 : MemberName.getAsIdentifierInfo()->isStr("isa"))
2773 : return Owned(new (Context) ObjCIsaExpr(BaseExpr, true, MemberLoc,
8: branch 2 taken
0: branch 3 not taken
2774 8: Context.getObjCClassType()));
2775 : }
2776 : }
2777 : // We have an 'id' type. Rather than fall through, we check if this
2778 : // is a reference to 'isa'.
0: branch 1 not taken
5: branch 2 taken
2779 5: if (BaseType != Context.ObjCIdRedefinitionType) {
2780 0: BaseType = Context.ObjCIdRedefinitionType;
2781 0: ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast);
2782 : }
2783 : }
2784 :
2785 : // If this is an Objective-C pseudo-builtin and a definition is provided then
2786 : // use that.
2: branch 1 taken
2131: branch 2 taken
2787 2133: if (Context.isObjCSelType(BaseType)) {
2788 : // We have an 'SEL' type. Rather than fall through, we check if this
2789 : // is a reference to 'sel_id'.
2: branch 1 taken
0: branch 2 not taken
2790 2: if (BaseType != Context.ObjCSelRedefinitionType) {
2791 2: BaseType = Context.ObjCSelRedefinitionType;
2792 2: ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast);
2793 : }
2794 : }
2795 :
2133: branch 1 taken
0: branch 2 not taken
2796 2133: assert(!BaseType.isNull() && "no type for member expression");
2797 :
2798 : // Handle properties on ObjC 'Class' types.
1497: branch 0 taken
636: branch 1 taken
6: branch 4 taken
1491: branch 5 taken
6: branch 6 taken
2127: branch 7 taken
2799 3630: if (!IsArrow && BaseType->isObjCClassType()) {
2800 : // Also must look for a getter name which uses property syntax.
2801 6: IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
2802 6: Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
6: branch 1 taken
0: branch 2 not taken
2803 6: if (ObjCMethodDecl *MD = getCurMethodDecl()) {
2804 6: ObjCInterfaceDecl *IFace = MD->getClassInterface();
2805 : ObjCMethodDecl *Getter;
2806 : // FIXME: need to also look locally in the implementation.
6: branch 1 taken
0: branch 2 not taken
2807 6: if ((Getter = IFace->lookupClassMethod(Sel))) {
2808 : // Check the use of this method.
0: branch 1 not taken
6: branch 2 taken
2809 6: if (DiagnoseUseOfDecl(Getter, MemberLoc))
2810 0: return ExprError();
2811 : }
2812 : // If we found a getter then this may be a valid dot-reference, we
2813 : // will look for the matching setter, in case it is needed.
2814 : Selector SetterSel =
2815 : SelectorTable::constructSetterName(PP.getIdentifierTable(),
2816 6: PP.getSelectorTable(), Member);
2817 6: ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
2: branch 0 taken
4: branch 1 taken
2818 6: if (!Setter) {
2819 : // If this reference is in an @implementation, also check for 'private'
2820 : // methods.
2821 2: Setter = IFace->lookupPrivateInstanceMethod(SetterSel);
2822 : }
2823 : // Look through local category implementations associated with the class.
2: branch 0 taken
4: branch 1 taken
2824 6: if (!Setter)
2825 2: Setter = IFace->getCategoryClassMethod(SetterSel);
2826 :
4: branch 0 taken
2: branch 1 taken
0: branch 3 not taken
4: branch 4 taken
0: branch 5 not taken
6: branch 6 taken
2827 6: if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
2828 0: return ExprError();
2829 :
0: branch 0 not taken
6: branch 1 taken
6: branch 2 taken
6: branch 3 taken
2830 6: if (Getter || Setter) {
2831 6: QualType PType;
2832 :
6: branch 0 taken
0: branch 1 not taken
2833 6: if (Getter)
2834 6: PType = Getter->getResultType();
2835 : else
2836 : // Get the expression type from Setter's incoming parameter.
2837 0: PType = (*(Setter->param_end() -1))->getType();
2838 : // FIXME: we must check that the setter has property type.
2839 : return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter,
2840 : PType,
6: branch 1 taken
0: branch 2 not taken
2841 6: Setter, MemberLoc, BaseExpr));
2842 : }
2843 : return ExprError(Diag(MemberLoc, diag::err_property_not_found)
2844 0: << MemberName << BaseType);
2845 : }
2846 : }
2847 :
1: branch 2 taken
2126: branch 3 taken
1: branch 5 taken
0: branch 6 not taken
1: branch 7 taken
2126: branch 8 taken
2848 2127: if (BaseType->isObjCClassType() &&
2849 : BaseType != Context.ObjCClassRedefinitionType) {
2850 1: BaseType = Context.ObjCClassRedefinitionType;
2851 1: ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast);
2852 : }
2853 :
636: branch 0 taken
1491: branch 1 taken
2854 2127: if (IsArrow) {
454: branch 2 taken
182: branch 3 taken
2855 636: if (const PointerType *PT = BaseType->getAs<PointerType>())
2856 454: BaseType = PT->getPointeeType();
0: branch 2 not taken
182: branch 3 taken
2857 182: else if (BaseType->isObjCObjectPointerType())
2858 : ;
0: branch 2 not taken
0: branch 3 not taken
2859 0: else if (BaseType->isRecordType()) {
2860 : // Recover from arrow accesses to records, e.g