Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | 210679c | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 17 | #include "clang/Parse/DeclSpec.h" |
Daniel Dunbar | 12bc692 | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 21 | /// ActOnCXXCasts - Parse {dynamic,static,reinterpret,const}_cast's. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | Action::ExprResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 23 | Sema::ActOnCXXCasts(SourceLocation OpLoc, tok::TokenKind Kind, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | SourceLocation LAngleBracketLoc, TypeTy *Ty, |
| 25 | SourceLocation RAngleBracketLoc, |
| 26 | SourceLocation LParenLoc, ExprTy *E, |
| 27 | SourceLocation RParenLoc) { |
| 28 | CXXCastExpr::Opcode Op; |
| 29 | |
| 30 | switch (Kind) { |
| 31 | default: assert(0 && "Unknown C++ cast!"); |
| 32 | case tok::kw_const_cast: Op = CXXCastExpr::ConstCast; break; |
| 33 | case tok::kw_dynamic_cast: Op = CXXCastExpr::DynamicCast; break; |
| 34 | case tok::kw_reinterpret_cast: Op = CXXCastExpr::ReinterpretCast; break; |
| 35 | case tok::kw_static_cast: Op = CXXCastExpr::StaticCast; break; |
| 36 | } |
| 37 | |
| 38 | return new CXXCastExpr(Op, QualType::getFromOpaquePtr(Ty), (Expr*)E, OpLoc); |
| 39 | } |
| 40 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 41 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 42 | Action::ExprResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 43 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | assert((Kind != tok::kw_true || Kind != tok::kw_false) && |
| 45 | "Unknown C++ Boolean value!"); |
Steve Naroff | 210679c | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 46 | return new CXXBoolLiteralExpr(Kind == tok::kw_true, Context.BoolTy, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 48 | |
| 49 | /// ActOnCXXThrow - Parse throw expressions. |
| 50 | Action::ExprResult |
| 51 | Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprTy *E) { |
| 52 | return new CXXThrowExpr((Expr*)E, Context.VoidTy, OpLoc); |
| 53 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 54 | |
| 55 | Action::ExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { |
| 56 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 57 | /// is a non-lvalue expression whose value is the address of the object for |
| 58 | /// which the function is called. |
| 59 | |
| 60 | if (!isa<FunctionDecl>(CurContext)) { |
| 61 | Diag(ThisLoc, diag::err_invalid_this_use); |
| 62 | return ExprResult(true); |
| 63 | } |
| 64 | |
| 65 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) |
| 66 | if (MD->isInstance()) |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 67 | return new PredefinedExpr(ThisLoc, MD->getThisType(Context), |
| 68 | PredefinedExpr::CXXThis); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 69 | |
| 70 | return Diag(ThisLoc, diag::err_invalid_this_use); |
| 71 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 72 | |
| 73 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. |
| 74 | /// Can be interpreted either as function-style casting ("int(x)") |
| 75 | /// or class type construction ("ClassType(x,y,z)") |
| 76 | /// or creation of a value-initialized type ("int()"). |
| 77 | Action::ExprResult |
| 78 | Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, |
| 79 | SourceLocation LParenLoc, |
| 80 | ExprTy **ExprTys, unsigned NumExprs, |
| 81 | SourceLocation *CommaLocs, |
| 82 | SourceLocation RParenLoc) { |
| 83 | assert(TypeRep && "Missing type!"); |
| 84 | QualType Ty = QualType::getFromOpaquePtr(TypeRep); |
| 85 | Expr **Exprs = (Expr**)ExprTys; |
| 86 | SourceLocation TyBeginLoc = TypeRange.getBegin(); |
| 87 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); |
| 88 | |
| 89 | if (const RecordType *RT = Ty->getAsRecordType()) { |
| 90 | // C++ 5.2.3p1: |
| 91 | // If the simple-type-specifier specifies a class type, the class type shall |
| 92 | // be complete. |
| 93 | // |
| 94 | if (!RT->getDecl()->isDefinition()) |
| 95 | return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use, |
| 96 | Ty.getAsString(), FullRange); |
| 97 | |
| 98 | // "class constructors are not supported yet" |
| 99 | return Diag(TyBeginLoc, diag::err_unsupported_class_constructor, FullRange); |
| 100 | } |
| 101 | |
| 102 | // C++ 5.2.3p1: |
| 103 | // If the expression list is a single expression, the type conversion |
| 104 | // expression is equivalent (in definedness, and if defined in meaning) to the |
| 105 | // corresponding cast expression. |
| 106 | // |
| 107 | if (NumExprs == 1) { |
| 108 | if (CheckCastTypes(TypeRange, Ty, Exprs[0])) |
| 109 | return true; |
| 110 | return new CXXFunctionalCastExpr(Ty, TyBeginLoc, Exprs[0], RParenLoc); |
| 111 | } |
| 112 | |
| 113 | // C++ 5.2.3p1: |
| 114 | // If the expression list specifies more than a single value, the type shall |
| 115 | // be a class with a suitably declared constructor. |
| 116 | // |
| 117 | if (NumExprs > 1) |
| 118 | return Diag(CommaLocs[0], diag::err_builtin_func_cast_more_than_one_arg, |
| 119 | FullRange); |
| 120 | |
| 121 | assert(NumExprs == 0 && "Expected 0 expressions"); |
| 122 | |
| 123 | // C++ 5.2.3p2: |
| 124 | // The expression T(), where T is a simple-type-specifier for a non-array |
| 125 | // complete object type or the (possibly cv-qualified) void type, creates an |
| 126 | // rvalue of the specified type, which is value-initialized. |
| 127 | // |
| 128 | if (Ty->isArrayType()) |
| 129 | return Diag(TyBeginLoc, diag::err_value_init_for_array_type, FullRange); |
| 130 | if (Ty->isIncompleteType() && !Ty->isVoidType()) |
| 131 | return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use, |
| 132 | Ty.getAsString(), FullRange); |
| 133 | |
| 134 | return new CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc); |
| 135 | } |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 136 | |
| 137 | |
| 138 | /// ActOnCXXConditionDeclarationExpr - Parsed a condition declaration of a |
| 139 | /// C++ if/switch/while/for statement. |
| 140 | /// e.g: "if (int x = f()) {...}" |
| 141 | Action::ExprResult |
| 142 | Sema::ActOnCXXConditionDeclarationExpr(Scope *S, SourceLocation StartLoc, |
| 143 | Declarator &D, |
| 144 | SourceLocation EqualLoc, |
| 145 | ExprTy *AssignExprVal) { |
| 146 | assert(AssignExprVal && "Null assignment expression"); |
| 147 | |
| 148 | // C++ 6.4p2: |
| 149 | // The declarator shall not specify a function or an array. |
| 150 | // The type-specifier-seq shall not contain typedef and shall not declare a |
| 151 | // new class or enumeration. |
| 152 | |
| 153 | assert(D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
| 154 | "Parser allowed 'typedef' as storage class of condition decl."); |
| 155 | |
| 156 | QualType Ty = GetTypeForDeclarator(D, S); |
| 157 | |
| 158 | if (Ty->isFunctionType()) { // The declarator shall not specify a function... |
| 159 | // We exit without creating a CXXConditionDeclExpr because a FunctionDecl |
| 160 | // would be created and CXXConditionDeclExpr wants a VarDecl. |
| 161 | return Diag(StartLoc, diag::err_invalid_use_of_function_type, |
| 162 | SourceRange(StartLoc, EqualLoc)); |
| 163 | } else if (Ty->isArrayType()) { // ...or an array. |
| 164 | Diag(StartLoc, diag::err_invalid_use_of_array_type, |
| 165 | SourceRange(StartLoc, EqualLoc)); |
| 166 | } else if (const RecordType *RT = Ty->getAsRecordType()) { |
| 167 | RecordDecl *RD = RT->getDecl(); |
| 168 | // The type-specifier-seq shall not declare a new class... |
| 169 | if (RD->isDefinition() && (RD->getIdentifier() == 0 || S->isDeclScope(RD))) |
| 170 | Diag(RD->getLocation(), diag::err_type_defined_in_condition); |
| 171 | } else if (const EnumType *ET = Ty->getAsEnumType()) { |
| 172 | EnumDecl *ED = ET->getDecl(); |
| 173 | // ...or enumeration. |
| 174 | if (ED->isDefinition() && (ED->getIdentifier() == 0 || S->isDeclScope(ED))) |
| 175 | Diag(ED->getLocation(), diag::err_type_defined_in_condition); |
| 176 | } |
| 177 | |
| 178 | DeclTy *Dcl = ActOnDeclarator(S, D, 0); |
| 179 | if (!Dcl) |
| 180 | return true; |
| 181 | AddInitializerToDecl(Dcl, AssignExprVal); |
| 182 | |
| 183 | return new CXXConditionDeclExpr(StartLoc, EqualLoc, |
| 184 | cast<VarDecl>(static_cast<Decl *>(Dcl))); |
| 185 | } |
| 186 | |
| 187 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
| 188 | bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) { |
| 189 | // C++ 6.4p4: |
| 190 | // The value of a condition that is an initialized declaration in a statement |
| 191 | // other than a switch statement is the value of the declared variable |
| 192 | // implicitly converted to type bool. If that conversion is ill-formed, the |
| 193 | // program is ill-formed. |
| 194 | // The value of a condition that is an expression is the value of the |
| 195 | // expression, implicitly converted to bool. |
| 196 | // |
| 197 | QualType Ty = CondExpr->getType(); // Save the type. |
| 198 | AssignConvertType |
| 199 | ConvTy = CheckSingleAssignmentConstraints(Context.BoolTy, CondExpr); |
| 200 | if (ConvTy == Incompatible) |
| 201 | return Diag(CondExpr->getLocStart(), diag::err_typecheck_bool_condition, |
| 202 | Ty.getAsString(), CondExpr->getSourceRange()); |
| 203 | return false; |
| 204 | } |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame^] | 205 | |
| 206 | /// Helper function to determine whether this is the (deprecated) C++ |
| 207 | /// conversion from a string literal to a pointer to non-const char or |
| 208 | /// non-const wchar_t (for narrow and wide string literals, |
| 209 | /// respectively). |
| 210 | bool |
| 211 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
| 212 | // Look inside the implicit cast, if it exists. |
| 213 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
| 214 | From = Cast->getSubExpr(); |
| 215 | |
| 216 | // A string literal (2.13.4) that is not a wide string literal can |
| 217 | // be converted to an rvalue of type "pointer to char"; a wide |
| 218 | // string literal can be converted to an rvalue of type "pointer |
| 219 | // to wchar_t" (C++ 4.2p2). |
| 220 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From)) |
| 221 | if (const PointerType *ToPtrType = ToType->getAsPointerType()) |
| 222 | if (const BuiltinType *ToPointeeType |
| 223 | = ToPtrType->getPointeeType()->getAsBuiltinType()) { |
| 224 | // This conversion is considered only when there is an |
| 225 | // explicit appropriate pointer target type (C++ 4.2p2). |
| 226 | if (ToPtrType->getPointeeType().getCVRQualifiers() == 0 && |
| 227 | ((StrLit->isWide() && ToPointeeType->isWideCharType()) || |
| 228 | (!StrLit->isWide() && |
| 229 | (ToPointeeType->getKind() == BuiltinType::Char_U || |
| 230 | ToPointeeType->getKind() == BuiltinType::Char_S)))) |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | return false; |
| 235 | } |