Skip to content

Commit 3bb81fb

Browse files
committed
Get @init working
1 parent c86c860 commit 3bb81fb

28 files changed

+327
-4
lines changed

clang/include/clang-c/Index.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2229,7 +2229,9 @@ enum CXCursorKind {
22292229
CXCursor_FirstExtraDecl = CXCursor_ModuleImportDecl,
22302230
CXCursor_LastExtraDecl = CXCursor_ConceptDecl,
22312231
CXCursor_ObjCOrigExpr = 606, /* FIXME: This doesn't belong
2232-
here. */
2232+
here. */
2233+
CXCursor_ObjCInitExpr = 607, /* FIXME: This doesn't belong
2234+
here. */
22332235
/**
22342236
* A code completion overload candidate.
22352237
*/

clang/include/clang/AST/ExprObjC.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,94 @@ class ObjCOrigExpr : public Expr {
489489
}
490490
};
491491

492+
493+
/// ObjCInitExpr, used for \@init in ObjC-Logos. \@orig has the same
494+
/// type as the return type of the Objective-C method it is used in.
495+
class ObjCInitExpr : public Expr {
496+
497+
public:
498+
SmallVector<ObjCGroupDecl*, 5> Args;
499+
private:
500+
501+
enum { NumArgsBitWidth = 16 };
502+
503+
/// \brief The number of arguments in the message send, not
504+
/// including the receiver.
505+
unsigned NumArgs : NumArgsBitWidth;
506+
507+
void setNumArgs(unsigned Num) {
508+
assert((Num >> NumArgsBitWidth) == 0 && "Num of args is out of range!");
509+
NumArgs = Num;
510+
}
511+
512+
SourceLocation AtLoc, RParenLoc;
513+
514+
public:
515+
ObjCInitExpr(QualType type, ArrayRef<ObjCGroupDecl*> Args,
516+
SourceLocation at, SourceLocation rp);
517+
518+
explicit ObjCInitExpr(EmptyShell Empty) : Expr(ObjCInitExprClass, Empty){}
519+
520+
521+
SourceLocation getAtLoc() const { return AtLoc; }
522+
void setAtLoc(SourceLocation L) { AtLoc = L; }
523+
SourceLocation getRParenLoc() const { return RParenLoc; }
524+
void setRParenLoc(SourceLocation L) { RParenLoc = L; }
525+
526+
/// \brief Return the number of arguments
527+
unsigned getNumArgs() const { return NumArgs; }
528+
529+
/// \brief Retrieve the arguments to this message, not including the
530+
/// receiver.
531+
Expr **getArgs() {
532+
return reinterpret_cast<Expr **>(this + 1) + 1;
533+
}
534+
const Expr * const *getArgs() const {
535+
return reinterpret_cast<const Expr * const *>(this + 1) + 1;
536+
}
537+
538+
/// getArg - Return the specified argument.
539+
Expr *getArg(unsigned Arg) {
540+
assert(Arg < NumArgs && "Arg access out of range!");
541+
return cast<Expr>(getArgs()[Arg]);
542+
}
543+
const Expr *getArg(unsigned Arg) const {
544+
assert(Arg < NumArgs && "Arg access out of range!");
545+
return cast<Expr>(getArgs()[Arg]);
546+
}
547+
/// setArg - Set the specified argument.
548+
void setArg(unsigned Arg, Expr *ArgExpr) {
549+
assert(Arg < NumArgs && "Arg access out of range!");
550+
getArgs()[Arg] = ArgExpr;
551+
}
552+
553+
child_range children();
554+
555+
typedef ExprIterator arg_iterator;
556+
typedef ConstExprIterator const_arg_iterator;
557+
558+
arg_iterator arg_begin() { return reinterpret_cast<Stmt **>(getArgs()); }
559+
arg_iterator arg_end() {
560+
return reinterpret_cast<Stmt **>(getArgs() + NumArgs);
561+
}
562+
const_arg_iterator arg_begin() const {
563+
return reinterpret_cast<Stmt const * const*>(getArgs());
564+
}
565+
const_arg_iterator arg_end() const {
566+
return reinterpret_cast<Stmt const * const*>(getArgs() + NumArgs);
567+
}
568+
569+
SourceLocation getLocStart() const LLVM_READONLY { return AtLoc; }
570+
SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
571+
572+
SourceLocation getBeginLoc() const LLVM_READONLY { return AtLoc; }
573+
SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
574+
575+
static bool classof(const Stmt *T) {
576+
return T->getStmtClass() == ObjCInitExprClass;
577+
}
578+
};
579+
492580
/// ObjCEncodeExpr, used for \@encode in Objective-C. \@encode has the same
493581
/// type and behavior as StringLiteral except that the string initializer is
494582
/// obtained from ASTContext with the encoding type as an argument.

clang/include/clang/AST/RecursiveASTVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2775,6 +2775,7 @@ DEF_TRAVERSE_STMT(ObjCMessageExpr, {
27752775
})
27762776

