Add support for C++'s "type-specifier ( expression-list )" expression:

-The Parser calls a new "ActOnCXXTypeConstructExpr" action.
-Sema, depending on the type and expressions number:
   -If the type is a class, it will treat it as a class constructor. [TODO]
   -If there's only one expression (i.e. "int(0.5)" ), creates a new "CXXFunctionalCastExpr" Expr node
   -If there are no expressions (i.e "int()" ), creates a new "CXXZeroInitValueExpr" Expr node.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@55177 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp
index a3efab6..9085d2c 100644
--- a/lib/AST/Expr.cpp
+++ b/lib/AST/Expr.cpp
@@ -360,6 +360,7 @@
     return false;
   }
   case ExplicitCastExprClass:
+  case CXXFunctionalCastExprClass:
     // If this is a cast to void, check the operand.  Otherwise, the result of
     // the cast is unused.
     if (getType()->isVoidType())
@@ -643,7 +644,8 @@
     return true;
   }
   case ImplicitCastExprClass:
-  case ExplicitCastExprClass: {
+  case ExplicitCastExprClass:
+  case CXXFunctionalCastExprClass: {
     const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr();
     SourceLocation CastLoc = getLocStart();
     if (!SubExpr->isConstantExpr(Ctx, Loc)) {
@@ -931,7 +933,8 @@
     break;
   }
   case ImplicitCastExprClass:
-  case ExplicitCastExprClass: {
+  case ExplicitCastExprClass:
+  case CXXFunctionalCastExprClass: {
     const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr();
     SourceLocation CastLoc = getLocStart();
     
diff --git a/lib/AST/ExprCXX.cpp b/lib/AST/ExprCXX.cpp
index 6069438..cf85ae2 100644
--- a/lib/AST/ExprCXX.cpp
+++ b/lib/AST/ExprCXX.cpp
@@ -45,3 +45,11 @@
 Stmt::child_iterator CXXDefaultArgExpr::child_end() {
   return child_iterator();
 }
+
+// CXXZeroInitValueExpr
+Stmt::child_iterator CXXZeroInitValueExpr::child_begin() { 
+  return child_iterator();
+}
+Stmt::child_iterator CXXZeroInitValueExpr::child_end() {
+  return child_iterator();
+}
diff --git a/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp
index 5b4c0ec..a765060 100644
--- a/lib/AST/StmtPrinter.cpp
+++ b/lib/AST/StmtPrinter.cpp
@@ -818,6 +818,17 @@
   // Nothing to print: we picked up the default argument
 }
 
+void StmtPrinter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *Node) {
+  OS << Node->getType().getAsString();
+  OS << "(";
+  PrintExpr(Node->getSubExpr());
+  OS << ")";
+}
+
+void StmtPrinter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *Node) {
+  OS << Node->getType().getAsString() << "()";
+}
+
 // Obj-C 
 
 void StmtPrinter::VisitObjCStringLiteral(ObjCStringLiteral *Node) {
diff --git a/lib/AST/StmtSerialization.cpp b/lib/AST/StmtSerialization.cpp
index 9859846..1c61b66 100644
--- a/lib/AST/StmtSerialization.cpp
+++ b/lib/AST/StmtSerialization.cpp
@@ -197,6 +197,12 @@
       
     case CXXDefaultArgExprClass:
       return CXXDefaultArgExpr::CreateImpl(D, C);      
+
+    case CXXFunctionalCastExprClass:
+      return CXXFunctionalCastExpr::CreateImpl(D, C);
+
+    case CXXZeroInitValueExprClass:
+      return CXXZeroInitValueExpr::CreateImpl(D, C);
   }
 }
 
@@ -1101,3 +1107,33 @@
   D.ReadPtr(Param, false);
   return new CXXDefaultArgExpr(Param);
 }
