2006-11-10 13:07:45 +08:00
|
|
|
//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-11-10 13:07:45 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements semantic analysis for statements.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Sema.h"
|
2007-08-23 13:46:52 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-08-11 13:35:13 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2007-05-09 05:09:37 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2007-11-25 08:25:21 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2006-11-10 13:17:58 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2006-11-10 13:07:45 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2007-09-16 22:56:35 +08:00
|
|
|
Sema::StmtResult Sema::ActOnExprStmt(ExprTy *expr) {
|
2007-06-27 13:38:08 +08:00
|
|
|
Expr *E = static_cast<Expr*>(expr);
|
2007-09-16 22:56:35 +08:00
|
|
|
assert(E && "ActOnExprStmt(): missing expression");
|
2008-07-26 07:18:17 +08:00
|
|
|
|
|
|
|
// C99 6.8.3p2: The expression in an expression statement is evaluated as a
|
|
|
|
// void expression for its side effects. Conversion to void allows any
|
|
|
|
// operand, even incomplete types.
|
|
|
|
|
|
|
|
// Same thing in for stmt first clause (when expr) and third clause.
|
2007-06-27 13:38:08 +08:00
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-16 22:56:35 +08:00
|
|
|
Sema::StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) {
|
2007-05-28 09:45:28 +08:00
|
|
|
return new NullStmt(SemiLoc);
|
|
|
|
}
|
|
|
|
|
2008-03-13 14:29:04 +08:00
|
|
|
Sema::StmtResult Sema::ActOnDeclStmt(DeclTy *decl, SourceLocation StartLoc,
|
|
|
|
SourceLocation EndLoc) {
|
|
|
|
if (decl == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
ScopedDecl *SD = cast<ScopedDecl>(static_cast<Decl *>(decl));
|
2008-10-08 07:09:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
// This is a temporary hack until we are always passing around
|
|
|
|
// DeclGroupRefs.
|
|
|
|
llvm::SmallVector<Decl*, 10> decls;
|
|
|
|
while (SD) {
|
|
|
|
ScopedDecl* d = SD;
|
|
|
|
SD = SD->getNextDeclarator();
|
|
|
|
d->setNextDeclarator(0);
|
|
|
|
decls.push_back(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert (!decls.empty());
|
|
|
|
|
|
|
|
if (decls.size() == 1) {
|
|
|
|
DeclGroupOwningRef DG(*decls.begin());
|
|
|
|
return new DeclStmt(DG, StartLoc, EndLoc);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DeclGroupOwningRef DG(DeclGroup::Create(Context, decls.size(), &decls[0]));
|
|
|
|
return new DeclStmt(DG, StartLoc, EndLoc);
|
|
|
|
}
|
2007-05-30 06:59:26 +08:00
|
|
|
}
|
2006-11-10 13:07:45 +08:00
|
|
|
|
|
|
|
Action::StmtResult
|
2007-09-16 22:56:35 +08:00
|
|
|
Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
|
2007-09-01 05:49:55 +08:00
|
|
|
StmtTy **elts, unsigned NumElts, bool isStmtExpr) {
|
2007-08-27 12:29:41 +08:00
|
|
|
Stmt **Elts = reinterpret_cast<Stmt**>(elts);
|
|
|
|
// If we're in C89 mode, check that we don't have any decls after stmts. If
|
|
|
|
// so, emit an extension diagnostic.
|
|
|
|
if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
|
|
|
|
// Note that __extension__ can be around a decl.
|
|
|
|
unsigned i = 0;
|
|
|
|
// Skip over all declarations.
|
|
|
|
for (; i != NumElts && isa<DeclStmt>(Elts[i]); ++i)
|
|
|
|
/*empty*/;
|
|
|
|
|
|
|
|
// We found the end of the list or a statement. Scan for another declstmt.
|
|
|
|
for (; i != NumElts && !isa<DeclStmt>(Elts[i]); ++i)
|
|
|
|
/*empty*/;
|
|
|
|
|
|
|
|
if (i != NumElts) {
|
2008-10-07 02:48:35 +08:00
|
|
|
ScopedDecl *D = *cast<DeclStmt>(Elts[i])->decl_begin();
|
2007-08-27 12:29:41 +08:00
|
|
|
Diag(D->getLocation(), diag::ext_mixed_decls_code);
|
|
|
|
}
|
|
|
|
}
|
2007-09-01 05:49:55 +08:00
|
|
|
// Warn about unused expressions in statements.
|
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
|
|
|
Expr *E = dyn_cast<Expr>(Elts[i]);
|
|
|
|
if (!E) continue;
|
|
|
|
|
|
|
|
// Warn about expressions with unused results.
|
|
|
|
if (E->hasLocalSideEffect() || E->getType()->isVoidType())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// The last expr in a stmt expr really is used.
|
|
|
|
if (isStmtExpr && i == NumElts-1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/// DiagnoseDeadExpr - This expression is side-effect free and evaluated in
|
|
|
|
/// a context where the result is unused. Emit a diagnostic to warn about
|
|
|
|
/// this.
|
|
|
|
if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
|
|
|
|
Diag(BO->getOperatorLoc(), diag::warn_unused_expr,
|
|
|
|
BO->getLHS()->getSourceRange(), BO->getRHS()->getSourceRange());
|
|
|
|
else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
|
|
|
|
Diag(UO->getOperatorLoc(), diag::warn_unused_expr,
|
|
|
|
UO->getSubExpr()->getSourceRange());
|
|
|
|
else
|
|
|
|
Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange());
|
|
|
|
}
|
|
|
|
|
2007-09-01 07:28:33 +08:00
|
|
|
return new CompoundStmt(Elts, NumElts, L, R);
|
2006-11-10 13:07:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Action::StmtResult
|
2007-09-16 22:56:35 +08:00
|
|
|
Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
|
2007-07-18 10:28:47 +08:00
|
|
|
SourceLocation DotDotDotLoc, ExprTy *rhsval,
|
2007-07-21 11:00:26 +08:00
|
|
|
SourceLocation ColonLoc, StmtTy *subStmt) {
|
|
|
|
Stmt *SubStmt = static_cast<Stmt*>(subStmt);
|
2007-07-24 01:05:23 +08:00
|
|
|
Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
|
2007-05-09 05:09:37 +08:00
|
|
|
assert((LHSVal != 0) && "missing expression in case statement");
|
2007-07-22 15:07:56 +08:00
|
|
|
|
2007-06-03 07:53:17 +08:00
|
|
|
SourceLocation ExpLoc;
|
2007-05-09 05:09:37 +08:00
|
|
|
// C99 6.8.4.2p3: The expression shall be an integer constant.
|
2007-07-21 11:00:26 +08:00
|
|
|
if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
|
|
|
|
Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
|
|
|
|
LHSVal->getSourceRange());
|
|
|
|
return SubStmt;
|
|
|
|
}
|
2007-05-09 05:09:37 +08:00
|
|
|
|
2007-07-18 10:28:47 +08:00
|
|
|
// GCC extension: The expression shall be an integer constant.
|
2007-07-21 11:00:26 +08:00
|
|
|
if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) {
|
|
|
|
Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr,
|
|
|
|
RHSVal->getSourceRange());
|
2007-08-23 13:46:52 +08:00
|
|
|
RHSVal = 0; // Recover by just forgetting about it.
|
2007-07-18 10:28:47 +08:00
|
|
|
}
|
2007-07-24 01:05:23 +08:00
|
|
|
|
|
|
|
if (SwitchStack.empty()) {
|
|
|
|
Diag(CaseLoc, diag::err_case_not_in_switch);
|
|
|
|
return SubStmt;
|
|
|
|
}
|
2007-06-03 09:44:43 +08:00
|
|
|
|
2007-09-01 07:28:33 +08:00
|
|
|
CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc);
|
2007-07-24 01:05:23 +08:00
|
|
|
SwitchStack.back()->addSwitchCase(CS);
|
2007-07-22 15:07:56 +08:00
|
|
|
return CS;
|
2006-11-10 13:07:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Action::StmtResult
|
2007-09-16 22:56:35 +08:00
|
|
|
Sema::ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc,
|
2007-07-21 11:00:26 +08:00
|
|
|
StmtTy *subStmt, Scope *CurScope) {
|
|
|
|
Stmt *SubStmt = static_cast<Stmt*>(subStmt);
|
2007-07-18 10:28:47 +08:00
|
|
|
|
2007-07-24 01:05:23 +08:00
|
|
|
if (SwitchStack.empty()) {
|
2007-07-21 11:00:26 +08:00
|
|
|
Diag(DefaultLoc, diag::err_default_not_in_switch);
|
|
|
|
return SubStmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt);
|
2007-07-24 01:05:23 +08:00
|
|
|
SwitchStack.back()->addSwitchCase(DS);
|
2007-07-22 15:07:56 +08:00
|
|
|
|
2007-07-18 10:28:47 +08:00
|
|
|
return DS;
|
2006-11-10 13:07:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Action::StmtResult
|
2007-09-16 22:56:35 +08:00
|
|
|
Sema::ActOnLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
|
2007-07-21 11:00:26 +08:00
|
|
|
SourceLocation ColonLoc, StmtTy *subStmt) {
|
|
|
|
Stmt *SubStmt = static_cast<Stmt*>(subStmt);
|
2007-05-28 14:28:18 +08:00
|
|
|
// Look up the record for this label identifier.
|
|
|
|
LabelStmt *&LabelDecl = LabelMap[II];
|
|
|
|
|
|
|
|
// If not forward referenced or defined already, just create a new LabelStmt.
|
|
|
|
if (LabelDecl == 0)
|
2007-07-21 11:00:26 +08:00
|
|
|
return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt);
|
2007-05-28 14:28:18 +08:00
|
|
|
|
2007-05-28 14:56:27 +08:00
|
|
|
assert(LabelDecl->getID() == II && "Label mismatch!");
|
2007-05-28 14:28:18 +08:00
|
|
|
|
|
|
|
// Otherwise, this label was either forward reference or multiply defined. If
|
|
|
|
// multiply defined, reject it now.
|
|
|
|
if (LabelDecl->getSubStmt()) {
|
2007-05-28 14:56:27 +08:00
|
|
|
Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
|
2007-05-28 14:28:18 +08:00
|
|
|
Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
|
2007-07-21 11:00:26 +08:00
|
|
|
return SubStmt;
|
2007-05-28 14:28:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, this label was forward declared, and we just found its real
|
|
|
|
// definition. Fill in the forward definition and return it.
|
|
|
|
LabelDecl->setIdentLoc(IdentLoc);
|
2007-07-21 11:00:26 +08:00
|
|
|
LabelDecl->setSubStmt(SubStmt);
|
2007-05-28 14:28:18 +08:00
|
|
|
return LabelDecl;
|
2006-11-10 13:07:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Action::StmtResult
|
2007-09-16 22:56:35 +08:00
|
|
|
Sema::ActOnIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
|
2006-11-10 13:07:45 +08:00
|
|
|
StmtTy *ThenVal, SourceLocation ElseLoc,
|
|
|
|
StmtTy *ElseVal) {
|
2007-05-29 10:14:17 +08:00
|
|
|
Expr *condExpr = (Expr *)CondVal;
|
2007-10-11 04:50:11 +08:00
|
|
|
Stmt *thenStmt = (Stmt *)ThenVal;
|
|
|
|
|
2007-09-16 22:56:35 +08:00
|
|
|
assert(condExpr && "ActOnIfStmt(): missing expression");
|
2007-05-29 10:14:17 +08:00
|
|
|
|
2007-07-17 05:54:35 +08:00
|
|
|
DefaultFunctionArrayConversion(condExpr);
|
|
|
|
QualType condType = condExpr->getType();
|
2007-05-29 10:14:17 +08:00
|
|
|
|
2008-09-10 10:17:11 +08:00
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
|
|
|
|
return true;
|
|
|
|
} else if (!condType->isScalarType()) // C99 6.8.4.1p1
|
2007-05-29 10:14:17 +08:00
|
|
|
return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
|
|
|
|
condType.getAsString(), condExpr->getSourceRange());
|
|
|
|
|
2007-10-11 04:50:11 +08:00
|
|
|
// Warn if the if block has a null body without an else value.
|
|
|
|
// this helps prevent bugs due to typos, such as
|
|
|
|
// if (condition);
|
|
|
|
// do_stuff();
|
|
|
|
if (!ElseVal) {
|
|
|
|
if (NullStmt* stmt = dyn_cast<NullStmt>(thenStmt))
|
|
|
|
Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new IfStmt(IfLoc, condExpr, thenStmt, (Stmt*)ElseVal);
|
2006-11-10 13:07:45 +08:00
|
|
|
}
|
2007-05-29 10:14:17 +08:00
|
|
|
|
2006-11-10 13:07:45 +08:00
|
|
|
Action::StmtResult
|
2007-09-16 22:56:35 +08:00
|
|
|
Sema::ActOnStartOfSwitchStmt(ExprTy *cond) {
|
2007-08-23 13:46:52 +08:00
|
|
|
Expr *Cond = static_cast<Expr*>(cond);
|
|
|
|
|
2008-09-10 10:17:11 +08:00
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
// C++ 6.4.2.p2:
|
|
|
|
// The condition shall be of integral type, enumeration type, or of a class
|
|
|
|
// type for which a single conversion function to integral or enumeration
|
|
|
|
// type exists (12.3). If the condition is of class type, the condition is
|
|
|
|
// converted by calling that conversion function, and the result of the
|
|
|
|
// conversion is used in place of the original condition for the remainder
|
|
|
|
// of this section. Integral promotions are performed.
|
|
|
|
|
|
|
|
QualType Ty = Cond->getType();
|
|
|
|
|
|
|
|
// FIXME: Handle class types.
|
|
|
|
|
|
|
|
// If the type is wrong a diagnostic will be emitted later at
|
|
|
|
// ActOnFinishSwitchStmt.
|
|
|
|
if (Ty->isIntegralType() || Ty->isEnumeralType()) {
|
|
|
|
// Integral promotions are performed.
|
|
|
|
// FIXME: Integral promotions for C++ are not complete.
|
|
|
|
UsualUnaryConversions(Cond);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// C99 6.8.4.2p5 - Integer promotions are performed on the controlling expr.
|
|
|
|
UsualUnaryConversions(Cond);
|
|
|
|
}
|
2007-08-23 13:46:52 +08:00
|
|
|
|
|
|
|
SwitchStmt *SS = new SwitchStmt(Cond);
|
2007-07-22 15:07:56 +08:00
|
|
|
SwitchStack.push_back(SS);
|
|
|
|
return SS;
|
|
|
|
}
|
2007-07-18 10:28:47 +08:00
|
|
|
|
2007-08-23 13:46:52 +08:00
|
|
|
/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
|
|
|
|
/// the specified width and sign. If an overflow occurs, detect it and emit
|
|
|
|
/// the specified diagnostic.
|
|
|
|
void Sema::ConvertIntegerToTypeWarnOnOverflow(llvm::APSInt &Val,
|
|
|
|
unsigned NewWidth, bool NewSign,
|
|
|
|
SourceLocation Loc,
|
|
|
|
unsigned DiagID) {
|
|
|
|
// Perform a conversion to the promoted condition type if needed.
|
|
|
|
if (NewWidth > Val.getBitWidth()) {
|
|
|
|
// If this is an extension, just do it.
|
|
|
|
llvm::APSInt OldVal(Val);
|
|
|
|
Val.extend(NewWidth);
|
|
|
|
|
|
|
|
// If the input was signed and negative and the output is unsigned,
|
|
|
|
// warn.
|
|
|
|
if (!NewSign && OldVal.isSigned() && OldVal.isNegative())
|
2008-08-17 15:19:51 +08:00
|
|
|
Diag(Loc, DiagID, OldVal.toString(10), Val.toString(10));
|
2007-08-23 13:46:52 +08:00
|
|
|
|
|
|
|
Val.setIsSigned(NewSign);
|
|
|
|
} else if (NewWidth < Val.getBitWidth()) {
|
|
|
|
// If this is a truncation, check for overflow.
|
|
|
|
llvm::APSInt ConvVal(Val);
|
|
|
|
ConvVal.trunc(NewWidth);
|
2007-08-24 06:08:35 +08:00
|
|
|
ConvVal.setIsSigned(NewSign);
|
2007-08-23 13:46:52 +08:00
|
|
|
ConvVal.extend(Val.getBitWidth());
|
2007-08-24 06:08:35 +08:00
|
|
|
ConvVal.setIsSigned(Val.isSigned());
|
2007-08-23 13:46:52 +08:00
|
|
|
if (ConvVal != Val)
|
2008-08-17 15:19:51 +08:00
|
|
|
Diag(Loc, DiagID, Val.toString(10), ConvVal.toString(10));
|
2007-08-23 13:46:52 +08:00
|
|
|
|
|
|
|
// Regardless of whether a diagnostic was emitted, really do the
|
|
|
|
// truncation.
|
|
|
|
Val.trunc(NewWidth);
|
2007-08-24 06:08:35 +08:00
|
|
|
Val.setIsSigned(NewSign);
|
2007-08-23 13:46:52 +08:00
|
|
|
} else if (NewSign != Val.isSigned()) {
|
|
|
|
// Convert the sign to match the sign of the condition. This can cause
|
|
|
|
// overflow as well: unsigned(INTMIN)
|
|
|
|
llvm::APSInt OldVal(Val);
|
|
|
|
Val.setIsSigned(NewSign);
|
|
|
|
|
|
|
|
if (Val.isNegative()) // Sign bit changes meaning.
|
2008-08-17 15:19:51 +08:00
|
|
|
Diag(Loc, DiagID, OldVal.toString(10), Val.toString(10));
|
2007-08-23 13:46:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-24 02:29:20 +08:00
|
|
|
namespace {
|
|
|
|
struct CaseCompareFunctor {
|
|
|
|
bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
|
|
|
|
const llvm::APSInt &RHS) {
|
|
|
|
return LHS.first < RHS;
|
|
|
|
}
|
2007-09-04 02:31:57 +08:00
|
|
|
bool operator()(const std::pair<llvm::APSInt, CaseStmt*> &LHS,
|
|
|
|
const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
|
|
|
|
return LHS.first < RHS.first;
|
|
|
|
}
|
2007-08-24 02:29:20 +08:00
|
|
|
bool operator()(const llvm::APSInt &LHS,
|
|
|
|
const std::pair<llvm::APSInt, CaseStmt*> &RHS) {
|
|
|
|
return LHS < RHS.first;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2007-09-22 02:15:22 +08:00
|
|
|
/// CmpCaseVals - Comparison predicate for sorting case values.
|
|
|
|
///
|
|
|
|
static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
|
|
|
|
const std::pair<llvm::APSInt, CaseStmt*>& rhs) {
|
|
|
|
if (lhs.first < rhs.first)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (lhs.first == rhs.first &&
|
|
|
|
lhs.second->getCaseLoc().getRawEncoding()
|
|
|
|
< rhs.second->getCaseLoc().getRawEncoding())
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-07-22 15:07:56 +08:00
|
|
|
Action::StmtResult
|
2007-09-22 02:15:22 +08:00
|
|
|
Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch,
|
|
|
|
ExprTy *Body) {
|
2007-07-22 15:07:56 +08:00
|
|
|
Stmt *BodyStmt = (Stmt*)Body;
|
|
|
|
|
|
|
|
SwitchStmt *SS = SwitchStack.back();
|
|
|
|
assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!");
|
|
|
|
|
2007-09-02 05:08:38 +08:00
|
|
|
SS->setBody(BodyStmt, SwitchLoc);
|
2007-07-22 15:07:56 +08:00
|
|
|
SwitchStack.pop_back();
|
|
|
|
|
2007-08-23 13:46:52 +08:00
|
|
|
Expr *CondExpr = SS->getCond();
|
|
|
|
QualType CondType = CondExpr->getType();
|
2007-07-18 10:28:47 +08:00
|
|
|
|
2007-08-23 13:46:52 +08:00
|
|
|
if (!CondType->isIntegerType()) { // C99 6.8.4.2p1
|
2007-07-22 15:07:56 +08:00
|
|
|
Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer,
|
2007-08-23 13:46:52 +08:00
|
|
|
CondType.getAsString(), CondExpr->getSourceRange());
|
|
|
|
return true;
|
2007-07-22 15:07:56 +08:00
|
|
|
}
|
2007-08-23 13:46:52 +08:00
|
|
|
|
|
|
|
// Get the bitwidth of the switched-on value before promotions. We must
|
|
|
|
// convert the integer case values to this width before comparison.
|
2008-03-06 02:54:05 +08:00
|
|
|
unsigned CondWidth = static_cast<unsigned>(Context.getTypeSize(CondType));
|
2007-08-23 13:46:52 +08:00
|
|
|
bool CondIsSigned = CondType->isSignedIntegerType();
|
|
|
|
|
|
|
|
// Accumulate all of the case values in a vector so that we can sort them
|
|
|
|
// and detect duplicates. This vector contains the APInt for the case after
|
|
|
|
// it has been converted to the condition type.
|
2007-08-24 02:29:20 +08:00
|
|
|
typedef llvm::SmallVector<std::pair<llvm::APSInt, CaseStmt*>, 64> CaseValsTy;
|
|
|
|
CaseValsTy CaseVals;
|
2007-08-23 13:46:52 +08:00
|
|
|
|
|
|
|
// Keep track of any GNU case ranges we see. The APSInt is the low value.
|
|
|
|
std::vector<std::pair<llvm::APSInt, CaseStmt*> > CaseRanges;
|
|
|
|
|
|
|
|
DefaultStmt *TheDefaultStmt = 0;
|
2007-07-22 15:07:56 +08:00
|
|
|
|
2007-08-23 14:23:56 +08:00
|
|
|
bool CaseListIsErroneous = false;
|
|
|
|
|
|
|
|
for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
|
2007-07-22 15:07:56 +08:00
|
|
|
SC = SC->getNextSwitchCase()) {
|
2007-08-23 14:23:56 +08:00
|
|
|
|
2007-07-22 15:07:56 +08:00
|
|
|
if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) {
|
2007-08-23 13:46:52 +08:00
|
|
|
if (TheDefaultStmt) {
|
|
|
|
Diag(DS->getDefaultLoc(), diag::err_multiple_default_labels_defined);
|
|
|
|
Diag(TheDefaultStmt->getDefaultLoc(), diag::err_first_label);
|
2007-07-22 15:07:56 +08:00
|
|
|
|
2007-08-23 13:46:52 +08:00
|
|
|
// FIXME: Remove the default statement from the switch block so that
|
|
|
|
// we'll return a valid AST. This requires recursing down the
|
|
|
|
// AST and finding it, not something we are set up to do right now. For
|
|
|
|
// now, just lop the entire switch stmt out of the AST.
|
2007-08-23 14:23:56 +08:00
|
|
|
CaseListIsErroneous = true;
|
2007-07-22 15:07:56 +08:00
|
|
|
}
|
2007-08-23 13:46:52 +08:00
|
|
|
TheDefaultStmt = DS;
|
2007-07-22 15:07:56 +08:00
|
|
|
|
2007-08-23 13:46:52 +08:00
|
|
|
} else {
|
|
|
|
CaseStmt *CS = cast<CaseStmt>(SC);
|
|
|
|
|
|
|
|
// We already verified that the expression has a i-c-e value (C99
|
|
|
|
// 6.8.4.2p3) - get that value now.
|
2008-01-17 03:17:22 +08:00
|
|
|
Expr *Lo = CS->getLHS();
|
2008-08-14 07:47:13 +08:00
|
|
|
llvm::APSInt LoVal = Lo->getIntegerConstantExprValue(Context);
|
2007-08-23 13:46:52 +08:00
|
|
|
|
|
|
|
// Convert the value to the same width/sign as the condition.
|
|
|
|
ConvertIntegerToTypeWarnOnOverflow(LoVal, CondWidth, CondIsSigned,
|
|
|
|
CS->getLHS()->getLocStart(),
|
|
|
|
diag::warn_case_value_overflow);
|
2007-07-18 10:28:47 +08:00
|
|
|
|
2008-01-17 03:17:22 +08:00
|
|
|
// If the LHS is not the same type as the condition, insert an implicit
|
|
|
|
// cast.
|
|
|
|
ImpCastExprToType(Lo, CondType);
|
|
|
|
CS->setLHS(Lo);
|
|
|
|
|
2007-08-23 14:23:56 +08:00
|
|
|
// If this is a case range, remember it in CaseRanges, otherwise CaseVals.
|
2007-08-23 13:46:52 +08:00
|
|
|
if (CS->getRHS())
|
|
|
|
CaseRanges.push_back(std::make_pair(LoVal, CS));
|
2007-08-23 14:23:56 +08:00
|
|
|
else
|
|
|
|
CaseVals.push_back(std::make_pair(LoVal, CS));
|
2007-08-23 13:46:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-23 14:23:56 +08:00
|
|
|
// Sort all the scalar case values so we can easily detect duplicates.
|
2007-09-22 02:15:22 +08:00
|
|
|
std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals);
|
2007-08-23 14:23:56 +08:00
|
|
|
|
2007-08-23 22:29:07 +08:00
|
|
|
if (!CaseVals.empty()) {
|
|
|
|
for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) {
|
|
|
|
if (CaseVals[i].first == CaseVals[i+1].first) {
|
|
|
|
// If we have a duplicate, report it.
|
|
|
|
Diag(CaseVals[i+1].second->getLHS()->getLocStart(),
|
2008-08-17 15:19:51 +08:00
|
|
|
diag::err_duplicate_case, CaseVals[i].first.toString(10));
|
2007-08-23 22:29:07 +08:00
|
|
|
Diag(CaseVals[i].second->getLHS()->getLocStart(),
|
|
|
|
diag::err_duplicate_case_prev);
|
|
|
|
// FIXME: We really want to remove the bogus case stmt from the substmt,
|
|
|
|
// but we have no way to do this right now.
|
|
|
|
CaseListIsErroneous = true;
|
|
|
|
}
|
2007-08-23 14:23:56 +08:00
|
|
|
}
|
|
|
|
}
|
2007-08-23 13:46:52 +08:00
|
|
|
|
2007-08-23 14:23:56 +08:00
|
|
|
// Detect duplicate case ranges, which usually don't exist at all in the first
|
|
|
|
// place.
|
|
|
|
if (!CaseRanges.empty()) {
|
|
|
|
// Sort all the case ranges by their low value so we can easily detect
|
|
|
|
// overlaps between ranges.
|
2007-08-24 02:29:20 +08:00
|
|
|
std::stable_sort(CaseRanges.begin(), CaseRanges.end());
|
2007-08-23 14:23:56 +08:00
|
|
|
|
|
|
|
// Scan the ranges, computing the high values and removing empty ranges.
|
|
|
|
std::vector<llvm::APSInt> HiVals;
|
2007-08-24 01:48:14 +08:00
|
|
|
for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
|
2007-08-23 14:23:56 +08:00
|
|
|
CaseStmt *CR = CaseRanges[i].second;
|
2008-01-17 03:17:22 +08:00
|
|
|
Expr *Hi = CR->getRHS();
|
2008-08-14 07:47:13 +08:00
|
|
|
llvm::APSInt HiVal = Hi->getIntegerConstantExprValue(Context);
|
2007-08-23 14:23:56 +08:00
|
|
|
|
|
|
|
// Convert the value to the same width/sign as the condition.
|
|
|
|
ConvertIntegerToTypeWarnOnOverflow(HiVal, CondWidth, CondIsSigned,
|
|
|
|
CR->getRHS()->getLocStart(),
|
|
|
|
diag::warn_case_value_overflow);
|
|
|
|
|
2008-01-17 03:17:22 +08:00
|
|
|
// If the LHS is not the same type as the condition, insert an implicit
|
|
|
|
// cast.
|
|
|
|
ImpCastExprToType(Hi, CondType);
|
|
|
|
CR->setRHS(Hi);
|
|
|
|
|
2007-08-24 01:48:14 +08:00
|
|
|
// If the low value is bigger than the high value, the case is empty.
|
|
|
|
if (CaseRanges[i].first > HiVal) {
|
|
|
|
Diag(CR->getLHS()->getLocStart(), diag::warn_case_empty_range,
|
|
|
|
SourceRange(CR->getLHS()->getLocStart(),
|
|
|
|
CR->getRHS()->getLocEnd()));
|
|
|
|
CaseRanges.erase(CaseRanges.begin()+i);
|
|
|
|
--i, --e;
|
|
|
|
continue;
|
|
|
|
}
|
2007-08-23 14:23:56 +08:00
|
|
|
HiVals.push_back(HiVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rescan the ranges, looking for overlap with singleton values and other
|
2007-08-24 02:29:20 +08:00
|
|
|
// ranges. Since the range list is sorted, we only need to compare case
|
|
|
|
// ranges with their neighbors.
|
2007-08-23 14:23:56 +08:00
|
|
|
for (unsigned i = 0, e = CaseRanges.size(); i != e; ++i) {
|
2007-08-24 02:29:20 +08:00
|
|
|
llvm::APSInt &CRLo = CaseRanges[i].first;
|
|
|
|
llvm::APSInt &CRHi = HiVals[i];
|
|
|
|
CaseStmt *CR = CaseRanges[i].second;
|
|
|
|
|
|
|
|
// Check to see whether the case range overlaps with any singleton cases.
|
|
|
|
CaseStmt *OverlapStmt = 0;
|
|
|
|
llvm::APSInt OverlapVal(32);
|
|
|
|
|
|
|
|
// Find the smallest value >= the lower bound. If I is in the case range,
|
|
|
|
// then we have overlap.
|
|
|
|
CaseValsTy::iterator I = std::lower_bound(CaseVals.begin(),
|
|
|
|
CaseVals.end(), CRLo,
|
|
|
|
CaseCompareFunctor());
|
|
|
|
if (I != CaseVals.end() && I->first < CRHi) {
|
|
|
|
OverlapVal = I->first; // Found overlap with scalar.
|
|
|
|
OverlapStmt = I->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the smallest value bigger than the upper bound.
|
|
|
|
I = std::upper_bound(I, CaseVals.end(), CRHi, CaseCompareFunctor());
|
|
|
|
if (I != CaseVals.begin() && (I-1)->first >= CRLo) {
|
|
|
|
OverlapVal = (I-1)->first; // Found overlap with scalar.
|
|
|
|
OverlapStmt = (I-1)->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if this case stmt overlaps with the subsequent case range.
|
|
|
|
if (i && CRLo <= HiVals[i-1]) {
|
|
|
|
OverlapVal = HiVals[i-1]; // Found overlap with range.
|
|
|
|
OverlapStmt = CaseRanges[i-1].second;
|
|
|
|
}
|
2007-08-23 14:23:56 +08:00
|
|
|
|
2007-08-24 02:29:20 +08:00
|
|
|
if (OverlapStmt) {
|
|
|
|
// If we have a duplicate, report it.
|
|
|
|
Diag(CR->getLHS()->getLocStart(),
|
2008-08-17 15:19:51 +08:00
|
|
|
diag::err_duplicate_case, OverlapVal.toString(10));
|
2007-08-24 02:29:20 +08:00
|
|
|
Diag(OverlapStmt->getLHS()->getLocStart(),
|
|
|
|
diag::err_duplicate_case_prev);
|
|
|
|
// FIXME: We really want to remove the bogus case stmt from the substmt,
|
|
|
|
// but we have no way to do this right now.
|
|
|
|
CaseListIsErroneous = true;
|
|
|
|
}
|
2007-08-23 14:23:56 +08:00
|
|
|
}
|
|
|
|
}
|
2007-08-23 13:46:52 +08:00
|
|
|
|
2007-08-23 14:23:56 +08:00
|
|
|
// FIXME: If the case list was broken is some way, we don't have a good system
|
|
|
|
// to patch it up. Instead, just return the whole substmt as broken.
|
|
|
|
if (CaseListIsErroneous)
|
|
|
|
return true;
|
2007-08-23 13:46:52 +08:00
|
|
|
|
2007-07-22 15:07:56 +08:00
|
|
|
return SS;
|
2006-11-10 13:07:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Action::StmtResult
|
2007-09-16 22:56:35 +08:00
|
|
|
Sema::ActOnWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
|
2007-05-29 10:14:17 +08:00
|
|
|
Expr *condExpr = (Expr *)Cond;
|
2007-09-16 22:56:35 +08:00
|
|
|
assert(condExpr && "ActOnWhileStmt(): missing expression");
|
2007-05-29 10:14:17 +08:00
|
|
|
|
2007-07-17 05:54:35 +08:00
|
|
|
DefaultFunctionArrayConversion(condExpr);
|
|
|
|
QualType condType = condExpr->getType();
|
2007-05-29 10:14:17 +08:00
|
|
|
|
2008-09-10 10:17:11 +08:00
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
|
|
|
|
return true;
|
|
|
|
} else if (!condType->isScalarType()) // C99 6.8.5p2
|
2007-05-29 10:14:17 +08:00
|
|
|
return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
|
|
|
|
condType.getAsString(), condExpr->getSourceRange());
|
|
|
|
|
2007-09-01 07:28:33 +08:00
|
|
|
return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc);
|
2006-11-10 13:07:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Action::StmtResult
|
2007-09-16 22:56:35 +08:00
|
|
|
Sema::ActOnDoStmt(SourceLocation DoLoc, StmtTy *Body,
|
2006-11-10 13:07:45 +08:00
|
|
|
SourceLocation WhileLoc, ExprTy *Cond) {
|
2007-05-29 10:14:17 +08:00
|
|
|
Expr *condExpr = (Expr *)Cond;
|
2007-09-16 22:56:35 +08:00
|
|
|
assert(condExpr && "ActOnDoStmt(): missing expression");
|
2007-05-29 10:14:17 +08:00
|
|
|
|
2007-07-17 05:54:35 +08:00
|
|
|
DefaultFunctionArrayConversion(condExpr);
|
|
|
|
QualType condType = condExpr->getType();
|
2007-05-29 10:14:17 +08:00
|
|
|
|
2008-09-11 13:16:22 +08:00
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
if (CheckCXXBooleanCondition(condExpr)) // C++ 6.4p4
|
|
|
|
return true;
|
|
|
|
} else if (!condType->isScalarType()) // C99 6.8.5p2
|
2007-05-29 10:14:17 +08:00
|
|
|
return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
|
|
|
|
condType.getAsString(), condExpr->getSourceRange());
|
|
|
|
|
2007-09-01 07:28:33 +08:00
|
|
|
return new DoStmt((Stmt*)Body, condExpr, DoLoc);
|
2006-11-10 13:07:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Action::StmtResult
|
2007-09-16 22:56:35 +08:00
|
|
|
Sema::ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
|
2007-08-28 12:55:47 +08:00
|
|
|
StmtTy *first, ExprTy *second, ExprTy *third,
|
|
|
|
SourceLocation RParenLoc, StmtTy *body) {
|
|
|
|
Stmt *First = static_cast<Stmt*>(first);
|
|
|
|
Expr *Second = static_cast<Expr*>(second);
|
|
|
|
Expr *Third = static_cast<Expr*>(third);
|
|
|
|
Stmt *Body = static_cast<Stmt*>(body);
|
|
|
|
|
2008-09-10 10:17:11 +08:00
|
|
|
if (!getLangOptions().CPlusPlus) {
|
|
|
|
if (DeclStmt *DS = dyn_cast_or_null<DeclStmt>(First)) {
|
|
|
|
// C99 6.8.5p3: The declaration part of a 'for' statement shall only declare
|
|
|
|
// identifiers for objects having storage class 'auto' or 'register'.
|
|
|
|
for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
|
|
|
|
DI!=DE; ++DI) {
|
|
|
|
VarDecl *VD = dyn_cast<VarDecl>(*DI);
|
|
|
|
if (VD && VD->isBlockVarDecl() && !VD->hasLocalStorage())
|
|
|
|
VD = 0;
|
|
|
|
if (VD == 0)
|
|
|
|
Diag((*DI)->getLocation(), diag::err_non_variable_decl_in_for);
|
|
|
|
// FIXME: mark decl erroneous!
|
|
|
|
}
|
2007-08-28 13:03:08 +08:00
|
|
|
}
|
2007-05-29 10:14:17 +08:00
|
|
|
}
|
|
|
|
if (Second) {
|
2007-08-28 12:55:47 +08:00
|
|
|
DefaultFunctionArrayConversion(Second);
|
|
|
|
QualType SecondType = Second->getType();
|
2007-05-29 10:14:17 +08:00
|
|
|
|
2008-09-10 10:17:11 +08:00
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
if (CheckCXXBooleanCondition(Second)) // C++ 6.4p4
|
|
|
|
return true;
|
|
|
|
} else if (!SecondType->isScalarType()) // C99 6.8.5p2
|
2007-05-29 10:14:17 +08:00
|
|
|
return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
|
2008-07-26 07:18:17 +08:00
|
|
|
SecondType.getAsString(), Second->getSourceRange());
|
2007-05-29 10:14:17 +08:00
|
|
|
}
|
2007-09-01 07:28:33 +08:00
|
|
|
return new ForStmt(First, Second, Third, Body, ForLoc);
|
2006-11-10 13:07:45 +08:00
|
|
|
}
|
|
|
|
|
2008-01-04 01:55:25 +08:00
|
|
|
Action::StmtResult
|
2008-01-10 08:24:29 +08:00
|
|
|
Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc,
|
2008-01-04 01:55:25 +08:00
|
|
|
SourceLocation LParenLoc,
|
|
|
|
StmtTy *first, ExprTy *second,
|
|
|
|
SourceLocation RParenLoc, StmtTy *body) {
|
|
|
|
Stmt *First = static_cast<Stmt*>(first);
|
|
|
|
Expr *Second = static_cast<Expr*>(second);
|
|
|
|
Stmt *Body = static_cast<Stmt*>(body);
|
2008-01-11 04:33:58 +08:00
|
|
|
if (First) {
|
|
|
|
QualType FirstType;
|
|
|
|
if (DeclStmt *DS = dyn_cast<DeclStmt>(First)) {
|
2008-10-07 04:58:11 +08:00
|
|
|
if (!DS->hasSolitaryDecl())
|
|
|
|
return Diag((*DS->decl_begin())->getLocation(),
|
|
|
|
diag::err_toomany_element_decls);
|
|
|
|
|
|
|
|
ScopedDecl *D = DS->getSolitaryDecl();
|
|
|
|
FirstType = cast<ValueDecl>(D)->getType();
|
2008-01-11 04:33:58 +08:00
|
|
|
// C99 6.8.5p3: The declaration part of a 'for' statement shall only declare
|
|
|
|
// identifiers for objects having storage class 'auto' or 'register'.
|
2008-04-16 06:42:06 +08:00
|
|
|
VarDecl *VD = cast<VarDecl>(D);
|
|
|
|
if (VD->isBlockVarDecl() && !VD->hasLocalStorage())
|
|
|
|
return Diag(VD->getLocation(), diag::err_non_variable_decl_in_for);
|
2008-08-26 02:16:36 +08:00
|
|
|
} else {
|
|
|
|
Expr::isLvalueResult lval = cast<Expr>(First)->isLvalue(Context);
|
|
|
|
|
|
|
|
if (lval != Expr::LV_Valid)
|
|
|
|
return Diag(First->getLocStart(), diag::err_selector_element_not_lvalue,
|
|
|
|
First->getSourceRange());
|
|
|
|
|
|
|
|
FirstType = static_cast<Expr*>(first)->getType();
|
|
|
|
}
|
2008-07-25 07:58:27 +08:00
|
|
|
if (!Context.isObjCObjectPointerType(FirstType))
|
2008-01-11 04:33:58 +08:00
|
|
|
Diag(ForLoc, diag::err_selector_element_type,
|
2008-08-26 02:16:36 +08:00
|
|
|
FirstType.getAsString(), First->getSourceRange());
|
2008-01-04 01:55:25 +08:00
|
|
|
}
|
|
|
|
if (Second) {
|
|
|
|
DefaultFunctionArrayConversion(Second);
|
|
|
|
QualType SecondType = Second->getType();
|
2008-07-25 07:58:27 +08:00
|
|
|
if (!Context.isObjCObjectPointerType(SecondType))
|
2008-01-10 08:24:29 +08:00
|
|
|
Diag(ForLoc, diag::err_collection_expr_type,
|
2008-01-04 08:27:46 +08:00
|
|
|
SecondType.getAsString(), Second->getSourceRange());
|
2008-01-04 01:55:25 +08:00
|
|
|
}
|
2008-01-10 08:24:29 +08:00
|
|
|
return new ObjCForCollectionStmt(First, Second, Body, ForLoc, RParenLoc);
|
2008-01-04 01:55:25 +08:00
|
|
|
}
|
2006-11-10 13:07:45 +08:00
|
|
|
|
|
|
|
Action::StmtResult
|
2007-09-16 22:56:35 +08:00
|
|
|
Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
|
2006-11-10 13:07:45 +08:00
|
|
|
IdentifierInfo *LabelII) {
|
2008-09-04 02:15:37 +08:00
|
|
|
// If we are in a block, reject all gotos for now.
|
|
|
|
if (CurBlock)
|
|
|
|
return Diag(GotoLoc, diag::err_goto_in_block);
|
|
|
|
|
2007-05-28 14:28:18 +08:00
|
|
|
// Look up the record for this label identifier.
|
|
|
|
LabelStmt *&LabelDecl = LabelMap[LabelII];
|
|
|
|
|
|
|
|
// If we haven't seen this label yet, create a forward reference.
|
|
|
|
if (LabelDecl == 0)
|
|
|
|
LabelDecl = new LabelStmt(LabelLoc, LabelII, 0);
|
|
|
|
|
2007-09-07 01:11:52 +08:00
|
|
|
return new GotoStmt(LabelDecl, GotoLoc, LabelLoc);
|
2006-11-10 13:07:45 +08:00
|
|
|
}
|
2007-05-31 14:00:00 +08:00
|
|
|
|
2006-11-10 13:07:45 +08:00
|
|
|
Action::StmtResult
|
2007-09-16 22:56:35 +08:00
|
|
|
Sema::ActOnIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
|
2006-11-10 13:07:45 +08:00
|
|
|
ExprTy *DestExp) {
|
2007-05-31 14:00:00 +08:00
|
|
|
// FIXME: Verify that the operand is convertible to void*.
|
|
|
|
|
2006-11-10 13:07:45 +08:00
|
|
|
return new IndirectGotoStmt((Expr*)DestExp);
|
|
|
|
}
|
|
|
|
|
|
|
|
Action::StmtResult
|
2007-09-16 22:56:35 +08:00
|
|
|
Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
|
2006-11-10 13:17:58 +08:00
|
|
|
Scope *S = CurScope->getContinueParent();
|
|
|
|
if (!S) {
|
|
|
|
// C99 6.8.6.2p1: A break shall appear only in or as a loop body.
|
|
|
|
Diag(ContinueLoc, diag::err_continue_not_in_loop);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-09-01 07:49:30 +08:00
|
|
|
return new ContinueStmt(ContinueLoc);
|
2006-11-10 13:07:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Action::StmtResult
|
2007-09-16 22:56:35 +08:00
|
|
|
Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
|
2006-11-10 13:17:58 +08:00
|
|
|
Scope *S = CurScope->getBreakParent();
|
|
|
|
if (!S) {
|
|
|
|
// C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body.
|
|
|
|
Diag(BreakLoc, diag::err_break_not_in_loop_or_switch);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-09-01 07:49:30 +08:00
|
|
|
return new BreakStmt(BreakLoc);
|
2006-11-10 13:07:45 +08:00
|
|
|
}
|
|
|
|
|
2008-10-29 08:13:59 +08:00
|
|
|
/// ActOnBlockReturnStmt - Utility routine to figure out block's return type.
|
2008-09-04 02:15:37 +08:00
|
|
|
///
|
|
|
|
Action::StmtResult
|
|
|
|
Sema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) {
|
|
|
|
|
|
|
|
// If this is the first return we've seen in the block, infer the type of
|
|
|
|
// the block from it.
|
|
|
|
if (CurBlock->ReturnType == 0) {
|
2008-09-17 06:25:10 +08:00
|
|
|
if (RetValExp) {
|
2008-09-25 06:26:48 +08:00
|
|
|
// Don't call UsualUnaryConversions(), since we don't want to do
|
|
|
|
// integer promotions here.
|
|
|
|
DefaultFunctionArrayConversion(RetValExp);
|
2008-09-04 02:15:37 +08:00
|
|
|
CurBlock->ReturnType = RetValExp->getType().getTypePtr();
|
2008-09-17 06:25:10 +08:00
|
|
|
} else
|
2008-09-04 02:15:37 +08:00
|
|
|
CurBlock->ReturnType = Context.VoidTy.getTypePtr();
|
|
|
|
return new ReturnStmt(ReturnLoc, RetValExp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, verify that this result type matches the previous one. We are
|
|
|
|
// pickier with blocks than for normal functions because we don't have GCC
|
|
|
|
// compatibility to worry about here.
|
|
|
|
if (CurBlock->ReturnType->isVoidType()) {
|
|
|
|
if (RetValExp) {
|
|
|
|
Diag(ReturnLoc, diag::err_return_block_has_expr);
|
|
|
|
delete RetValExp;
|
|
|
|
RetValExp = 0;
|
|
|
|
}
|
|
|
|
return new ReturnStmt(ReturnLoc, RetValExp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!RetValExp) {
|
|
|
|
Diag(ReturnLoc, diag::err_block_return_missing_expr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we have a non-void block with an expression, continue checking
|
|
|
|
QualType RetValType = RetValExp->getType();
|
|
|
|
|
|
|
|
// For now, restrict multiple return statements in a block to have
|
|
|
|
// strict compatible types only.
|
|
|
|
QualType BlockQT = QualType(CurBlock->ReturnType, 0);
|
|
|
|
if (Context.getCanonicalType(BlockQT).getTypePtr()
|
|
|
|
!= Context.getCanonicalType(RetValType).getTypePtr()) {
|
|
|
|
DiagnoseAssignmentResult(Incompatible, ReturnLoc, BlockQT,
|
|
|
|
RetValType, RetValExp, "returning");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RetValExp) CheckReturnStackAddr(RetValExp, BlockQT, ReturnLoc);
|
|
|
|
|
|
|
|
return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
|
|
|
|
}
|
2006-11-10 13:07:45 +08:00
|
|
|
|
|
|
|
Action::StmtResult
|
2007-09-16 22:56:35 +08:00
|
|
|
Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
|
2007-07-14 07:32:42 +08:00
|
|
|
Expr *RetValExp = static_cast<Expr *>(rex);
|
2008-09-04 02:15:37 +08:00
|
|
|
if (CurBlock)
|
|
|
|
return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
|
2008-06-28 14:07:14 +08:00
|
|
|
QualType FnRetType =
|
|
|
|
getCurFunctionDecl() ? getCurFunctionDecl()->getResultType() :
|
|
|
|
getCurMethodDecl()->getResultType();
|
2007-05-28 07:58:33 +08:00
|
|
|
|
2008-01-05 02:04:52 +08:00
|
|
|
if (FnRetType->isVoidType()) {
|
2007-05-29 22:23:36 +08:00
|
|
|
if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
|
2007-12-05 03:20:11 +08:00
|
|
|
Diag(ReturnLoc, diag::ext_return_has_expr,
|
2008-06-28 14:07:14 +08:00
|
|
|
( getCurFunctionDecl() ?
|
|
|
|
getCurFunctionDecl()->getIdentifier()->getName() :
|
|
|
|
getCurMethodDecl()->getSelector().getName() ),
|
2007-07-14 07:32:42 +08:00
|
|
|
RetValExp->getSourceRange());
|
2007-09-01 07:49:30 +08:00
|
|
|
return new ReturnStmt(ReturnLoc, RetValExp);
|
2007-05-29 22:23:36 +08:00
|
|
|
} else {
|
|
|
|
if (!RetValExp) {
|
2008-06-28 14:07:14 +08:00
|
|
|
const char *funcName =
|
|
|
|
getCurFunctionDecl() ?
|
|
|
|
getCurFunctionDecl()->getIdentifier()->getName() :
|
|
|
|
getCurMethodDecl()->getSelector().getName().c_str();
|
2007-05-29 22:23:36 +08:00
|
|
|
if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns)
|
|
|
|
Diag(ReturnLoc, diag::ext_return_missing_expr, funcName);
|
|
|
|
else // C90 6.6.6.4p4
|
|
|
|
Diag(ReturnLoc, diag::warn_return_missing_expr, funcName);
|
2007-09-01 07:49:30 +08:00
|
|
|
return new ReturnStmt(ReturnLoc, (Expr*)0);
|
2007-05-29 22:23:36 +08:00
|
|
|
}
|
2007-05-28 07:58:33 +08:00
|
|
|
}
|
2007-05-29 22:23:36 +08:00
|
|
|
// we have a non-void function with an expression, continue checking
|
2008-01-05 02:04:52 +08:00
|
|
|
QualType RetValType = RetValExp->getType();
|
2007-05-28 07:58:33 +08:00
|
|
|
|
2007-05-29 22:23:36 +08:00
|
|
|
// C99 6.8.6.4p3(136): The return statement is not an assignment. The
|
|
|
|
// overlap restriction of subclause 6.5.16.1 does not apply to the case of
|
|
|
|
// function return.
|
2008-10-29 08:13:59 +08:00
|
|
|
|
|
|
|
// In C++ the return statement is handled via a copy initialization.
|
|
|
|
// the C version of which boils down to
|
|
|
|
// CheckSingleAssignmentConstraints.
|
|
|
|
if (PerformCopyInitialization(RetValExp, FnRetType, "returning"))
|
2008-01-05 02:04:52 +08:00
|
|
|
return true;
|
2007-08-18 00:46:58 +08:00
|
|
|
|
2008-01-05 02:04:52 +08:00
|
|
|
if (RetValExp) CheckReturnStackAddr(RetValExp, FnRetType, ReturnLoc);
|
2007-08-18 00:46:58 +08:00
|
|
|
|
2007-09-01 07:49:30 +08:00
|
|
|
return new ReturnStmt(ReturnLoc, (Expr*)RetValExp);
|
2006-11-10 13:07:45 +08:00
|
|
|
}
|
|
|
|
|
2007-11-21 03:21:03 +08:00
|
|
|
Sema::StmtResult Sema::ActOnAsmStmt(SourceLocation AsmLoc,
|
2008-02-06 07:03:50 +08:00
|
|
|
bool IsSimple,
|
2007-11-24 07:12:25 +08:00
|
|
|
bool IsVolatile,
|
2007-11-22 09:36:19 +08:00
|
|
|
unsigned NumOutputs,
|
|
|
|
unsigned NumInputs,
|
|
|
|
std::string *Names,
|
2008-08-19 03:55:17 +08:00
|
|
|
ExprTy **constraints,
|
|
|
|
ExprTy **exprs,
|
2008-07-23 14:46:56 +08:00
|
|
|
ExprTy *asmString,
|
2007-11-22 09:36:19 +08:00
|
|
|
unsigned NumClobbers,
|
2008-08-19 03:55:17 +08:00
|
|
|
ExprTy **clobbers,
|
2007-10-29 12:04:16 +08:00
|
|
|
SourceLocation RParenLoc) {
|
2008-08-19 03:55:17 +08:00
|
|
|
StringLiteral **Constraints = reinterpret_cast<StringLiteral**>(constraints);
|
|
|
|
Expr **Exprs = reinterpret_cast<Expr **>(exprs);
|
2008-07-23 14:46:56 +08:00
|
|
|
StringLiteral *AsmString = cast<StringLiteral>((Expr *)asmString);
|
2008-08-19 03:55:17 +08:00
|
|
|
StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers);
|
|
|
|
|
|
|
|
// The parser verifies that there is a string literal here.
|
2008-07-23 14:46:56 +08:00
|
|
|
if (AsmString->isWide())
|
|
|
|
// FIXME: We currently leak memory here.
|
|
|
|
return Diag(AsmString->getLocStart(), diag::err_asm_wide_character,
|
|
|
|
AsmString->getSourceRange());
|
|
|
|
|
|
|
|
|
2008-08-19 03:55:17 +08:00
|
|
|
for (unsigned i = 0; i != NumOutputs; i++) {
|
|
|
|
StringLiteral *Literal = Constraints[i];
|
2008-07-23 14:46:56 +08:00
|
|
|
if (Literal->isWide())
|
|
|
|
// FIXME: We currently leak memory here.
|
|
|
|
return Diag(Literal->getLocStart(), diag::err_asm_wide_character,
|
|
|
|
Literal->getSourceRange());
|
|
|
|
|
2007-11-27 12:11:28 +08:00
|
|
|
std::string OutputConstraint(Literal->getStrData(),
|
|
|
|
Literal->getByteLength());
|
|
|
|
|
|
|
|
TargetInfo::ConstraintInfo info;
|
2008-07-23 14:46:56 +08:00
|
|
|
if (!Context.Target.validateOutputConstraint(OutputConstraint.c_str(),info))
|
2007-11-27 12:11:28 +08:00
|
|
|
// FIXME: We currently leak memory here.
|
2008-07-23 14:46:56 +08:00
|
|
|
return Diag(Literal->getLocStart(),
|
2008-08-19 03:55:17 +08:00
|
|
|
diag::err_asm_invalid_output_constraint, OutputConstraint);
|
2007-11-27 12:11:28 +08:00
|
|
|
|
|
|
|
// Check that the output exprs are valid lvalues.
|
2008-08-19 03:55:17 +08:00
|
|
|
ParenExpr *OutputExpr = cast<ParenExpr>(Exprs[i]);
|
2008-07-27 05:30:36 +08:00
|
|
|
Expr::isLvalueResult Result = OutputExpr->isLvalue(Context);
|
2007-11-24 03:43:50 +08:00
|
|
|
if (Result != Expr::LV_Valid) {
|
|
|
|
// FIXME: We currently leak memory here.
|
2008-08-19 03:55:17 +08:00
|
|
|
return Diag(OutputExpr->getSubExpr()->getLocStart(),
|
|
|
|
diag::err_asm_invalid_lvalue_in_output,
|
|
|
|
OutputExpr->getSubExpr()->getSourceRange());
|
2007-11-24 03:43:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
|
2008-08-19 03:55:17 +08:00
|
|
|
StringLiteral *Literal = Constraints[i];
|
2008-07-23 14:46:56 +08:00
|
|
|
if (Literal->isWide())
|
|
|
|
// FIXME: We currently leak memory here.
|
|
|
|
return Diag(Literal->getLocStart(), diag::err_asm_wide_character,
|
|
|
|
Literal->getSourceRange());
|
2007-11-27 12:11:28 +08:00
|
|
|
|
|
|
|
std::string InputConstraint(Literal->getStrData(),
|
|
|
|
Literal->getByteLength());
|
|
|
|
|
|
|
|
TargetInfo::ConstraintInfo info;
|
|
|
|
if (!Context.Target.validateInputConstraint(InputConstraint.c_str(),
|
2008-08-19 03:55:17 +08:00
|
|
|
NumOutputs, info)) {
|
2007-11-27 12:11:28 +08:00
|
|
|
// FIXME: We currently leak memory here.
|
2008-07-23 14:46:56 +08:00
|
|
|
return Diag(Literal->getLocStart(),
|
2008-08-19 03:55:17 +08:00
|
|
|
diag::err_asm_invalid_input_constraint, InputConstraint);
|
2007-11-27 12:11:28 +08:00
|
|
|
}
|
2007-11-24 03:43:50 +08:00
|
|
|
|
2007-11-27 12:11:28 +08:00
|
|
|
// Check that the input exprs aren't of type void.
|
2008-08-19 03:55:17 +08:00
|
|
|
ParenExpr *InputExpr = cast<ParenExpr>(Exprs[i]);
|
2007-11-24 03:43:50 +08:00
|
|
|
if (InputExpr->getType()->isVoidType()) {
|
|
|
|
|
|
|
|
// FIXME: We currently leak memory here.
|
2008-08-19 03:55:17 +08:00
|
|
|
return Diag(InputExpr->getSubExpr()->getLocStart(),
|
|
|
|
diag::err_asm_invalid_type_in_input,
|
|
|
|
InputExpr->getType().getAsString(), InputConstraint,
|
|
|
|
InputExpr->getSubExpr()->getSourceRange());
|
2007-11-24 03:43:50 +08:00
|
|
|
}
|
|
|
|
}
|
2007-11-22 09:36:19 +08:00
|
|
|
|
2007-11-25 08:25:21 +08:00
|
|
|
// Check that the clobbers are valid.
|
2008-08-19 03:55:17 +08:00
|
|
|
for (unsigned i = 0; i != NumClobbers; i++) {
|
|
|
|
StringLiteral *Literal = Clobbers[i];
|
2008-07-23 14:46:56 +08:00
|
|
|
if (Literal->isWide())
|
|
|
|
// FIXME: We currently leak memory here.
|
|
|
|
return Diag(Literal->getLocStart(), diag::err_asm_wide_character,
|
|
|
|
Literal->getSourceRange());
|
2007-11-25 08:25:21 +08:00
|
|
|
|
|
|
|
llvm::SmallString<16> Clobber(Literal->getStrData(),
|
|
|
|
Literal->getStrData() +
|
|
|
|
Literal->getByteLength());
|
|
|
|
|
2008-07-23 14:46:56 +08:00
|
|
|
if (!Context.Target.isValidGCCRegisterName(Clobber.c_str()))
|
2007-11-25 08:25:21 +08:00
|
|
|
// FIXME: We currently leak memory here.
|
2008-07-23 14:46:56 +08:00
|
|
|
return Diag(Literal->getLocStart(),
|
2008-08-19 03:55:17 +08:00
|
|
|
diag::err_asm_unknown_register_name, Clobber.c_str());
|
2007-11-25 08:25:21 +08:00
|
|
|
}
|
|
|
|
|
2008-08-19 03:55:17 +08:00
|
|
|
return new AsmStmt(AsmLoc, IsSimple, IsVolatile, NumOutputs, NumInputs,
|
|
|
|
Names, Constraints, Exprs, AsmString, NumClobbers,
|
|
|
|
Clobbers, RParenLoc);
|
2007-10-29 12:04:16 +08:00
|
|
|
}
|
2007-11-02 07:59:59 +08:00
|
|
|
|
|
|
|
Action::StmtResult
|
2008-01-08 03:49:32 +08:00
|
|
|
Sema::ActOnObjCAtCatchStmt(SourceLocation AtLoc,
|
2007-11-02 07:59:59 +08:00
|
|
|
SourceLocation RParen, StmtTy *Parm,
|
|
|
|
StmtTy *Body, StmtTy *CatchList) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCAtCatchStmt *CS = new ObjCAtCatchStmt(AtLoc, RParen,
|
2007-11-02 07:59:59 +08:00
|
|
|
static_cast<Stmt*>(Parm), static_cast<Stmt*>(Body),
|
|
|
|
static_cast<Stmt*>(CatchList));
|
|
|
|
return CatchList ? CatchList : CS;
|
|
|
|
}
|
|
|
|
|
2007-11-02 08:18:53 +08:00
|
|
|
Action::StmtResult
|
2008-01-08 03:49:32 +08:00
|
|
|
Sema::ActOnObjCAtFinallyStmt(SourceLocation AtLoc, StmtTy *Body) {
|
|
|
|
ObjCAtFinallyStmt *FS = new ObjCAtFinallyStmt(AtLoc,
|
2007-11-02 08:18:53 +08:00
|
|
|
static_cast<Stmt*>(Body));
|
|
|
|
return FS;
|
|
|
|
}
|
2007-11-02 23:39:31 +08:00
|
|
|
|
|
|
|
Action::StmtResult
|
2008-01-08 03:49:32 +08:00
|
|
|
Sema::ActOnObjCAtTryStmt(SourceLocation AtLoc,
|
2007-11-02 23:39:31 +08:00
|
|
|
StmtTy *Try, StmtTy *Catch, StmtTy *Finally) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCAtTryStmt *TS = new ObjCAtTryStmt(AtLoc, static_cast<Stmt*>(Try),
|
2007-11-02 23:39:31 +08:00
|
|
|
static_cast<Stmt*>(Catch),
|
|
|
|
static_cast<Stmt*>(Finally));
|
|
|
|
return TS;
|
|
|
|
}
|
|
|
|
|
2007-11-07 10:00:49 +08:00
|
|
|
Action::StmtResult
|
2008-01-08 03:49:32 +08:00
|
|
|
Sema::ActOnObjCAtThrowStmt(SourceLocation AtLoc, StmtTy *Throw) {
|
|
|
|
ObjCAtThrowStmt *TS = new ObjCAtThrowStmt(AtLoc, static_cast<Stmt*>(Throw));
|
2007-11-07 10:00:49 +08:00
|
|
|
return TS;
|
|
|
|
}
|
2007-11-02 23:39:31 +08:00
|
|
|
|
2008-01-30 03:14:59 +08:00
|
|
|
Action::StmtResult
|
|
|
|
Sema::ActOnObjCAtSynchronizedStmt(SourceLocation AtLoc, ExprTy *SynchExpr,
|
|
|
|
StmtTy *SynchBody) {
|
|
|
|
ObjCAtSynchronizedStmt *SS = new ObjCAtSynchronizedStmt(AtLoc,
|
2008-01-30 06:59:37 +08:00
|
|
|
static_cast<Stmt*>(SynchExpr), static_cast<Stmt*>(SynchBody));
|
2008-01-30 03:14:59 +08:00
|
|
|
return SS;
|
|
|
|
}
|