Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit f2a3c7b

Browse files
committed
[TrailingObjects] Convert classes in ExprObjC.h
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@256659 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 34d3857 commit f2a3c7b

File tree

4 files changed

+99
-122
lines changed

4 files changed

+99
-122
lines changed

Diff for: include/clang/AST/ExprObjC.h

+81-86
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,17 @@ class ObjCBoxedExpr : public Expr {
141141

142142
/// ObjCArrayLiteral - used for objective-c array containers; as in:
143143
/// @[@"Hello", NSApp, [NSNumber numberWithInt:42]];
144-
class ObjCArrayLiteral : public Expr {
144+
class ObjCArrayLiteral final
145+
: public Expr,
146+
private llvm::TrailingObjects<ObjCArrayLiteral, Expr *> {
145147
unsigned NumElements;
146148
SourceRange Range;
147149
ObjCMethodDecl *ArrayWithObjectsMethod;
148-
150+
149151
ObjCArrayLiteral(ArrayRef<Expr *> Elements,
150152
QualType T, ObjCMethodDecl * Method,
151153
SourceRange SR);
152-
154+
153155
explicit ObjCArrayLiteral(EmptyShell Empty, unsigned NumElements)
154156
: Expr(ObjCArrayLiteralClass, Empty), NumElements(NumElements) {}
155157

@@ -171,11 +173,11 @@ class ObjCArrayLiteral : public Expr {
171173
}
172174

173175
/// \brief Retrieve elements of array of literals.
174-
Expr **getElements() { return reinterpret_cast<Expr **>(this + 1); }
176+
Expr **getElements() { return getTrailingObjects<Expr *>(); }
175177

176178
/// \brief Retrieve elements of array of literals.
177-
const Expr * const *getElements() const {
178-
return reinterpret_cast<const Expr * const*>(this + 1);
179+
const Expr * const *getElements() const {
180+
return getTrailingObjects<Expr *>();
179181
}
180182

181183
/// getNumElements - Return number of elements of objective-c array literal.
@@ -196,11 +198,12 @@ class ObjCArrayLiteral : public Expr {
196198
}
197199

198200
// Iterators
199-
child_range children() {
200-
return child_range((Stmt **)getElements(),
201-
(Stmt **)getElements() + NumElements);
201+
child_range children() {
202+
return child_range(reinterpret_cast<Stmt **>(getElements()),
203+
reinterpret_cast<Stmt **>(getElements()) + NumElements);
202204
}
203-
205+
206+
friend TrailingObjects;
204207
friend class ASTStmtReader;
205208
};
206209

@@ -230,32 +233,35 @@ template <> struct isPodLike<clang::ObjCDictionaryElement> : std::true_type {};
230233
}
231234

232235
namespace clang {
233-
/// ObjCDictionaryLiteral - AST node to represent objective-c dictionary
234-
/// literals; as in: @{@"name" : NSUserName(), @"date" : [NSDate date] };
235-
class ObjCDictionaryLiteral : public Expr {
236-
/// \brief Key/value pair used to store the key and value of a given element.
237-
///
238-
/// Objects of this type are stored directly after the expression.
239-
struct KeyValuePair {
240-
Expr *Key;
241-
Expr *Value;
242-
};
243-
244-
/// \brief Data that describes an element that is a pack expansion, used if any
245-
/// of the elements in the dictionary literal are pack expansions.
246-
struct ExpansionData {
247-
/// \brief The location of the ellipsis, if this element is a pack
248-
/// expansion.
249-
SourceLocation EllipsisLoc;
250-
251-
/// \brief If non-zero, the number of elements that this pack
252-
/// expansion will expand to (+1).
253-
unsigned NumExpansionsPlusOne;
254-
};
236+
/// \brief Internal struct for storing Key/value pair.
237+
struct ObjCDictionaryLiteral_KeyValuePair {
238+
Expr *Key;
239+
Expr *Value;
240+
};
241+
242+
/// \brief Internal struct to describes an element that is a pack
243+
/// expansion, used if any of the elements in the dictionary literal
244+
/// are pack expansions.
245+
struct ObjCDictionaryLiteral_ExpansionData {
246+
/// \brief The location of the ellipsis, if this element is a pack
247+
/// expansion.
248+
SourceLocation EllipsisLoc;
255249

250+
/// \brief If non-zero, the number of elements that this pack
251+
/// expansion will expand to (+1).
252+
unsigned NumExpansionsPlusOne;
253+
};
254+
255+
/// ObjCDictionaryLiteral - AST node to represent objective-c dictionary
256+
/// literals; as in: @{@"name" : NSUserName(), @"date" : [NSDate date] };
257+
class ObjCDictionaryLiteral final
258+
: public Expr,
259+
private llvm::TrailingObjects<ObjCDictionaryLiteral,
260+
ObjCDictionaryLiteral_KeyValuePair,
261+
ObjCDictionaryLiteral_ExpansionData> {
256262
/// \brief The number of elements in this dictionary literal.
257263
unsigned NumElements : 31;
258-
264+
259265
/// \brief Determine whether this dictionary literal has any pack expansions.
260266
///
261267
/// If the dictionary literal has pack expansions, then there will
@@ -264,10 +270,17 @@ class ObjCDictionaryLiteral : public Expr {
264270
/// any) and number of elements in the expansion (if known). If
265271
/// there are no pack expansions, we optimize away this storage.
266272
unsigned HasPackExpansions : 1;
267-
273+
268274
SourceRange Range;
269275
ObjCMethodDecl *DictWithObjectsMethod;
270-
276+
277+
typedef ObjCDictionaryLiteral_KeyValuePair KeyValuePair;
278+
typedef ObjCDictionaryLiteral_ExpansionData ExpansionData;
279+
280+
size_t numTrailingObjects(OverloadToken<KeyValuePair>) const {
281+
return NumElements;
282+
}
283+
271284
ObjCDictionaryLiteral(ArrayRef<ObjCDictionaryElement> VK,
272285
bool HasPackExpansions,
273286
QualType T, ObjCMethodDecl *method,
@@ -278,28 +291,6 @@ class ObjCDictionaryLiteral : public Expr {
278291
: Expr(ObjCDictionaryLiteralClass, Empty), NumElements(NumElements),
279292
HasPackExpansions(HasPackExpansions) {}
280293

281-
KeyValuePair *getKeyValues() {
282-
return reinterpret_cast<KeyValuePair *>(this + 1);
283-
}
284-
285-
const KeyValuePair *getKeyValues() const {
286-
return reinterpret_cast<const KeyValuePair *>(this + 1);
287-
}
288-
289-
ExpansionData *getExpansionData() {
290-
if (!HasPackExpansions)
291-
return nullptr;
292-
293-
return reinterpret_cast<ExpansionData *>(getKeyValues() + NumElements);
294-
}
295-
296-
const ExpansionData *getExpansionData() const {
297-
if (!HasPackExpansions)
298-
return nullptr;
299-
300-
return reinterpret_cast<const ExpansionData *>(getKeyValues()+NumElements);
301-
}
302-
303294
public:
304295
static ObjCDictionaryLiteral *Create(const ASTContext &C,
305296
ArrayRef<ObjCDictionaryElement> VK,
@@ -317,10 +308,11 @@ class ObjCDictionaryLiteral : public Expr {
317308

318309
ObjCDictionaryElement getKeyValueElement(unsigned Index) const {
319310
assert((Index < NumElements) && "Arg access out of range!");
320-
const KeyValuePair &KV = getKeyValues()[Index];
311+
const KeyValuePair &KV = getTrailingObjects<KeyValuePair>()[Index];
321312
ObjCDictionaryElement Result = { KV.Key, KV.Value, SourceLocation(), None };
322313
if (HasPackExpansions) {
323-
const ExpansionData &Expansion = getExpansionData()[Index];
314+
const ExpansionData &Expansion =
315+
getTrailingObjects<ExpansionData>()[Index];
324316
Result.EllipsisLoc = Expansion.EllipsisLoc;
325317
if (Expansion.NumExpansionsPlusOne > 0)
326318
Result.NumExpansions = Expansion.NumExpansionsPlusOne - 1;
@@ -340,17 +332,20 @@ class ObjCDictionaryLiteral : public Expr {
340332
}
341333

342334
// Iterators
343-
child_range children() {
335+
child_range children() {
344336
// Note: we're taking advantage of the layout of the KeyValuePair struct
345337
// here. If that struct changes, this code will need to change as well.
346338
static_assert(sizeof(KeyValuePair) == sizeof(Stmt *) * 2,
347339
"KeyValuePair is expected size");
348-
return child_range(reinterpret_cast<Stmt **>(this + 1),
349-
reinterpret_cast<Stmt **>(this + 1) + NumElements * 2);
340+
return child_range(
341+
reinterpret_cast<Stmt **>(getTrailingObjects<KeyValuePair>()),
342+
reinterpret_cast<Stmt **>(getTrailingObjects<KeyValuePair>()) +
343+
NumElements * 2);
350344
}
351345

352346
friend class ASTStmtReader;
353347
friend class ASTStmtWriter;
348+
friend TrailingObjects;
354349
};
355350

356351

@@ -797,13 +792,6 @@ class ObjCSubscriptRefExpr : public Expr {
797792
explicit ObjCSubscriptRefExpr(EmptyShell Empty)
798793
: Expr(ObjCSubscriptRefExprClass, Empty) {}
799794

800-
static ObjCSubscriptRefExpr *Create(const ASTContext &C,
801-
Expr *base,
802-
Expr *key, QualType T,
803-
ObjCMethodDecl *getMethod,
804-
ObjCMethodDecl *setMethod,
805-
SourceLocation RB);
806-
807795
SourceLocation getRBracket() const { return RBracket; }
808796
void setRBracket(SourceLocation RB) { RBracket = RB; }
809797

@@ -865,7 +853,13 @@ class ObjCSubscriptRefExpr : public Expr {
865853
/// All four kinds of message sends are modeled by the ObjCMessageExpr
866854
/// class, and can be distinguished via \c getReceiverKind(). Example:
867855
///
868-
class ObjCMessageExpr : public Expr {
856+
/// The "void *" trailing objects are actually ONE void * (the
857+
/// receiver pointer), and NumArgs Expr *. But due to the
858+
/// implementation of children(), these must be together contiguously.
859+
860+
class ObjCMessageExpr final
861+
: public Expr,
862+
private llvm::TrailingObjects<ObjCMessageExpr, void *, SourceLocation> {
869863
/// \brief Stores either the selector that this message is sending
870864
/// to (when \c HasMethod is zero) or an \c ObjCMethodDecl pointer
871865
/// referring to the method that we type-checked against.
@@ -877,11 +871,6 @@ class ObjCMessageExpr : public Expr {
877871
/// including the receiver.
878872
unsigned NumArgs : NumArgsBitWidth;
879873

880-
void setNumArgs(unsigned Num) {
881-
assert((Num >> NumArgsBitWidth) == 0 && "Num of args is out of range!");
882-
NumArgs = Num;
883-
}
884-
885874
/// \brief The kind of message send this is, which is one of the
886875
/// ReceiverKind values.
887876
///
@@ -915,6 +904,13 @@ class ObjCMessageExpr : public Expr {
915904
/// brackets ('[' and ']', respectively).
916905
SourceLocation LBracLoc, RBracLoc;
917906

907+
size_t numTrailingObjects(OverloadToken<void *>) const { return NumArgs + 1; }
908+
909+
void setNumArgs(unsigned Num) {
910+
assert((Num >> NumArgsBitWidth) == 0 && "Num of args is out of range!");
911+
NumArgs = Num;
912+
}
913+
918914
ObjCMessageExpr(EmptyShell Empty, unsigned NumArgs)
919915
: Expr(ObjCMessageExprClass, Empty), SelectorOrMethod(0), Kind(0),
920916
HasMethod(0), IsDelegateInitCall(0), IsImplicit(0), SelLocsKind(0) {
@@ -959,14 +955,11 @@ class ObjCMessageExpr : public Expr {
959955
SelectorLocationsKind SelLocsK);
960956

961957
/// \brief Retrieve the pointer value of the message receiver.
962-
void *getReceiverPointer() const {
963-
return *const_cast<void **>(
964-
reinterpret_cast<const void * const*>(this + 1));
965-
}
958+
void *getReceiverPointer() const { return *getTrailingObjects<void *>(); }
966959

967960
/// \brief Set the pointer value of the message receiver.
968961
void setReceiverPointer(void *Value) {
969-
*reinterpret_cast<void **>(this + 1) = Value;
962+
*getTrailingObjects<void *>() = Value;
970963
}
971964

972965
SelectorLocationsKind getSelLocsKind() const {
@@ -979,10 +972,10 @@ class ObjCMessageExpr : public Expr {
979972
/// \brief Get a pointer to the stored selector identifiers locations array.
980973
/// No locations will be stored if HasStandardSelLocs is true.
981974
SourceLocation *getStoredSelLocs() {
982-
return reinterpret_cast<SourceLocation*>(getArgs() + getNumArgs());
975+
return getTrailingObjects<SourceLocation>();
983976
}
984977
const SourceLocation *getStoredSelLocs() const {
985-
return reinterpret_cast<const SourceLocation*>(getArgs() + getNumArgs());
978+
return getTrailingObjects<SourceLocation>();
986979
}
987980

988981
/// \brief Get the number of stored selector identifiers locations.
@@ -1286,20 +1279,21 @@ class ObjCMessageExpr : public Expr {
12861279
/// \brief Retrieve the arguments to this message, not including the
12871280
/// receiver.
12881281
Expr **getArgs() {
1289-
return reinterpret_cast<Expr **>(this + 1) + 1;
1282+
return reinterpret_cast<Expr **>(getTrailingObjects<void *>() + 1);
12901283
}
12911284
const Expr * const *getArgs() const {
1292-
return reinterpret_cast<const Expr * const *>(this + 1) + 1;
1285+
return reinterpret_cast<const Expr *const *>(getTrailingObjects<void *>() +
1286+
1);
12931287
}
12941288

12951289
/// getArg - Return the specified argument.
12961290
Expr *getArg(unsigned Arg) {
12971291
assert(Arg < NumArgs && "Arg access out of range!");
1298-
return cast<Expr>(getArgs()[Arg]);
1292+
return getArgs()[Arg];
12991293
}
13001294
const Expr *getArg(unsigned Arg) const {
13011295
assert(Arg < NumArgs && "Arg access out of range!");
1302-
return cast<Expr>(getArgs()[Arg]);
1296+
return getArgs()[Arg];
13031297
}
13041298
/// setArg - Set the specified argument.
13051299
void setArg(unsigned Arg, Expr *ArgExpr) {
@@ -1379,6 +1373,7 @@ class ObjCMessageExpr : public Expr {
13791373
return reinterpret_cast<Stmt const * const*>(getArgs() + NumArgs);
13801374
}
13811375

1376+
friend TrailingObjects;
13821377
friend class ASTStmtReader;
13831378
friend class ASTStmtWriter;
13841379
};

0 commit comments

Comments
 (0)