+
+void CXXFunctionalCastExpr::EmitImpl(Serializer& S) const {
+  S.Emit(getType());
+  S.Emit(TyBeginLoc);
+  S.Emit(RParenLoc);
+  S.EmitOwnedPtr(getSubExpr());
+}
+
+CXXFunctionalCastExpr *
+CXXFunctionalCastExpr::CreateImpl(Deserializer& D, ASTContext& C) {
+  QualType Ty = QualType::ReadVal(D);
+  SourceLocation TyBeginLoc = SourceLocation::ReadVal(D);
+  SourceLocation RParenLoc = SourceLocation::ReadVal(D);
+  Expr* SubExpr = D.ReadOwnedPtr<Expr>(C);
+  return new CXXFunctionalCastExpr(Ty, TyBeginLoc, SubExpr, RParenLoc);
+}
+
+void CXXZeroInitValueExpr::EmitImpl(Serializer& S) const {
+  S.Emit(getType());
+  S.Emit(TyBeginLoc);
+  S.Emit(RParenLoc);
+}
+
+CXXZeroInitValueExpr *
+CXXZeroInitValueExpr::CreateImpl(Deserializer& D, ASTContext& C) {
+  QualType Ty = QualType::ReadVal(D);
+  SourceLocation TyBeginLoc = SourceLocation::ReadVal(D);
+  SourceLocation RParenLoc = SourceLocation::ReadVal(D);
+  return new CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc);
+}
diff --git a/lib/Parse/ParseExpr.cpp b/lib/Parse/ParseExpr.cpp
index 9eccd06..d4b4698 100644
--- a/lib/Parse/ParseExpr.cpp
+++ b/lib/Parse/ParseExpr.cpp
@@ -20,6 +20,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Parse/Parser.h"
+#include "clang/Parse/DeclSpec.h"
 #include "clang/Basic/Diagnostic.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/SmallString.h"
@@ -372,6 +373,8 @@
 /// [OBJC]  '@protocol' '(' identifier ')'             
 /// [OBJC]  '@encode' '(' type-name ')'                
 /// [OBJC]  objc-string-literal
+/// [C++]   simple-type-specifier '(' expression-list[opt] ')'      [C++ 5.2.3]
+/// [C++]   typename-specifier '(' expression-list[opt] ')'         [TODO]
 /// [C++]   'const_cast' '<' type-name '>' '(' expression ')'       [C++ 5.2p1]
 /// [C++]   'dynamic_cast' '<' type-name '>' '(' expression ')'     [C++ 5.2p1]
 /// [C++]   'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
@@ -446,8 +449,17 @@
   case tok::kw_false:
     return ParseCXXBoolLiteral();
 
