Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/ExprObjC.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- ExprObjC.cpp - (ObjC) Expression AST Node Implementation -----------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements the subclesses of Expr class declared in ExprObjC.h
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/AST/ExprObjC.h"
14
#include "clang/AST/ASTContext.h"
15
#include "clang/AST/SelectorLocationsKind.h"
16
#include "clang/AST/Type.h"
17
#include "clang/AST/TypeLoc.h"
18
#include "llvm/ADT/SmallVector.h"
19
#include "llvm/Support/ErrorHandling.h"
20
#include <algorithm>
21
#include <cassert>
22
#include <cstdint>
23
24
using namespace clang;
25
26
ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements, QualType T,
27
                                   ObjCMethodDecl *Method, SourceRange SR)
28
    : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
29
           false, false),
30
282
      NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method) {
31
282
  Expr **SaveElements = getElements();
32
755
  for (unsigned I = 0, N = Elements.size(); I != N; 
++I473
) {
33
473
    if (Elements[I]->isTypeDependent() || 
Elements[I]->isValueDependent()470
)
34
3
      ExprBits.ValueDependent = true;
35
473
    if (Elements[I]->isInstantiationDependent())
36
3
      ExprBits.InstantiationDependent = true;
37
473
    if (Elements[I]->containsUnexpandedParameterPack())
38
1
      ExprBits.ContainsUnexpandedParameterPack = true;
39
473
40
473
    SaveElements[I] = Elements[I];
41
473
  }
42
282
}
43
44
ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C,
45
                                           ArrayRef<Expr *> Elements,
46
                                           QualType T, ObjCMethodDecl *Method,
47
282
                                           SourceRange SR) {
48
282
  void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Elements.size()));
49
282
  return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
50
282
}
51
52
ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C,
53
4
                                                unsigned NumElements) {
54
4
  void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumElements));
55
4
  return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
56
4
}
57
58
ObjCDictionaryLiteral::ObjCDictionaryLiteral(ArrayRef<ObjCDictionaryElement> VK,
59
                                             bool HasPackExpansions, QualType T,
60
                                             ObjCMethodDecl *method,
61
                                             SourceRange SR)
62
    : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
63
           false, false),
64
      NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
65
207
      DictWithObjectsMethod(method) {
66
207
  KeyValuePair *KeyValues = getTrailingObjects<KeyValuePair>();
67
207
  ExpansionData *Expansions =
68
207
      HasPackExpansions ? 
getTrailingObjects<ExpansionData>()2
:
nullptr205
;
69
493
  for (unsigned I = 0; I < NumElements; 
I++286
) {
70
286
    if (VK[I].Key->isTypeDependent() || 
VK[I].Key->isValueDependent()280
||
71
286
        
VK[I].Value->isTypeDependent()280
||
VK[I].Value->isValueDependent()279
)
72
7
      ExprBits.ValueDependent = true;
73
286
    if (VK[I].Key->isInstantiationDependent() ||
74
286
        
VK[I].Value->isInstantiationDependent()280
)
75
7
      ExprBits.InstantiationDependent = true;
76
286
    if (VK[I].EllipsisLoc.isInvalid() &&
77
286
        
(282
VK[I].Key->containsUnexpandedParameterPack()282
||
78
282
         
VK[I].Value->containsUnexpandedParameterPack()281
))
79
1
      ExprBits.ContainsUnexpandedParameterPack = true;
80
286
81
286
    KeyValues[I].Key = VK[I].Key;
82
286
    KeyValues[I].Value = VK[I].Value;
83
286
    if (Expansions) {
84
4
      Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
85
4
      if (VK[I].NumExpansions)
86
0
        Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
87
4
      else
88
4
        Expansions[I].NumExpansionsPlusOne = 0;
89
4
    }
90
286
  }
91
207
}
92
93
ObjCDictionaryLiteral *
94
ObjCDictionaryLiteral::Create(const ASTContext &C,
95
                              ArrayRef<ObjCDictionaryElement> VK,
96
                              bool HasPackExpansions, QualType T,
97
207
                              ObjCMethodDecl *method, SourceRange SR) {
98
207
  void *Mem = C.Allocate(totalSizeToAlloc<KeyValuePair, ExpansionData>(
99
207
      VK.size(), HasPackExpansions ? 
VK.size()2
:
0205
));
100
207
  return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
101
207
}
102
103
ObjCDictionaryLiteral *
104
ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements,
105
7
                                   bool HasPackExpansions) {
106
7
  void *Mem = C.Allocate(totalSizeToAlloc<KeyValuePair, ExpansionData>(
107
7
      NumElements, HasPackExpansions ? 
NumElements3
:
04
));
108
7
  return new (Mem)
109
7
      ObjCDictionaryLiteral(EmptyShell(), NumElements, HasPackExpansions);
110
7
}
111
112
3.52k
QualType ObjCPropertyRefExpr::getReceiverType(const ASTContext &ctx) const {
113
3.52k
  if (isClassReceiver())
114
182
    return ctx.getObjCInterfaceType(getClassReceiver());
115
3.34k
116
3.34k
  if (isSuperReceiver())
117
121
    return getSuperReceiverType();
118
3.22k
119
3.22k
  return getBase()->getType();
120
3.22k
}
121
122
ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK,
123
                                 SourceLocation LBracLoc,
