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" |
Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 21 | |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 24 | Sema::StmtResult Sema::ParseExprStmt(ExprTy *expr) { |
| 25 | Expr *E = static_cast<Expr*>(expr); |
| 26 | |
| 27 | // Exprs are statements, so there is no need to do a conversion here. However, |
| 28 | // diagnose some potentially bad code. |
| 29 | if (!E->hasLocalSideEffect()) |
Chris Lattner | 9b3b9a1 | 2007-06-27 06:08:24 +0000 | [diff] [blame] | 30 | Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange()); |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 31 | |
| 32 | return E; |
| 33 | } |
| 34 | |
| 35 | |
Chris Lattner | 0f203a7 | 2007-05-28 01:45:28 +0000 | [diff] [blame] | 36 | Sema::StmtResult Sema::ParseNullStmt(SourceLocation SemiLoc) { |
| 37 | return new NullStmt(SemiLoc); |
| 38 | } |
| 39 | |
Steve Naroff | 2a8ad18 | 2007-05-29 22:59:26 +0000 | [diff] [blame] | 40 | Sema::StmtResult Sema::ParseDeclStmt(DeclTy *decl) { |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 41 | if (decl) |
| 42 | return new DeclStmt(static_cast<Decl *>(decl)); |
| 43 | else |
| 44 | return true; // error |
Steve Naroff | 2a8ad18 | 2007-05-29 22:59:26 +0000 | [diff] [blame] | 45 | } |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 46 | |
| 47 | Action::StmtResult |
| 48 | Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R, |
| 49 | StmtTy **Elts, unsigned NumElts) { |
Chris Lattner | 3bbe7be | 2007-05-20 22:47:04 +0000 | [diff] [blame] | 50 | return new CompoundStmt((Stmt**)Elts, NumElts); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | Action::StmtResult |
Chris Lattner | 0c46c5d | 2007-06-02 23:53:17 +0000 | [diff] [blame] | 54 | Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval, |
Chris Lattner | 46eeb22 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 55 | SourceLocation DotDotDotLoc, ExprTy *rhsval, |
Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 56 | SourceLocation ColonLoc, StmtTy *subStmt) { |
| 57 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
Chris Lattner | 54f4d2b | 2007-07-23 17:05:23 +0000 | [diff] [blame^] | 58 | Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval); |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 59 | assert((LHSVal != 0) && "missing expression in case statement"); |
Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 0c46c5d | 2007-06-02 23:53:17 +0000 | [diff] [blame] | 61 | SourceLocation ExpLoc; |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 62 | // C99 6.8.4.2p3: The expression shall be an integer constant. |
Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 63 | if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) { |
| 64 | Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr, |
| 65 | LHSVal->getSourceRange()); |
| 66 | return SubStmt; |
| 67 | } |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 46eeb22 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 69 | // GCC extension: The expression shall be an integer constant. |
Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 70 | if (RHSVal && !RHSVal->isIntegerConstantExpr(Context, &ExpLoc)) { |
| 71 | Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr, |
| 72 | RHSVal->getSourceRange()); |
| 73 | return SubStmt; |
Chris Lattner | 46eeb22 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 74 | } |
Chris Lattner | 54f4d2b | 2007-07-23 17:05:23 +0000 | [diff] [blame^] | 75 | |
| 76 | if (SwitchStack.empty()) { |
| 77 | Diag(CaseLoc, diag::err_case_not_in_switch); |
| 78 | return SubStmt; |
| 79 | } |
Chris Lattner | 35e287b | 2007-06-03 01:44:43 +0000 | [diff] [blame] | 80 | |
Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 81 | CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt); |
Chris Lattner | 54f4d2b | 2007-07-23 17:05:23 +0000 | [diff] [blame^] | 82 | SwitchStack.back()->addSwitchCase(CS); |
Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 83 | return CS; |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | Action::StmtResult |
Chris Lattner | 46eeb22 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 87 | Sema::ParseDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, |
Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 88 | StmtTy *subStmt, Scope *CurScope) { |
| 89 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
Chris Lattner | 46eeb22 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 54f4d2b | 2007-07-23 17:05:23 +0000 | [diff] [blame^] | 91 | if (SwitchStack.empty()) { |
Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 92 | Diag(DefaultLoc, diag::err_default_not_in_switch); |
| 93 | return SubStmt; |
| 94 | } |
| 95 | |
Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 96 | DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt); |
Chris Lattner | 54f4d2b | 2007-07-23 17:05:23 +0000 | [diff] [blame^] | 97 | SwitchStack.back()->addSwitchCase(DS); |
Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 46eeb22 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 99 | return DS; |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | Action::StmtResult |
| 103 | Sema::ParseLabelStmt(SourceLocation IdentLoc, IdentifierInfo *II, |
Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 104 | SourceLocation ColonLoc, StmtTy *subStmt) { |
| 105 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
Chris Lattner | e247306 | 2007-05-28 06:28:18 +0000 | [diff] [blame] | 106 | // Look up the record for this label identifier. |
| 107 | LabelStmt *&LabelDecl = LabelMap[II]; |
| 108 | |
| 109 | // If not forward referenced or defined already, just create a new LabelStmt. |
| 110 | if (LabelDecl == 0) |
Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 111 | return LabelDecl = new LabelStmt(IdentLoc, II, SubStmt); |
Chris Lattner | e247306 | 2007-05-28 06:28:18 +0000 | [diff] [blame] | 112 | |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 113 | assert(LabelDecl->getID() == II && "Label mismatch!"); |
Chris Lattner | e247306 | 2007-05-28 06:28:18 +0000 | [diff] [blame] | 114 | |
| 115 | // Otherwise, this label was either forward reference or multiply defined. If |
| 116 | // multiply defined, reject it now. |
| 117 | if (LabelDecl->getSubStmt()) { |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 118 | Diag(IdentLoc, diag::err_redefinition_of_label, LabelDecl->getName()); |
Chris Lattner | e247306 | 2007-05-28 06:28:18 +0000 | [diff] [blame] | 119 | Diag(LabelDecl->getIdentLoc(), diag::err_previous_definition); |
Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 120 | return SubStmt; |
Chris Lattner | e247306 | 2007-05-28 06:28:18 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // Otherwise, this label was forward declared, and we just found its real |
| 124 | // definition. Fill in the forward definition and return it. |
| 125 | LabelDecl->setIdentLoc(IdentLoc); |
Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame] | 126 | LabelDecl->setSubStmt(SubStmt); |
Chris Lattner | e247306 | 2007-05-28 06:28:18 +0000 | [diff] [blame] | 127 | return LabelDecl; |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | Action::StmtResult |
| 131 | Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal, |
| 132 | StmtTy *ThenVal, SourceLocation ElseLoc, |
| 133 | StmtTy *ElseVal) { |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 134 | Expr *condExpr = (Expr *)CondVal; |
| 135 | assert(condExpr && "ParseIfStmt(): missing expression"); |
| 136 | |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 137 | DefaultFunctionArrayConversion(condExpr); |
| 138 | QualType condType = condExpr->getType(); |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 139 | |
| 140 | if (!condType->isScalarType()) // C99 6.8.4.1p1 |
| 141 | return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar, |
| 142 | condType.getAsString(), condExpr->getSourceRange()); |
| 143 | |
| 144 | return new IfStmt(condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 145 | } |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 146 | |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 147 | Action::StmtResult |
Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 148 | Sema::StartSwitchStmt(ExprTy *Cond) { |
| 149 | SwitchStmt *SS = new SwitchStmt((Expr*)Cond); |
| 150 | SwitchStack.push_back(SS); |
| 151 | return SS; |
| 152 | } |
Chris Lattner | 46eeb22 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 153 | |
Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 154 | Action::StmtResult |
| 155 | Sema::FinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Body) { |
| 156 | Stmt *BodyStmt = (Stmt*)Body; |
| 157 | |
| 158 | SwitchStmt *SS = SwitchStack.back(); |
| 159 | assert(SS == (SwitchStmt*)Switch && "switch stack missing push/pop!"); |
| 160 | |
| 161 | SS->setBody(BodyStmt); |
| 162 | SwitchStack.pop_back(); |
| 163 | |
| 164 | Expr *condExpr = SS->getCond(); |
Chris Lattner | 46eeb22 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 165 | QualType condType = condExpr->getType(); |
| 166 | |
Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 167 | if (!condType->isIntegerType()) { // C99 6.8.4.2p1 |
| 168 | Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer, |
| 169 | condType.getAsString(), condExpr->getSourceRange()); |
| 170 | return false; |
| 171 | } |
Chris Lattner | 46eeb22 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 172 | |
Anders Carlsson | 51873c2 | 2007-07-22 07:07:56 +0000 | [diff] [blame] | 173 | DefaultStmt *CurDefaultStmt = 0; |
| 174 | |
| 175 | // FIXME: The list of cases is backwards and needs to be reversed. |
| 176 | for (SwitchCase *SC = SS->getSwitchCaseList(); SC; |
| 177 | SC = SC->getNextSwitchCase()) { |
| 178 | if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC)) { |
| 179 | if (CurDefaultStmt) { |
| 180 | Diag(DS->getDefaultLoc(), |
| 181 | diag::err_multiple_default_labels_defined); |
| 182 | Diag(CurDefaultStmt->getDefaultLoc(), |
| 183 | diag::err_first_label); |
| 184 | |
| 185 | // FIXME: Remove the default statement from the switch block |
| 186 | // so that we'll return a valid AST. |
| 187 | } else { |
| 188 | CurDefaultStmt = DS; |
| 189 | } |
| 190 | |
| 191 | // FIXME: Check that case values are unique here. |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return SS; |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | Action::StmtResult |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 199 | Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) { |
| 200 | Expr *condExpr = (Expr *)Cond; |
| 201 | assert(condExpr && "ParseWhileStmt(): missing expression"); |
| 202 | |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 203 | DefaultFunctionArrayConversion(condExpr); |
| 204 | QualType condType = condExpr->getType(); |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 205 | |
| 206 | if (!condType->isScalarType()) // C99 6.8.5p2 |
| 207 | return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar, |
| 208 | condType.getAsString(), condExpr->getSourceRange()); |
| 209 | |
| 210 | return new WhileStmt(condExpr, (Stmt*)Body); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | Action::StmtResult |
| 214 | Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body, |
| 215 | SourceLocation WhileLoc, ExprTy *Cond) { |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 216 | Expr *condExpr = (Expr *)Cond; |
| 217 | assert(condExpr && "ParseDoStmt(): missing expression"); |
| 218 | |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 219 | DefaultFunctionArrayConversion(condExpr); |
| 220 | QualType condType = condExpr->getType(); |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 221 | |
| 222 | if (!condType->isScalarType()) // C99 6.8.5p2 |
| 223 | return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar, |
| 224 | condType.getAsString(), condExpr->getSourceRange()); |
| 225 | |
| 226 | return new DoStmt((Stmt*)Body, condExpr); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | Action::StmtResult |
| 230 | Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, |
| 231 | StmtTy *First, ExprTy *Second, ExprTy *Third, |
| 232 | SourceLocation RParenLoc, StmtTy *Body) { |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 233 | if (First) { |
| 234 | // C99 6.8.5p3: FIXME. Need to hack Parser::ParseForStatement() and |
| 235 | // declaration support to create a DeclStmt node. Once this is done, |
| 236 | // we can test for DeclStmt vs. Expr (already a sub-class of Stmt). |
| 237 | } |
| 238 | if (Second) { |
| 239 | Expr *testExpr = (Expr *)Second; |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 240 | DefaultFunctionArrayConversion(testExpr); |
| 241 | QualType testType = testExpr->getType(); |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 242 | |
| 243 | if (!testType->isScalarType()) // C99 6.8.5p2 |
| 244 | return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar, |
| 245 | testType.getAsString(), testExpr->getSourceRange()); |
| 246 | } |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 247 | return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body); |
| 248 | } |
| 249 | |
| 250 | |
| 251 | Action::StmtResult |
| 252 | Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, |
| 253 | IdentifierInfo *LabelII) { |
Chris Lattner | e247306 | 2007-05-28 06:28:18 +0000 | [diff] [blame] | 254 | // Look up the record for this label identifier. |
| 255 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 256 | |
| 257 | // If we haven't seen this label yet, create a forward reference. |
| 258 | if (LabelDecl == 0) |
| 259 | LabelDecl = new LabelStmt(LabelLoc, LabelII, 0); |
| 260 | |
| 261 | return new GotoStmt(LabelDecl); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 262 | } |
Chris Lattner | 1c31050 | 2007-05-31 06:00:00 +0000 | [diff] [blame] | 263 | |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 264 | Action::StmtResult |
| 265 | Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc, |
| 266 | ExprTy *DestExp) { |
Chris Lattner | 1c31050 | 2007-05-31 06:00:00 +0000 | [diff] [blame] | 267 | // FIXME: Verify that the operand is convertible to void*. |
| 268 | |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 269 | return new IndirectGotoStmt((Expr*)DestExp); |
| 270 | } |
| 271 | |
| 272 | Action::StmtResult |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 273 | Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { |
| 274 | Scope *S = CurScope->getContinueParent(); |
| 275 | if (!S) { |
| 276 | // C99 6.8.6.2p1: A break shall appear only in or as a loop body. |
| 277 | Diag(ContinueLoc, diag::err_continue_not_in_loop); |
| 278 | return true; |
| 279 | } |
| 280 | |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 281 | return new ContinueStmt(); |
| 282 | } |
| 283 | |
| 284 | Action::StmtResult |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 285 | Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { |
| 286 | Scope *S = CurScope->getBreakParent(); |
| 287 | if (!S) { |
| 288 | // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body. |
| 289 | Diag(BreakLoc, diag::err_break_not_in_loop_or_switch); |
| 290 | return true; |
| 291 | } |
| 292 | |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 293 | return new BreakStmt(); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | Action::StmtResult |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 298 | Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) { |
| 299 | Expr *RetValExp = static_cast<Expr *>(rex); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 300 | QualType lhsType = CurFunctionDecl->getResultType(); |
| 301 | |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 302 | if (lhsType->isVoidType()) { |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 303 | if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 304 | Diag(ReturnLoc, diag::ext_return_has_expr, |
| 305 | CurFunctionDecl->getIdentifier()->getName(), |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 306 | RetValExp->getSourceRange()); |
| 307 | return new ReturnStmt(RetValExp); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 308 | } else { |
| 309 | if (!RetValExp) { |
| 310 | const char *funcName = CurFunctionDecl->getIdentifier()->getName(); |
| 311 | if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 312 | Diag(ReturnLoc, diag::ext_return_missing_expr, funcName); |
| 313 | else // C90 6.6.6.4p4 |
| 314 | Diag(ReturnLoc, diag::warn_return_missing_expr, funcName); |
| 315 | return new ReturnStmt((Expr*)0); |
| 316 | } |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 317 | } |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 318 | // we have a non-void function with an expression, continue checking |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 319 | QualType rhsType = RetValExp->getType(); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 320 | |
| 321 | if (lhsType == rhsType) // common case, fast path... |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 322 | return new ReturnStmt(RetValExp); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 323 | |
| 324 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 325 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
| 326 | // function return. |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 327 | AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType, |
| 328 | RetValExp); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 329 | bool hadError = false; |
| 330 | |
| 331 | // decode the result (notice that extensions still return a type). |
| 332 | switch (result) { |
| 333 | case Compatible: |
| 334 | break; |
| 335 | case Incompatible: |
| 336 | Diag(ReturnLoc, diag::err_typecheck_return_incompatible, |
| 337 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 338 | RetValExp->getSourceRange()); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 339 | hadError = true; |
| 340 | break; |
| 341 | case PointerFromInt: |
| 342 | // check for null pointer constant (C99 6.3.2.3p3) |
Chris Lattner | 0e9d622 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 343 | if (!RetValExp->isNullPointerConstant(Context)) { |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 344 | Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int, |
| 345 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 346 | RetValExp->getSourceRange()); |
Steve Naroff | 9992bba | 2007-05-30 16:27:15 +0000 | [diff] [blame] | 347 | } |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 348 | break; |
| 349 | case IntFromPointer: |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 350 | Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int, |
| 351 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 352 | RetValExp->getSourceRange()); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 353 | break; |
| 354 | case IncompatiblePointer: |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 355 | Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer, |
| 356 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 357 | RetValExp->getSourceRange()); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 358 | break; |
| 359 | case CompatiblePointerDiscardsQualifiers: |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 360 | Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers, |
| 361 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 362 | RetValExp->getSourceRange()); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 363 | break; |
| 364 | } |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 365 | return new ReturnStmt((Expr*)RetValExp); |
| 366 | } |
| 367 | |