27772777
DEF_TRAVERSE_STMT(ObjCOrigExpr, { })
2778+
DEF_TRAVERSE_STMT(ObjCInitExpr, { })
27782779
DEF_TRAVERSE_STMT(ObjCPropertyRefExpr, {
27792780
if (S->isClassReceiver()) {
27802781
ObjCInterfaceDecl *IDecl = S->getClassReceiver();

clang/include/clang/Basic/StmtNodes.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ def ObjCArrayLiteral : StmtNode<Expr>;
179179
def ObjCDictionaryLiteral : StmtNode<Expr>;
180180
def ObjCEncodeExpr : StmtNode<Expr>;
181181
def ObjCOrigExpr : StmtNode<Expr>;
182+
def ObjCInitExpr : StmtNode<Expr>;
182183
def ObjCMessageExpr : StmtNode<Expr>;
183184
def ObjCSelectorExpr : StmtNode<Expr>;
184185
def ObjCProtocolExpr : StmtNode<Expr>;

clang/include/clang/Basic/TokenKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,7 @@ OBJC_AT_KEYWORD(available)
793793

794794
OBJC_AT_KEYWORD(hook)
795795
OBJC_AT_KEYWORD(group)
796+
OBJC_AT_KEYWORD(init)
796797
OBJC_AT_KEYWORD(orig)
797798
OBJC_AT_KEYWORD(new)
798799

clang/include/clang/Parse/Parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,6 +2086,7 @@ class Parser : public CodeCompletionHandler {
20862086
ExprResult ParseObjCSelectorExpression(SourceLocation AtLoc);
20872087
ExprResult ParseObjCProtocolExpression(SourceLocation AtLoc);
20882088
ExprResult ParseObjCOrigExpression(SourceLocation AtLoc);
2089+
ExprResult ParseObjCInitExpression(SourceLocation AtLoc);
20892090
bool isSimpleObjCMessageExpression();
20902091
ExprResult ParseObjCMessageExpression();
20912092
ExprResult ParseObjCMessageExpressionBody(SourceLocation LBracloc,

clang/include/clang/Sema/Sema.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7553,6 +7553,9 @@ class Sema final {
75537553
ExprResult BuildObjCOrigExpression(SourceLocation AtLoc,
75547554
ArrayRef<Expr *> Args,
75557555
SourceLocation RParenLoc);
7556+
ExprResult BuildObjCInitExpression(SourceLocation AtLoc,
7557+
ArrayRef<ObjCGroupDecl *> Args,
7558+
SourceLocation RParenLoc);
75567559
ExprResult BuildObjCEncodeExpression(SourceLocation AtLoc,
75577560
TypeSourceInfo *EncodedTypeInfo,
75587561
SourceLocation RParenLoc);

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,6 +1782,9 @@ enum StmtCode {
17821782
/// An ObjCOrigExpr record.
17831783
EXPR_OBJC_ORIG_EXPR,
17841784

1785+
/// An ObjCInitExpr record.
1786+
EXPR_OBJC_INIT_EXPR,
1787+
17851788
/// An ObjCIsa Expr record.
17861789
EXPR_OBJC_ISA,
17871790

clang/lib/AST/Expr.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3784,6 +3784,7 @@ bool Expr::HasSideEffects(const ASTContext &Ctx,
37843784
case ObjCMessageExprClass:
37853785
case ObjCPropertyRefExprClass:
37863786
case ObjCOrigExprClass:
3787+
case ObjCInitExprClass:
37873788
// FIXME: Classify these cases better.
37883789
if (IncludePossibleEffects)
37893790
return true;
@@ -4309,6 +4310,26 @@ Stmt::child_range ObjCOrigExpr::children() {
43094310
reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
43104311
}
43114312

4313+
ObjCInitExpr::ObjCInitExpr(QualType rType, ArrayRef<ObjCGroupDecl *> args, SourceLocation at, SourceLocation rp) :
4314+
Expr(ObjCInitExprClass, rType, VK_PRValue, OK_Ordinary),
4315+
4316+
AtLoc(at), RParenLoc(rp) {
4317+
setNumArgs(Args.size());
4318+
for (auto g : args)
4319+
{
4320+
Args.push_back(g);
4321+
}
4322+
}
4323+
4324+
// ObjCMessageExpr
4325+
Stmt::child_range ObjCInitExpr::children() {
4326+
Stmt **begin;
4327+
4328+
begin = reinterpret_cast<Stmt **>(getArgs());
4329+
4330+
return child_range(begin, begin);
4331+
}
4332+
43124333
ShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr *> args,
43134334
QualType Type, SourceLocation BLoc,
43144335
SourceLocation RP)

clang/lib/AST/ExprClassification.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) {
188188
case Expr::ObjCDictionaryLiteralClass:
189189
case Expr::ObjCBoolLiteralExprClass:
190190
case Expr::ObjCOrigExprClass:
191+
case Expr::ObjCInitExprClass:
191192
case Expr::ObjCAvailabilityCheckExprClass:
192193
case Expr::ParenListExprClass:
193194
case Expr::SizeOfPackExprClass:

0 commit comments

Comments
 (0)