124
                                 SourceLocation SuperLoc, bool IsInstanceSuper,
125
                                 QualType SuperType, Selector Sel,
126
                                 ArrayRef<SourceLocation> SelLocs,
127
                                 SelectorLocationsKind SelLocsK,
128
                                 ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
129
                                 SourceLocation RBracLoc, bool isImplicit)
130
    : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
131
           /*TypeDependent=*/false, /*ValueDependent=*/false,
132
           /*InstantiationDependent=*/false,
133
           /*ContainsUnexpandedParameterPack=*/false),
134
      SelectorOrMethod(
135
          reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())),
136
      Kind(IsInstanceSuper ? SuperInstance : SuperClass),
137
      HasMethod(Method != nullptr), IsDelegateInitCall(false),
138
      IsImplicit(isImplicit), SuperLoc(SuperLoc), LBracLoc(LBracLoc),
139
1.24k
      RBracLoc(RBracLoc) {
140
1.24k
  initArgsAndSelLocs(Args, SelLocs, SelLocsK);
141
1.24k
  setReceiverPointer(SuperType.getAsOpaquePtr());
142
1.24k
}
143
144
ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK,
145
                                 SourceLocation LBracLoc,
146
                                 TypeSourceInfo *Receiver, Selector Sel,
147
                                 ArrayRef<SourceLocation> SelLocs,
148
                                 SelectorLocationsKind SelLocsK,
149
                                 ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
150
                                 SourceLocation RBracLoc, bool isImplicit)
151
    : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
152
           T->isDependentType(), T->isInstantiationDependentType(),
153
           T->containsUnexpandedParameterPack()),
154
      SelectorOrMethod(
155
          reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())),
156
      Kind(Class), HasMethod(Method != nullptr), IsDelegateInitCall(false),
157
7.56k
      IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc) {
158
7.56k
  initArgsAndSelLocs(Args, SelLocs, SelLocsK);
159
7.56k
  setReceiverPointer(Receiver);
160
7.56k
}
161
162
ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK,
163
                                 SourceLocation LBracLoc, Expr *Receiver,
164
                                 Selector Sel, ArrayRef<SourceLocation> SelLocs,
165
                                 SelectorLocationsKind SelLocsK,
166
                                 ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
167
                                 SourceLocation RBracLoc, bool isImplicit)
168
    : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
169
           Receiver->isTypeDependent(), Receiver->isTypeDependent(),
170
           Receiver->isInstantiationDependent(),
171
           Receiver->containsUnexpandedParameterPack()),
172
      SelectorOrMethod(
173
          reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())),
174
      Kind(Instance), HasMethod(Method != nullptr), IsDelegateInitCall(false),
175
16.7k
      IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc) {
176
16.7k
  initArgsAndSelLocs(Args, SelLocs, SelLocsK);
177
16.7k
  setReceiverPointer(Receiver);
178
16.7k
}
179
180
void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
181
                                         ArrayRef<SourceLocation> SelLocs,