-  case tok::identifier: {      // primary-expression: identifier
-                               // constant: enumeration-constant
+  case tok::identifier: {
+    if (getLang().CPlusPlus &&
+        Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
+      // Handle C++ function-style cast, e.g. "T(4.5)" where T is a typedef for
+      // double.
+      goto HandleType;
+    }
+    
+    // primary-expression: identifier
+    // constant: enumeration-constant
+
     // Consume the identifier so that we can see if it is followed by a '('.
     // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
     // need to know whether or not this identifier is a function designator or
@@ -545,6 +557,35 @@
     Res = ParseCXXThis();
     // This can be followed by postfix-expr pieces.
     return ParsePostfixExpressionSuffix(Res);
+
+  case tok::kw_char:
+  case tok::kw_wchar_t:
+  case tok::kw_bool:
+  case tok::kw_short:
+  case tok::kw_int:
+  case tok::kw_long:
+  case tok::kw_signed:
+  case tok::kw_unsigned:
+  case tok::kw_float:
+  case tok::kw_double:
+  case tok::kw_void:
+  case tok::kw_typeof: {
+    if (!getLang().CPlusPlus)
+      goto UnhandledToken;
+  HandleType:
+    // postfix-expression: simple-type-specifier '(' expression-list[opt] ')'
+    //
+    DeclSpec DS;
+    ParseCXXSimpleTypeSpecifier(DS);
+    if (Tok.isNot(tok::l_paren))
+      return Diag(Tok.getLocation(), diag::err_expected_lparen_after_type,
+                  DS.getSourceRange());
+
+    Res = ParseCXXTypeConstructExpression(DS);
+    // This can be followed by postfix-expr pieces.
+    return ParsePostfixExpressionSuffix(Res);
+  }
+
   case tok::at: {
     SourceLocation AtLoc = ConsumeToken();
     return ParseObjCAtExpression(AtLoc);
@@ -555,6 +596,7 @@
       return ParsePostfixExpressionSuffix(ParseObjCMessageExpression());
     // FALL THROUGH.
   default:
+  UnhandledToken:
     Diag(Tok, diag::err_expected_expression);
     return ExprResult(true);
   }
diff --git a/lib/Parse/ParseExprCXX.cpp b/lib/Parse/ParseExprCXX.cpp
index 45f4827..bd8e7d5 100644
--- a/lib/Parse/ParseExprCXX.cpp
+++ b/lib/Parse/ParseExprCXX.cpp
@@ -13,6 +13,7 @@
 
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Parse/Parser.h"
+#include "clang/Parse/DeclSpec.h"
 using namespace clang;
 
 /// ParseCXXCasts - This handles the various ways to cast expressions to another
@@ -114,3 +115,130 @@
   SourceLocation ThisLoc = ConsumeToken();
   return Actions.ActOnCXXThis(ThisLoc);
 }
+
+/// ParseCXXTypeConstructExpression - Parse construction of a specified type.
+/// Can be interpreted either as function-style casting ("int(x)")
+/// or class type construction ("ClassType(x,y,z)")
+/// or creation of a value-initialized type ("int()").
+///
+///       postfix-expression: [C++ 5.2p1]
+///         simple-type-specifier '(' expression-list[opt] ')'      [C++ 5.2.3]
+///         typename-specifier '(' expression-list[opt] ')'         [TODO]
+///
+Parser::ExprResult Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) {
+  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
+  TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
+
+  assert(Tok.is(tok::l_paren) && "Expected '('!");
+  SourceLocation LParenLoc = ConsumeParen();
+
+  ExprListTy Exprs;
+  CommaLocsTy CommaLocs;
+
+  if (Tok.isNot(tok::r_paren)) {
+    if (ParseExpressionList(Exprs, CommaLocs)) {
+      SkipUntil(tok::r_paren);
+      return ExprResult(true);
+    }
+  }
+
+  // Match the ')'.
+  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
+
+  assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&&
+         "Unexpected number of commas!");
+  return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep,
+                                           LParenLoc,
+                                           &Exprs[0], Exprs.size(),
+                                           &CommaLocs[0], RParenLoc);
+}
+
+/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
+/// This should only be called when the current token is known to be part of
+/// simple-type-specifier.
+///
+///       simple-type-specifier:
+///         '::'[opt] nested-name-specifier[opt] type-name                [TODO]
+///         '::'[opt] nested-name-specifier 'template' simple-template-id [TODO]
+///         char
+///         wchar_t
+///         bool
+///         short
+///         int
+///         long
+///         signed
+///         unsigned
+///         float
+///         double
+///         void
+/// [GNU]   typeof-specifier
+/// [C++0x] auto               [TODO]
+///
+///       type-name:
+///         class-name
+///         enum-name
+///         typedef-name
+///
+void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) {
+  DS.SetRangeStart(Tok.getLocation());
+  const char *PrevSpec;
+  SourceLocation Loc = Tok.getLocation();
+  
+  switch (Tok.getKind()) {
+  default: 
+    assert(0 && "Not a simple-type-specifier token!");
+    abort();
+      
+  // type-name
+  case tok::identifier: {
+    TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
+    assert(TypeRep && "Identifier wasn't a type-name!");
+    DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec, TypeRep);
+    break;
+  }
+    
+  // builtin types
+  case tok::kw_short:
+    DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
+    break;
+  case tok::kw_long:
+    DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
+    break;
+  case tok::kw_signed:
+    DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
+    break;
+  case tok::kw_unsigned:
+    DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
+    break;
+  case tok::kw_void:
+    DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
+    break;
+  case tok::kw_char:
+    DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
+    break;
+  case tok::kw_int:
+    DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
+    break;
+  case tok::kw_float:
+    DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
+    break;
+  case tok::kw_double:
+    DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
+    break;
+  case tok::kw_wchar_t:
+    DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
+    break;
+  case tok::kw_bool:
+    DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
+    break;
+  
+  // GNU typeof support.
+  case tok::kw_typeof:
+    ParseTypeofSpecifier(DS);
+    DS.Finish(Diags, PP.getSourceManager(), getLang());
+    return;
+  }
+  DS.SetRangeEnd(Tok.getLocation());
+  ConsumeToken();
+  DS.Finish(Diags, PP.getSourceManager(), getLang());
+}
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h
index 4bb5482..321471d 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -559,6 +559,18 @@
   virtual ExprResult ActOnCXXThrow(SourceLocation OpLoc,
                                    ExprTy *expr);
 
