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 clang; |
| 22 | |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 23 | Sema::StmtResult Sema::ParseExprStmt(ExprTy *expr) { |
| 24 | Expr *E = static_cast<Expr*>(expr); |
| 25 | |
| 26 | // Exprs are statements, so there is no need to do a conversion here. However, |
| 27 | // diagnose some potentially bad code. |
| 28 | if (!E->hasLocalSideEffect()) |
Chris Lattner | 9b3b9a1 | 2007-06-27 06:08:24 +0000 | [diff] [blame] | 29 | Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange()); |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 30 | |
| 31 | return E; |
| 32 | } |
| 33 | |
| 34 | |
Chris Lattner | 0f203a7 | 2007-05-28 01:45:28 +0000 | [diff] [blame] | 35 | Sema::StmtResult Sema::ParseNullStmt(SourceLocation SemiLoc) { |
| 36 | return new NullStmt(SemiLoc); |
| 37 | } |
| 38 | |
Steve Naroff | 2a8ad18 | 2007-05-29 22:59:26 +0000 | [diff] [blame] | 39 | Sema::StmtResult Sema::ParseDeclStmt(DeclTy *decl) { |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 40 | if (decl) |
| 41 | return new DeclStmt(static_cast<Decl *>(decl)); |
| 42 | else |
| 43 | return true; // error |
Steve Naroff | 2a8ad18 | 2007-05-29 22:59:26 +0000 | [diff] [blame] | 44 | } |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 45 | |
| 46 | Action::StmtResult |
| 47 | Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R, |
| 48 | StmtTy **Elts, unsigned NumElts) { |
Chris Lattner | 3bbe7be | 2007-05-20 22:47:04 +0000 | [diff] [blame] | 49 | return new CompoundStmt((Stmt**)Elts, NumElts); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | Action::StmtResult |
Chris Lattner | 0c46c5d | 2007-06-02 23:53:17 +0000 | [diff] [blame] | 53 | Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval, |
Chris Lattner | 46eeb22 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 54 | SourceLocation DotDotDotLoc, ExprTy *rhsval, |
Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame^] | 55 | SourceLocation ColonLoc, StmtTy *subStmt) { |
| 56 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
Chris Lattner | 0c46c5d | 2007-06-02 23:53:17 +0000 | [diff] [blame] | 57 | Expr *LHSVal = ((Expr *)lhsval); |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 58 | assert((LHSVal != 0) && "missing expression in case statement"); |
| 59 | |
Chris Lattner | 0c46c5d | 2007-06-02 23:53:17 +0000 | [diff] [blame] | 60 | SourceLocation ExpLoc; |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 61 | // C99 6.8.4.2p3: The expression shall be an integer constant. |
Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame^] | 62 | if (!LHSVal->isIntegerConstantExpr(Context, &ExpLoc)) { |
| 63 | Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr, |
| 64 | LHSVal->getSourceRange()); |
| 65 | return SubStmt; |
| 66 | } |
Steve Naroff | 8eeeb13 | 2007-05-08 21:09:37 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 46eeb22 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 68 | // GCC extension: The expression shall be an integer constant. |
| 69 | Expr *RHSVal = ((Expr *)rhsval); |
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 | 35e287b | 2007-06-03 01:44:43 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame^] | 76 | return new CaseStmt(LHSVal, (Expr*)RHSVal, SubStmt); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | Action::StmtResult |
Chris Lattner | 46eeb22 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 80 | Sema::ParseDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, |
Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame^] | 81 | StmtTy *subStmt, Scope *CurScope) { |
| 82 | Stmt *SubStmt = static_cast<Stmt*>(subStmt); |
Chris Lattner | 46eeb22 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 83 | Scope *S = CurScope->getBreakParent(); |
| 84 | |
Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame^] | 85 | if (!S) { |
| 86 | Diag(DefaultLoc, diag::err_default_not_in_switch); |
| 87 | return SubStmt; |
| 88 | } |
| 89 | |
Chris Lattner | 46eeb22 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 90 | if (S->getDefaultStmt()) { |
| 91 | Diag(DefaultLoc, diag::err_multiple_default_labels_defined); |
| 92 | Diag(((DefaultStmt *)S->getDefaultStmt())->getDefaultLoc(), |
| 93 | diag::err_first_label); |
Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame^] | 94 | return SubStmt; |
Chris Lattner | 46eeb22 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Chris Lattner | 3940737 | 2007-07-21 03:00:26 +0000 | [diff] [blame^] | 97 | DefaultStmt *DS = new DefaultStmt(DefaultLoc, SubStmt); |
Chris Lattner | 46eeb22 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 98 | S->setDefaultStmt(DS); |
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 |
| 148 | Sema::ParseSwitchStmt(SourceLocation SwitchLoc, ExprTy *Cond, StmtTy *Body) { |
Chris Lattner | 46eeb22 | 2007-07-18 02:28:47 +0000 | [diff] [blame] | 149 | Expr *condExpr = (Expr *)Cond; |
| 150 | |
| 151 | QualType condType = condExpr->getType(); |
| 152 | |
| 153 | if (!condType->isIntegerType()) // C99 6.8.4.2p1 |
| 154 | return Diag(SwitchLoc, diag::err_typecheck_statement_requires_integer, |
| 155 | condType.getAsString(), condExpr->getSourceRange()); |
| 156 | |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 157 | return new SwitchStmt((Expr*)Cond, (Stmt*)Body); |
| 158 | } |
| 159 | |
| 160 | Action::StmtResult |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 161 | Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) { |
| 162 | Expr *condExpr = (Expr *)Cond; |
| 163 | assert(condExpr && "ParseWhileStmt(): missing expression"); |
| 164 | |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 165 | DefaultFunctionArrayConversion(condExpr); |
| 166 | QualType condType = condExpr->getType(); |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 167 | |
| 168 | if (!condType->isScalarType()) // C99 6.8.5p2 |
| 169 | return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar, |
| 170 | condType.getAsString(), condExpr->getSourceRange()); |
| 171 | |
| 172 | return new WhileStmt(condExpr, (Stmt*)Body); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | Action::StmtResult |
| 176 | Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body, |
| 177 | SourceLocation WhileLoc, ExprTy *Cond) { |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 178 | Expr *condExpr = (Expr *)Cond; |
| 179 | assert(condExpr && "ParseDoStmt(): missing expression"); |
| 180 | |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 181 | DefaultFunctionArrayConversion(condExpr); |
| 182 | QualType condType = condExpr->getType(); |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 183 | |
| 184 | if (!condType->isScalarType()) // C99 6.8.5p2 |
| 185 | return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar, |
| 186 | condType.getAsString(), condExpr->getSourceRange()); |
| 187 | |
| 188 | return new DoStmt((Stmt*)Body, condExpr); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | Action::StmtResult |
| 192 | Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, |
| 193 | StmtTy *First, ExprTy *Second, ExprTy *Third, |
| 194 | SourceLocation RParenLoc, StmtTy *Body) { |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 195 | if (First) { |
| 196 | // C99 6.8.5p3: FIXME. Need to hack Parser::ParseForStatement() and |
| 197 | // declaration support to create a DeclStmt node. Once this is done, |
| 198 | // we can test for DeclStmt vs. Expr (already a sub-class of Stmt). |
| 199 | } |
| 200 | if (Second) { |
| 201 | Expr *testExpr = (Expr *)Second; |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 202 | DefaultFunctionArrayConversion(testExpr); |
| 203 | QualType testType = testExpr->getType(); |
Steve Naroff | 86272ea | 2007-05-29 02:14:17 +0000 | [diff] [blame] | 204 | |
| 205 | if (!testType->isScalarType()) // C99 6.8.5p2 |
| 206 | return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar, |
| 207 | testType.getAsString(), testExpr->getSourceRange()); |
| 208 | } |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 209 | return new ForStmt((Stmt*)First, (Expr*)Second, (Expr*)Third, (Stmt*)Body); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | Action::StmtResult |
| 214 | Sema::ParseGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, |
| 215 | IdentifierInfo *LabelII) { |
Chris Lattner | e247306 | 2007-05-28 06:28:18 +0000 | [diff] [blame] | 216 | // Look up the record for this label identifier. |
| 217 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 218 | |
| 219 | // If we haven't seen this label yet, create a forward reference. |
| 220 | if (LabelDecl == 0) |
| 221 | LabelDecl = new LabelStmt(LabelLoc, LabelII, 0); |
| 222 | |
| 223 | return new GotoStmt(LabelDecl); |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 224 | } |
Chris Lattner | 1c31050 | 2007-05-31 06:00:00 +0000 | [diff] [blame] | 225 | |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 226 | Action::StmtResult |
| 227 | Sema::ParseIndirectGotoStmt(SourceLocation GotoLoc,SourceLocation StarLoc, |
| 228 | ExprTy *DestExp) { |
Chris Lattner | 1c31050 | 2007-05-31 06:00:00 +0000 | [diff] [blame] | 229 | // FIXME: Verify that the operand is convertible to void*. |
| 230 | |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 231 | return new IndirectGotoStmt((Expr*)DestExp); |
| 232 | } |
| 233 | |
| 234 | Action::StmtResult |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 235 | Sema::ParseContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) { |
| 236 | Scope *S = CurScope->getContinueParent(); |
| 237 | if (!S) { |
| 238 | // C99 6.8.6.2p1: A break shall appear only in or as a loop body. |
| 239 | Diag(ContinueLoc, diag::err_continue_not_in_loop); |
| 240 | return true; |
| 241 | } |
| 242 | |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 243 | return new ContinueStmt(); |
| 244 | } |
| 245 | |
| 246 | Action::StmtResult |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 247 | Sema::ParseBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { |
| 248 | Scope *S = CurScope->getBreakParent(); |
| 249 | if (!S) { |
| 250 | // C99 6.8.6.3p1: A break shall appear only in or as a switch/loop body. |
| 251 | Diag(BreakLoc, diag::err_break_not_in_loop_or_switch); |
| 252 | return true; |
| 253 | } |
| 254 | |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 255 | return new BreakStmt(); |
| 256 | } |
| 257 | |
| 258 | |
| 259 | Action::StmtResult |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 260 | Sema::ParseReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) { |
| 261 | Expr *RetValExp = static_cast<Expr *>(rex); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 262 | QualType lhsType = CurFunctionDecl->getResultType(); |
| 263 | |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 264 | if (lhsType->isVoidType()) { |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 265 | if (RetValExp) // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 266 | Diag(ReturnLoc, diag::ext_return_has_expr, |
| 267 | CurFunctionDecl->getIdentifier()->getName(), |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 268 | RetValExp->getSourceRange()); |
| 269 | return new ReturnStmt(RetValExp); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 270 | } else { |
| 271 | if (!RetValExp) { |
| 272 | const char *funcName = CurFunctionDecl->getIdentifier()->getName(); |
| 273 | if (getLangOptions().C99) // C99 6.8.6.4p1 (ext_ since GCC warns) |
| 274 | Diag(ReturnLoc, diag::ext_return_missing_expr, funcName); |
| 275 | else // C90 6.6.6.4p4 |
| 276 | Diag(ReturnLoc, diag::warn_return_missing_expr, funcName); |
| 277 | return new ReturnStmt((Expr*)0); |
| 278 | } |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 279 | } |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 280 | // we have a non-void function with an expression, continue checking |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 281 | QualType rhsType = RetValExp->getType(); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 282 | |
| 283 | if (lhsType == rhsType) // common case, fast path... |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 284 | return new ReturnStmt(RetValExp); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 285 | |
| 286 | // C99 6.8.6.4p3(136): The return statement is not an assignment. The |
| 287 | // overlap restriction of subclause 6.5.16.1 does not apply to the case of |
| 288 | // function return. |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 289 | AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType, |
| 290 | RetValExp); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 291 | bool hadError = false; |
| 292 | |
| 293 | // decode the result (notice that extensions still return a type). |
| 294 | switch (result) { |
| 295 | case Compatible: |
| 296 | break; |
| 297 | case Incompatible: |
| 298 | Diag(ReturnLoc, diag::err_typecheck_return_incompatible, |
| 299 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 300 | RetValExp->getSourceRange()); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 301 | hadError = true; |
| 302 | break; |
| 303 | case PointerFromInt: |
| 304 | // check for null pointer constant (C99 6.3.2.3p3) |
Chris Lattner | 0e9d622 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 305 | if (!RetValExp->isNullPointerConstant(Context)) { |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 306 | Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int, |
| 307 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 308 | RetValExp->getSourceRange()); |
Steve Naroff | 9992bba | 2007-05-30 16:27:15 +0000 | [diff] [blame] | 309 | } |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 310 | break; |
| 311 | case IntFromPointer: |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 312 | Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int, |
| 313 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 314 | RetValExp->getSourceRange()); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 315 | break; |
| 316 | case IncompatiblePointer: |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 317 | Diag(ReturnLoc, diag::ext_typecheck_return_incompatible_pointer, |
| 318 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 319 | RetValExp->getSourceRange()); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 320 | break; |
| 321 | case CompatiblePointerDiscardsQualifiers: |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 322 | Diag(ReturnLoc, diag::ext_typecheck_return_discards_qualifiers, |
| 323 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 324 | RetValExp->getSourceRange()); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 325 | break; |
| 326 | } |
Chris Lattner | af8d581 | 2006-11-10 05:07:45 +0000 | [diff] [blame] | 327 | return new ReturnStmt((Expr*)RetValExp); |
| 328 | } |
| 329 | |