182
25.5k
                                         SelectorLocationsKind SelLocsK) {
183
25.5k
  setNumArgs(Args.size());
184
25.5k
  Expr **MyArgs = getArgs();
185
44.7k
  for (unsigned I = 0; I != Args.size(); 
++I19.2k
) {
186
19.2k
    if (Args[I]->isTypeDependent())
187
17
      ExprBits.TypeDependent = true;
188
19.2k
    if (Args[I]->isValueDependent())
189
25
      ExprBits.ValueDependent = true;
190
19.2k
    if (Args[I]->isInstantiationDependent())
191
26
      ExprBits.InstantiationDependent = true;
192
19.2k
    if (Args[I]->containsUnexpandedParameterPack())
193
0
      ExprBits.ContainsUnexpandedParameterPack = true;
194
19.2k
195
19.2k
    MyArgs[I] = Args[I];
196
19.2k
  }
197
25.5k
198
25.5k
  SelLocsKind = SelLocsK;
199
25.5k
  if (!isImplicit()) {
200
22.7k
    if (SelLocsK == SelLoc_NonStandard)
201
1.52k
      std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
202
22.7k
  }
203
25.5k
}
204
205
ObjCMessageExpr *
206
ObjCMessageExpr::Create(const ASTContext &Context, QualType T, ExprValueKind VK,
207
                        SourceLocation LBracLoc, SourceLocation SuperLoc,
208
                        bool IsInstanceSuper, QualType SuperType, Selector Sel,
209
                        ArrayRef<SourceLocation> SelLocs,
210
                        ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
211
1.24k
                        SourceLocation RBracLoc, bool isImplicit) {
212
1.24k
  assert((!SelLocs.empty() || isImplicit) &&
213
1.24k
         "No selector locs for non-implicit message");
214
1.24k
  ObjCMessageExpr *Mem;
215
1.24k
  SelectorLocationsKind SelLocsK = SelectorLocationsKind();
216
1.24k
  if (isImplicit)
217
74
    Mem = alloc(Context, Args.size(), 0);
218
1.17k
  else
219
1.17k
    Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
220
1.24k
  return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
221
1.24k
                                   SuperType, Sel, SelLocs, SelLocsK, Method,
222
1.24k
                                   Args, RBracLoc, isImplicit);
223
1.24k
}
224
225
ObjCMessageExpr *
226
ObjCMessageExpr::Create(const ASTContext &Context, QualType T, ExprValueKind VK,
227
                        SourceLocation LBracLoc, TypeSourceInfo *Receiver,
228
                        Selector Sel, ArrayRef<SourceLocation> SelLocs,
229
                        ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
230
7.56k
                        SourceLocation RBracLoc, bool isImplicit) {
231
7.56k
  assert((!SelLocs.empty() || isImplicit) &&
232
7.56k
         "No selector locs for non-implicit message");
233
7.56k
  ObjCMessageExpr *Mem;
234
7.56k
  SelectorLocationsKind SelLocsK = SelectorLocationsKind();
235
7.56k
  if (isImplicit)
236
197
    Mem = alloc(Context, Args.size(), 0);
237
7.36k
  else
238
7.36k
    Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
239
7.56k
  return new (Mem)
240
7.56k
      ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLocs, SelLocsK, Method,
241
7.56k
                      Args, RBracLoc, isImplicit);
242
7.56k
}
243
244
ObjCMessageExpr *
245
ObjCMessageExpr::Create(const ASTContext &Context, QualType T, ExprValueKind VK,
246
                        SourceLocation LBracLoc, Expr *Receiver, Selector Sel,
247
                        ArrayRef<SourceLocation> SelLocs,
248
                        ObjCMethodDecl *Method, ArrayRef<Expr *> Args,
249
16.7k
                        SourceLocation RBracLoc, bool isImplicit) {
250
16.7k
  assert((!SelLocs.empty() || isImplicit) &&
251
16.7k
         "No selector locs for non-implicit message");
252
16.7k
  ObjCMessageExpr *Mem;
253
16.7k
  SelectorLocationsKind SelLocsK = SelectorLocationsKind();
254
16.7k
  if (isImplicit)
255
2.46k
    Mem = alloc(Context, Args.size(), 0);
256
14.2k
  else
257
14.2k
    Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
258
16.7k
  return new (Mem)
259
16.7k
      ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLocs, SelLocsK, Method,
260
16.7k
                      Args, RBracLoc, isImplicit);
261
16.7k
}
262
263
ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context,
264
                                              unsigned NumArgs,
265
40
                                              unsigned NumStoredSelLocs) {
266
40
  ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
267
40
  return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
268
40
}
269
270
ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C,
271
                                        ArrayRef<Expr *> Args,
272
                                        SourceLocation RBraceLoc,
273
                                        ArrayRef<SourceLocation> SelLocs,
274
                                        Selector Sel,
