Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 1 | //===--- SemaStmt.cpp - Semantic Analysis for Statements ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for statements. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/Stmt.h" |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 17 | #include "clang/Parse/Scope.h" |
| 18 | #include "clang/Basic/Diagnostic.h" |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 19 | #include "clang/Basic/LangOptions.h" |
| 20 | #include "clang/Lex/IdentifierTable.h" |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | using namespace clang; |
| 23 | |
Chris Lattner | 0f203a7 | 2007-05-28 01:45:28 +0000 | [diff] [blame] | 24 | Sema::StmtResult Sema::ParseNullStmt(SourceLocation SemiLoc) { |
| 25 | return new NullStmt(SemiLoc); |
| 26 | } |
| 27 | |
Steve Naroff | 2a8ad18 | 2007-05-29 22:59:26 +0000 | [diff] [blame] | 28 | Sema::StmtResult Sema::ParseDeclStmt(DeclTy *decl) { |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame^] | 29 | if (decl) |
| 30 | return new DeclStmt(static_cast<Decl *>(decl)); |
| 31 | else |
| 32 | return true; // error |
Steve Naroff | 2a8ad18 | 2007-05-29 22:59:26 +0000 | [diff] [blame] | 33 | } |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 34 | |
| 35 | Action::StmtResult |
| 36 | Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R, |
| 37 | StmtTy **Elts, unsigned NumElts) { |
Chris Lattner | 3bbe7be | 2007-05-20 22:47:04 +0000 | [diff] [blame] | 38 | return new CompoundStmt((Stmt**)Elts, NumElts); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | Action::StmtResult |
| 42 | Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal, |
| 43 | SourceLocation DotDotDotLoc, ExprTy *RHSVal, |
| 44 | SourceLocation ColonLoc, StmtTy *SubStmt) { |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 45 | assert((LHSVal != 0) && "missing expression in case statement"); |
| 46 | |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 47 | SourceLocation expLoc; |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 48 | // C99 6.8.4.2p3: The expression shall be an integer constant. |
Steve Naroff | 72cada0 | 2007-05-18 00:18:52 +0000 | [diff] [blame] | 49 | if (!((Expr *)LHSVal)->isIntegerConstantExpr(expLoc)) |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 50 | return Diag(CaseLoc, diag::err_case_label_not_integer_constant_expr); |
| 51 | |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 52 | return new CaseStmt((Expr*)LHSVal, (Expr*)RHSVal, (Stmt*)SubStmt); |
| 53 | } |
| 54 | |
| 55 | Action::StmtResult |
| 56 | Sema::ParseDefaultStmt(SourceLocation DefaultLoc, |
| 57 | SourceLocation ColonLoc, StmtTy *SubStmt) { |
| 58 | return new DefaultStmt((Stmt*)SubStmt); |
| 59 | } |
| 60 | |
| 61 | Action::StmtResult |
| 62 | Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II, |
| 63 | SourceLocation ColonLoc, StmtTy *SubStmt) { |
Chris Lattner | e247306 | 2007-05-28 06:28:18 +0000 | [diff] [blame] | 64 | // Look up the record for this label identifier. |
| 65 | LabelStmt *&LabelDecl = LabelMap[II]; |
| 66 | |
| 67 | // If not forward referenced or defined already, just create a new LabelStmt. |
| 68 | if (LabelDecl == 0) |
| 69 | return LabelDecl = new LabelStmt(IdentLoc, II, (Stmt*)SubStmt); |
| 70 | |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 71 | assert(LabelDecl->getID() == II && "Label mismatch!"); |
Chris Lattner | e247306 | 2007-05-28 06:28:18 +0000 | [diff] [blame] | 72 | |
| 73 | // Otherwise, this label was either forward reference or multiply defined. If |
| 74 | // multiply defined, reject it now. |
| 75 | if (LabelDecl->getSubStmt()) { |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 76 | Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName()); |
Chris Lattner | e247306 | 2007-05-28 06:28:18 +0000 | [diff] [blame] | 77 | Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition); |
| 78 | return (Stmt*)SubStmt; |
| 79 | } |
| 80 | |
| 81 | // Otherwise, this label was forward declared, and we just found its real |
| 82 | // definition. Fill in the forward definition and return it. |
| 83 | LabelDecl->setIdentLoc(IdentLoc); |
| 84 | LabelDecl->setSubStmt((Stmt*)SubStmt); |
| 85 | return LabelDecl; |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | Action::StmtResult |
| 89 | Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal, |
| 90 | StmtTy *ThenVal, SourceLocation ElseLoc, |
| 91 | StmtTy *ElseVal) { |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 92 | Expr *condExpr = (Expr *)CondVal; |
| 93 | assert(condExpr && "ParseIfStmt(): missing expression"); |
| 94 | |
| 95 | QualType condType = condExpr->getType(); |
| 96 | assert(!condType.isNull() && "ParseIfStmt(): missing expression type"); |
| 97 | |
| 98 | if (!condType->isScalarType()) // C99 6.8.4.1p1 |
| 99 | return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar, |
| 100 | condType.getAsString(), condExpr->getSourceRange()); |
| 101 | |
| 102 | return new IfStmt(condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 103 | } |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 104 | |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 105 | Action::StmtResult |
| 106 | Sema::ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond, StmtTy *Body) { |
| 107 | return new SwitchStmt((Expr*)Cond, (Stmt*)Body); |
| 108 | } |
| 109 | |
| 110 | Action::StmtResult |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 111 | Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) { |
| 112 | Expr *condExpr = (Expr *)Cond; |
| 113 | assert(condExpr && "ParseWhileStmt(): missing expression"); |
| 114 | |
| 115 | QualType condType = condExpr->getType(); |
| 116 | assert(!condType.isNull() && "ParseWhileStmt(): missing expression type"); |
| 117 | |
| 118 | if (!condType->isScalarType()) // C99 6.8.5p2 |
| 119 | return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar, |
| 120 | condType.getAsString(), condExpr->getSourceRange()); |
| 121 | |
| 122 | return new WhileStmt(condExpr, (Stmt*)Body); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | Action::StmtResult |
| 126 | Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body, |
| 127 | SourceLocation WhileLoc, ExprTy *Cond) { |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 128 | Expr *condExpr = (Expr *)Cond; |
| 129 | assert(condExpr && "ParseDoStmt(): missing expression"); |
| 130 | |
| 131 | QualType condType = condExpr->getType(); |
| 132 | assert(!condType.isNull() && "ParseDoStmt(): missing expression type"); |
| 133 | |
| 134 | if (!condType->isScalarType()) // C99 6.8.5p2 |
| 135 | return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar, |
| 136 | condType.getAsString(), condExpr->getSourceRange()); |
| 137 | |
| 138 | return new DoStmt((Stmt*)Body, condExpr); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | Action::StmtResult |
| 142 | Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, |
| 143 | StmtTy *First, ExprTy *Second, ExprTy *Third, |
| 144 | SourceLocation RParenLoc, StmtTy *Body) { |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 145 | if (First) { |
| 146 | // C99 6.8.5p3: FIXME. Need to hack Parser::ParseForStatement() and |
| 147 | // declaration support to create a DeclStmt node. Once this is done, |
| 148 | // we can test for DeclStmt vs. Expr (already a sub-class of Stmt). |
| 149 | } |
| 150 | if (Second) { |
| 151 | Expr *testExpr = (Expr *)Second; |
| 152 | QualType testType = testExpr->getType(); |
| 153 | assert(!testType.isNull() && "ParseForStmt(): missing test expression type"); |
| 154 | |
| 155 | if (!testType->isScalarType()) // C99 6.8.5p2 |
| 156 | return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar, |
| 157 | testType.getAsString(), testExpr->getSourceRange()); |
| 158 | } |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 159 | return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | Action::StmtResult |
| 164 | Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, |
| 165 | IdentifierInfo *LabelII) { |
Chris Lattner | e247306 | 2007-05-28 06:28:18 +0000 | [diff] [blame] | 166 | // Look up the record for this label identifier. |
| 167 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 168 | |
| 169 | // If we haven't seen this label yet, create a forward reference. |
| 170 | if (LabelDecl == 0) |
| 171 | LabelDecl = new LabelStmt(LabelLoc, LabelII, 0); |
| 172 | |
| 173 | return new GotoStmt(LabelDecl); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 174 | } |
| 175 | Action::StmtResult |
| 176 | Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc, |
| 177 | ExprTy *DestExp) { |
| 178 | return new IndirectGotoStmt((Expr*)DestExp); |
| 179 | } |
| 180 | |
| 181 | Action::StmtResult |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 182 | Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { |
| 183 | Scope *S = CurScope->getContinueParent(); |
| 184 | if (!S) { |
| 185 | // C99 6.8.6.2p1: A break shall appear only in or as a loop body. |
| 186 | Diag(ContinueLoc, diag::err_continue_not_in_loop); |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | // FIXME: Remember that this continue goes with this loop. |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 191 | return new ContinueStmt(); |
| 192 | } |
| 193 | |
| 194 | Action::StmtResult |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 195 | Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { |
| 196 | Scope *S = CurScope->getBreakParent(); |
| 197 | if (!S) { |
| 198 | // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body. |
| 199 | Diag(BreakLoc, diag::err_break_not_in_loop_or_switch); |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | // FIXME: Remember that this break goes with this loop/switch. |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 204 | return new BreakStmt(); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | Action::StmtResult |
| 209 | Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *RetValExp) { |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 210 | QualType lhsType = CurFunctionDecl->getResultType(); |
| 211 | |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 212 | if (lhsType->isVoidType()) { |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 213 | if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 214 | Diag(ReturnLoc, diag::ext_return_has_expr, |
| 215 | CurFunctionDecl->getIdentifier()->getName(), |
| 216 | ((Expr *)RetValExp)->getSourceRange()); |
| 217 | return new ReturnStmt((Expr*)RetValExp); |
| 218 | } else { |
| 219 | if (!RetValExp) { |
| 220 | const char *funcName = CurFunctionDecl->getIdentifier()->getName(); |
| 221 | if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 222 | Diag(ReturnLoc, diag::ext_return_missing_expr, funcName); |
| 223 | else // C90 6.6.6.4p4 |
| 224 | Diag(ReturnLoc, diag::warn_return_missing_expr, funcName); |
| 225 | return new ReturnStmt((Expr*)0); |
| 226 | } |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 227 | } |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 228 | // we have a non-void function with an expression, continue checking |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 229 | QualType rhsType = ((Expr *)RetValExp)->getType(); |
| 230 | |
| 231 | if (lhsType == rhsType) // common case, fast path... |
| 232 | return new ReturnStmt((Expr*)RetValExp); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 233 | |
| 234 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 235 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
| 236 | // function return. |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 237 | AssignmentConversionResult result; |
| 238 | QualType resType = UsualAssignmentConversions(lhsType, rhsType, result); |
| 239 | bool hadError = false; |
| 240 | |
| 241 | // decode the result (notice that extensions still return a type). |
| 242 | switch (result) { |
| 243 | case Compatible: |
| 244 | break; |
| 245 | case Incompatible: |
| 246 | Diag(ReturnLoc, diag::err_typecheck_return_incompatible, |
| 247 | lhsType.getAsString(), rhsType.getAsString(), |
| 248 | ((Expr *)RetValExp)->getSourceRange()); |
| 249 | hadError = true; |
| 250 | break; |
| 251 | case PointerFromInt: |
| 252 | // check for null pointer constant (C99 6.3.2.3p3) |
| 253 | if (!((Expr *)RetValExp)->isNullPointerConstant()) |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame^] | 254 | Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int, |
| 255 | lhsType.getAsString(), rhsType.getAsString(), |
| 256 | ((Expr *)RetValExp)->getSourceRange()); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 257 | break; |
| 258 | case IntFromPointer: |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame^] | 259 | Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int, |
| 260 | lhsType.getAsString(), rhsType.getAsString(), |
| 261 | ((Expr *)RetValExp)->getSourceRange()); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 262 | break; |
| 263 | case IncompatiblePointer: |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame^] | 264 | Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer, |
| 265 | lhsType.getAsString(), rhsType.getAsString(), |
| 266 | ((Expr *)RetValExp)->getSourceRange()); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 267 | break; |
| 268 | case CompatiblePointerDiscardsQualifiers: |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame^] | 269 | Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers, |
| 270 | lhsType.getAsString(), rhsType.getAsString(), |
| 271 | ((Expr *)RetValExp)->getSourceRange()); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 272 | break; |
| 273 | } |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 274 | return new ReturnStmt((Expr*)RetValExp); |
| 275 | } |
| 276 | |