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