275
22.7k
                                        SelectorLocationsKind &SelLocsK) {
276
22.7k
  SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
277
22.7k
  unsigned NumStoredSelLocs =
278
22.7k
      (SelLocsK == SelLoc_NonStandard) ? 
SelLocs.size()1.52k
:
021.2k
;
279
22.7k
  return alloc(C, Args.size(), NumStoredSelLocs);
280
22.7k
}
281
282
ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C, unsigned NumArgs,
283
25.5k
                                        unsigned NumStoredSelLocs) {
284
25.5k
  return (ObjCMessageExpr *)C.Allocate(
285
25.5k
      totalSizeToAlloc<void *, SourceLocation>(NumArgs + 1, NumStoredSelLocs),
286
25.5k
      alignof(ObjCMessageExpr));
287
25.5k
}
288
289
void ObjCMessageExpr::getSelectorLocs(
290
659
    SmallVectorImpl<SourceLocation> &SelLocs) const {
291
1.29k
  for (unsigned i = 0, e = getNumSelectorLocs(); i != e; 
++i640
)
292
640
    SelLocs.push_back(getSelectorLoc(i));
293
659
}
294
295
296
9
QualType ObjCMessageExpr::getCallReturnType(ASTContext &Ctx) const {
297
9
  if (const ObjCMethodDecl *MD = getMethodDecl()) {
298
9
    QualType QT = MD->getReturnType();
299
9
    if (QT == Ctx.getObjCInstanceType()) {
300
0
      // instancetype corresponds to expression types.
301
0
      return getType();
302
0
    }
303
9
    return QT;
304
9
  }
305
0
306
0
  // Expression type might be different from an expected call return type,
307
0
  // as expression type would never be a reference even if call returns a
308
0
  // reference. Reconstruct the original expression type.
309
0
  QualType QT = getType();
310
0
  switch (getValueKind()) {
311
0
  case VK_LValue:
312
0
    return Ctx.getLValueReferenceType(QT);
313
0
  case VK_XValue:
314
0
    return Ctx.getRValueReferenceType(QT);
315
0
  case VK_RValue:
316
0
    return QT;
317
0
  }
318
0
  llvm_unreachable("Unsupported ExprValueKind");
319
0
}
320
321
69
SourceRange ObjCMessageExpr::getReceiverRange() const {
322
69
  switch (getReceiverKind()) {
323
69
  case Instance:
324
69
    return getInstanceReceiver()->getSourceRange();
325
0
326
0
  case Class:
327
0
    return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
328
0
329
0
  case SuperInstance:
330
0
  case SuperClass:
331
0
    return getSuperLoc();
332
0
  }
333
0
334
0
  llvm_unreachable("Invalid ReceiverKind!");
335
0
}
336
337
38.7k
Selector ObjCMessageExpr::getSelector() const {
338
38.7k
  if (HasMethod)
339
37.9k
    return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
340
37.9k
        ->getSelector();
341
772
  return Selector(SelectorOrMethod);
342
772
}
343
344
92.1k
QualType ObjCMessageExpr::getReceiverType() const {
345
92.1k
  switch (getReceiverKind()) {
346
80.4k
  case Instance:
347
80.4k
    return getInstanceReceiver()->getType();
348
7.15k
  case Class:
349
7.15k
    return getClassReceiver();
350
4.52k
  case SuperInstance:
351
4.52k
  case SuperClass:
352
4.52k
    return getSuperType();
353
0
  }
354
0
355
0
  llvm_unreachable("unexpected receiver kind");
356
0
}
357
358
91.8k
ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
359
91.8k
  QualType T = getReceiverType();
360
91.8k
361
91.8k
  if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>())
362
84.6k
    return Ptr->getInterfaceDecl();
363
7.27k
364
7.27k
  if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>())
365
7.16k
    return Ty->getInterface();
366
108
367
108
  return nullptr;
368
108
}
369
370
86.3k
Stmt::child_range ObjCMessageExpr::children() {
371
86.3k
  Stmt **begin;
372
86.3k
  if (getReceiverKind() == Instance)
373
58.0k
    begin = reinterpret_cast<Stmt **>(getTrailingObjects<void *>());
374
28.3k
  else
375
28.3k
    begin = reinterpret_cast<Stmt **>(getArgs());
376
86.3k
  return child_range(begin,
377
86.3k
                     reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
378
86.3k
}
379
380
0
Stmt::const_child_range ObjCMessageExpr::children() const {
381
0
  auto Children = const_cast<ObjCMessageExpr *>(this)->children();
382
0
  return const_child_range(Children.begin(), Children.end());
383
0
}
384
385
4
StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
386
4
  switch (getBridgeKind()) {
387
4
  case OBC_Bridge:
388
4
    return "__bridge";
389
0
  case OBC_BridgeTransfer:
390
0
    return "__bridge_transfer";
391
0
  case OBC_BridgeRetained:
392
0
    return "__bridge_retained";
393
0
  }
394
0
395
0
  llvm_unreachable("Invalid BridgeKind!");
396
0
}