+  /// ActOnCXXTypeConstructExpr - Parse construction of a specified type.
+  /// Can be interpreted either as function-style casting ("int(x)")
+  /// or class type construction ("ClassType(x,y,z)")
+  /// or creation of a value-initialized type ("int()").
+  virtual ExprResult ActOnCXXTypeConstructExpr(SourceRange TypeRange,
+                                               TypeTy *TypeRep,
+                                               SourceLocation LParenLoc,
+                                               ExprTy **Exprs,
+                                               unsigned NumExprs,
+                                               SourceLocation *CommaLocs,
+                                               SourceLocation RParenLoc);
+
   // ParseObjCStringLiteral - Parse Objective-C string literals.
   virtual ExprResult ParseObjCStringLiteral(SourceLocation *AtLocs, 
                                             ExprTy **Strings,
diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp
index 86e3345..1c723e6 100644
--- a/lib/Sema/SemaExprCXX.cpp
+++ b/lib/Sema/SemaExprCXX.cpp
@@ -68,3 +68,67 @@
 
   return Diag(ThisLoc, diag::err_invalid_this_use);
 }
+
+/// ActOnCXXTypeConstructExpr - Parse construction of a specified type.
+/// Can be interpreted either as function-style casting ("int(x)")
+/// or class type construction ("ClassType(x,y,z)")
+/// or creation of a value-initialized type ("int()").
+Action::ExprResult
+Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep,
+                                SourceLocation LParenLoc,
+                                ExprTy **ExprTys, unsigned NumExprs,
+                                SourceLocation *CommaLocs,
+                                SourceLocation RParenLoc) {
+  assert(TypeRep && "Missing type!");
+  QualType Ty = QualType::getFromOpaquePtr(TypeRep);
+  Expr **Exprs = (Expr**)ExprTys;
+  SourceLocation TyBeginLoc = TypeRange.getBegin();
+  SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc);
+
+  if (const RecordType *RT = Ty->getAsRecordType()) {
+    // C++ 5.2.3p1:
+    // If the simple-type-specifier specifies a class type, the class type shall
+    // be complete.
+    //
+    if (!RT->getDecl()->isDefinition())
+      return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use,
+                  Ty.getAsString(), FullRange);
+
+    // "class constructors are not supported yet"
+    return Diag(TyBeginLoc, diag::err_unsupported_class_constructor, FullRange);
+  }
+
+  // C++ 5.2.3p1:
+  // If the expression list is a single expression, the type conversion
+  // expression is equivalent (in definedness, and if defined in meaning) to the
+  // corresponding cast expression.
+  //
+  if (NumExprs == 1) {
+    if (CheckCastTypes(TypeRange, Ty, Exprs[0]))
+      return true;
+    return new CXXFunctionalCastExpr(Ty, TyBeginLoc, Exprs[0], RParenLoc);
+  }
+
+  // C++ 5.2.3p1:
+  // If the expression list specifies more than a single value, the type shall
+  // be a class with a suitably declared constructor.
+  //
+  if (NumExprs > 1)
+    return Diag(CommaLocs[0], diag::err_builtin_func_cast_more_than_one_arg,
+                FullRange);
+
+  assert(NumExprs == 0 && "Expected 0 expressions");
+
+  // C++ 5.2.3p2:
+  // The expression T(), where T is a simple-type-specifier for a non-array
+  // complete object type or the (possibly cv-qualified) void type, creates an
+  // rvalue of the specified type, which is value-initialized.
+  //
+  if (Ty->isArrayType())
+    return Diag(TyBeginLoc, diag::err_value_init_for_array_type, FullRange);
+  if (Ty->isIncompleteType() && !Ty->isVoidType())
+    return Diag(TyBeginLoc, diag::err_invalid_incomplete_type_use,
+                Ty.getAsString(), FullRange);
+
+  return new CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc);
+}