2006-11-10 05:07:45 +00:00
|
|
|
//===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements semantic analysis for statements.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Sema.h"
|
|
|
|
#include "clang/AST/Stmt.h"
|
2007-05-08 21:09:37 +00:00
|
|
|
#include "clang/AST/Expr.h"
|
2006-11-10 05:17:58 +00:00
|
|
|
#include "clang/Parse/Scope.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2007-05-29 14:23:36 +00:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
|
|
|
#include "clang/Lex/IdentifierTable.h"
|
2006-11-10 05:07:45 +00:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
2007-05-28 01:45:28 +00:00
|
|
|
Sema::StmtResult Sema::ParseNullStmt(SourceLocation SemiLoc) {
|
|
|
|
return new NullStmt(SemiLoc);
|
|
|
|
}
|
|
|
|
|
2007-05-29 22:59:26 +00:00
|
|
|
Sema::StmtResult Sema::ParseDeclStmt(DeclTy *decl) {
|
2007-05-30 04:20:12 +00:00
|
|
|
if (decl)
|
|
|
|
return new DeclStmt(static_cast<Decl *>(decl));
|
|
|
|
else
|
|
|
|
return true; // error
|
2007-05-29 22:59:26 +00:00
|
|
|
}
|
2006-11-10 05:07:45 +00:00
|
|
|
|
|
|
|
Action::StmtResult
|
|
|
|
Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R,
|
|
|
|
StmtTy **Elts, unsigned NumElts) {
|
2007-05-20 22:47:04 +00:00
|
|
|
return new CompoundStmt((Stmt**)Elts, NumElts);
|
2006-11-10 05:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Action::StmtResult
|
|
|
|
Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal,
|
|
|
|
SourceLocation DotDotDotLoc, ExprTy *RHSVal,
|
|
|
|
SourceLocation ColonLoc, StmtTy *SubStmt) {
|
2007-05-08 21:09:37 +00:00
|
|
|
assert((LHSVal != 0) && "missing expression in case statement");
|
|
|
|
|
2007-05-18 00:18:52 +00:00
|
|
|
SourceLocation expLoc;
|
2007-05-08 21:09:37 +00:00
|
|
|
// C99 6.8.4.2p3: The expression shall be an integer constant.
|
2007-05-18 00:18:52 +00:00
|
|
|
if (!((Expr *)LHSVal)->isIntegerConstantExpr(expLoc))
|
2007-05-08 21:09:37 +00:00
|
|
|
return Diag(CaseLoc, diag::err_case_label_not_integer_constant_expr);
|
|
|
|
|
2006-11-10 05:07:45 +00:00
|
|
|
return new CaseStmt((Expr*)LHSVal, (Expr*)RHSVal, (Stmt*)SubStmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
Action::StmtResult
|
|
|
|
Sema::ParseDefaultStmt(SourceLocation DefaultLoc,
|
|
|
|
SourceLocation ColonLoc, StmtTy *SubStmt) {
|
|
|
|
return new DefaultStmt((Stmt*)SubStmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
Action::StmtResult
|
|
|
|
Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II,
|
|
|
|
SourceLocation ColonLoc, StmtTy *SubStmt) {
|
2007-05-28 06:28:18 +00: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)
|
|
|
|
return LabelDecl = new LabelStmt(IdentLoc, II, (Stmt*)SubStmt);
|
|
|
|
|
2007-05-28 06:56:27 +00:00
|
|
|
assert(LabelDecl->getID() == II && "Label mismatch!");
|
2007-05-28 06:28:18 +00:00
|
|
|
|
|
|
|
// Otherwise, this label was either forward reference or multiply defined. If
|
|
|
|
// multiply defined, reject it now.
|
|
|
|
if (LabelDecl->getSubStmt()) {
|
2007-05-28 06:56:27 +00:00
|
|
|
Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName());
|
2007-05-28 06:28:18 +00:00
|
|
|
Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition);
|
|
|
|
return (Stmt*)SubStmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, this label was forward declared, and we just found its real
|
|
|
|
// definition. Fill in the forward definition and return it.
|
|
|
|
LabelDecl->setIdentLoc(IdentLoc);
|
|
|
|
LabelDecl->setSubStmt((Stmt*)SubStmt);
|
|
|
|
return LabelDecl;
|
2006-11-10 05:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Action::StmtResult
|
|
|
|
Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal,
|
|
|
|
StmtTy *ThenVal, SourceLocation ElseLoc,
|
|
|
|
StmtTy *ElseVal) {
|
2007-05-29 02:14:17 +00:00
|
|
|
Expr *condExpr = (Expr *)CondVal;
|
|
|
|
assert(condExpr && "ParseIfStmt(): missing expression");
|
|
|
|
|
|
|
|
QualType condType = condExpr->getType();
|
|
|
|
assert(!condType.isNull() && "ParseIfStmt(): missing expression type");
|
|
|
|
|
|
|
|
if (!condType->isScalarType()) // C99 6.8.4.1p1
|
|
|
|
return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar,
|
|
|
|
condType.getAsString(), condExpr->getSourceRange());
|
|
|
|
|
|
|
|
return new IfStmt(condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal);
|
2006-11-10 05:07:45 +00:00
|
|
|
}
|
2007-05-29 02:14:17 +00:00
|
|
|
|
2006-11-10 05:07:45 +00:00
|
|
|
Action::StmtResult
|
|
|
|
Sema::ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond, StmtTy *Body) {
|
|
|
|
return new SwitchStmt((Expr*)Cond, (Stmt*)Body);
|
|
|
|
}
|
|
|
|
|
|
|
|
Action::StmtResult
|
2007-05-29 02:14:17 +00:00
|
|
|
Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) {
|
|
|
|
Expr *condExpr = (Expr *)Cond;
|
|
|
|
assert(condExpr && "ParseWhileStmt(): missing expression");
|
|
|
|
|
|
|
|
QualType condType = condExpr->getType();
|
|
|
|
assert(!condType.isNull() && "ParseWhileStmt(): missing expression type");
|
|
|
|
|
|
|
|
if (!condType->isScalarType()) // C99 6.8.5p2
|
|
|
|
return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar,
|
|
|
|
condType.getAsString(), condExpr->getSourceRange());
|
|
|
|
|
|
|
|
return new WhileStmt(condExpr, (Stmt*)Body);
|
2006-11-10 05:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Action::StmtResult
|
|
|
|
Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body,
|
|
|
|
SourceLocation WhileLoc, ExprTy *Cond) {
|
2007-05-29 02:14:17 +00:00
|
|
|
Expr *condExpr = (Expr *)Cond;
|
|
|
|
assert(condExpr && "ParseDoStmt(): missing expression");
|
|
|
|
|
|
|
|
QualType condType = condExpr->getType();
|
|
|
|
assert(!condType.isNull() && "ParseDoStmt(): missing expression type");
|
|
|
|
|
|
|
|
if (!condType->isScalarType()) // C99 6.8.5p2
|
|
|
|
return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar,
|
|
|
|
condType.getAsString(), condExpr->getSourceRange());
|
|
|
|
|
|
|
|
return new DoStmt((Stmt*)Body, condExpr);
|
2006-11-10 05:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Action::StmtResult
|
|
|
|
Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc,
|
|
|
|
StmtTy *First, ExprTy *Second, ExprTy *Third,
|
|
|
|
SourceLocation RParenLoc, StmtTy *Body) {
|
2007-05-29 02:14:17 +00:00
|
|
|
if (First) {
|
|
|
|
// C99 6.8.5p3: FIXME. Need to hack Parser::ParseForStatement() and
|
|
|
|
// declaration support to create a DeclStmt node. Once this is done,
|
|
|
|
// we can test for DeclStmt vs. Expr (already a sub-class of Stmt).
|
|
|
|
}
|
|
|
|
if (Second) {
|
|
|
|
Expr *testExpr = (Expr *)Second;
|
|
|
|
QualType testType = testExpr->getType();
|
|
|
|
assert(!testType.isNull() && "ParseForStmt(): missing test expression type");
|
|
|
|
|
|
|
|
if (!testType->isScalarType()) // C99 6.8.5p2
|
|
|
|
return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar,
|
|
|
|
testType.getAsString(), testExpr->getSourceRange());
|
|
|
|
}
|
2006-11-10 05:07:45 +00:00
|
|
|
return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Action::StmtResult
|
|
|
|
Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc,
|
|
|
|
IdentifierInfo *LabelII) {
|
2007-05-28 06:28:18 +00: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);
|
|
|
|
|
|
|
|
return new GotoStmt(LabelDecl);
|
2006-11-10 05:07:45 +00:00
|
|
|
}
|
|
|
|
Action::StmtResult
|
|
|
|
Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc,
|
|
|
|
ExprTy *DestExp) {
|
|
|
|
return new IndirectGotoStmt((Expr*)DestExp);
|
|
|
|
}
|
|
|
|
|
|
|
|
Action::StmtResult
|
2006-11-10 05:17:58 +00:00
|
|
|
Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Remember that this continue goes with this loop.
|
2006-11-10 05:07:45 +00:00
|
|
|
return new ContinueStmt();
|
|
|
|
}
|
|
|
|
|
|
|
|
Action::StmtResult
|
2006-11-10 05:17:58 +00:00
|
|
|
Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Remember that this break goes with this loop/switch.
|
2006-11-10 05:07:45 +00:00
|
|
|
return new BreakStmt();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Action::StmtResult
|
|
|
|
Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *RetValExp) {
|
2007-05-27 23:58:33 +00:00
|
|
|
QualType lhsType = CurFunctionDecl->getResultType();
|
|
|
|
|
|
|
|
if (lhsType->isVoidType()) {
|
2007-05-29 14:23:36 +00:00
|
|
|
if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns)
|
|
|
|
Diag(ReturnLoc, diag::ext_return_has_expr,
|
|
|
|
CurFunctionDecl->getIdentifier()->getName(),
|
|
|
|
((Expr *)RetValExp)->getSourceRange());
|
|
|
|
return new ReturnStmt((Expr*)RetValExp);
|
|
|
|
} else {
|
|
|
|
if (!RetValExp) {
|
|
|
|
const char *funcName = CurFunctionDecl->getIdentifier()->getName();
|
|
|
|
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);
|
|
|
|
return new ReturnStmt((Expr*)0);
|
|
|
|
}
|
2007-05-27 23:58:33 +00:00
|
|
|
}
|
2007-05-29 14:23:36 +00:00
|
|
|
// we have a non-void function with an expression, continue checking
|
2007-05-27 23:58:33 +00:00
|
|
|
QualType rhsType = ((Expr *)RetValExp)->getType();
|
|
|
|
|
|
|
|
if (lhsType == rhsType) // common case, fast path...
|
|
|
|
return new ReturnStmt((Expr*)RetValExp);
|
2007-05-29 14:23:36 +00: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.
|
2007-05-27 23:58:33 +00:00
|
|
|
AssignmentConversionResult result;
|
|
|
|
QualType resType = UsualAssignmentConversions(lhsType, rhsType, result);
|
|
|
|
bool hadError = false;
|
|
|
|
|
|
|
|
// decode the result (notice that extensions still return a type).
|
|
|
|
switch (result) {
|
|
|
|
case Compatible:
|
|
|
|
break;
|
|
|
|
case Incompatible:
|
|
|
|
Diag(ReturnLoc, diag::err_typecheck_return_incompatible,
|
|
|
|
lhsType.getAsString(), rhsType.getAsString(),
|
|
|
|
((Expr *)RetValExp)->getSourceRange());
|
|
|
|
hadError = true;
|
|
|
|
break;
|
|
|
|
case PointerFromInt:
|
|
|
|
// check for null pointer constant (C99 6.3.2.3p3)
|
|
|
|
if (!((Expr *)RetValExp)->isNullPointerConstant())
|
2007-05-30 04:20:12 +00:00
|
|
|
Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
|
|
|
|
lhsType.getAsString(), rhsType.getAsString(),
|
|
|
|
((Expr *)RetValExp)->getSourceRange());
|
2007-05-27 23:58:33 +00:00
|
|
|
break;
|
|
|
|
case IntFromPointer:
|
2007-05-30 04:20:12 +00:00
|
|
|
Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
|
|
|
|
lhsType.getAsString(), rhsType.getAsString(),
|
|
|
|
((Expr *)RetValExp)->getSourceRange());
|
2007-05-27 23:58:33 +00:00
|
|
|
break;
|
|
|
|
case IncompatiblePointer:
|
2007-05-30 04:20:12 +00:00
|
|
|
Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer,
|
|
|
|
lhsType.getAsString(), rhsType.getAsString(),
|
|
|
|
((Expr *)RetValExp)->getSourceRange());
|
2007-05-27 23:58:33 +00:00
|
|
|
break;
|
|
|
|
case CompatiblePointerDiscardsQualifiers:
|
2007-05-30 04:20:12 +00:00
|
|
|
Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers,
|
|
|
|
lhsType.getAsString(), rhsType.getAsString(),
|
|
|
|
((Expr *)RetValExp)->getSourceRange());
|
2007-05-27 23:58:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-11-10 05:07:45 +00:00
|
|
|
return new ReturnStmt((Expr*)RetValExp);
|
|
|
|
}
|
|
|
|
|