This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[cxx-reflection] Parse decltype


This patchlet makes the parser recognize the decltype syntax.  See

   http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1478.pdf

The paper had the syntax wrong; the correct syntax is

  simple-type-specifier:
     ...
     decltype ( expression )
     decltype ( type-id )
     ...

For the moment, I don't enforce the presence of parens as described in
the paper.  No semantics.

-- Gaby

2003-06-14  Gabriel Dos Reis <gdr@integrable-solutions.net>

	* c-common.h (enum rid): Add new enumeration RID_DECLTYPE.

cp/

2003-06-15  Gabriel Dos Reis  <gdr@integrable-solutions.net>

	* cp-tree.def: Add DECLARED_TYPE tree code.
	* lex.c (reswords): Add decltype keyword.
	* error.c (dump_type): Handle DECLARED_TYPE.
	(dump_type_prefix): Likewise.
	(dump_type_suffix): Likewise.
	* cp-tree.h (build_decltype): Declare.
	* semantics.c (build_decltype): Define.
	* parser.c (cp_parser_simple_type_specifier): Parse decltype.

Index: c-common.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-common.h,v
retrieving revision 1.168.4.2
diff -p -r1.168.4.2 c-common.h
*** c-common.h	14 Jun 2003 18:53:07 -0000	1.168.4.2
--- c-common.h	15 Jun 2003 17:25:42 -0000
*************** enum rid
*** 90,95 ****
--- 90,98 ----
    RID_THROW,    RID_TRUE,     RID_TRY,
    RID_TYPENAME, RID_TYPEID,   RID_USING,
  
+   /* C++0X extension.  */
+   RID_DECLTYPE,
+ 
    /* casts */
    RID_CONSTCAST, RID_DYNCAST, RID_REINTCAST, RID_STATCAST,
  
Index: cp/cp-tree.def
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/cp-tree.def,v
retrieving revision 1.70.4.2
diff -p -r1.70.4.2 cp-tree.def
*** cp/cp-tree.def	14 Jun 2003 17:24:42 -0000	1.70.4.2
--- cp/cp-tree.def	15 Jun 2003 17:25:43 -0000
*************** DEFTREECODE (UNBOUND_CLASS_TEMPLATE, "un
*** 195,200 ****
--- 195,203 ----
     expression in question.  */
  DEFTREECODE (TYPEOF_TYPE, "typeof_type", 't', 0)
  
+ /* Same as __typeof__, but reference preserving.   */
+ DEFTREECODE (DECLARED_TYPE, "declared_type", 't', 0)
+ 
  /* A using declaration.  DECL_INITIAL contains the specified scope.  
     This is not an alias, but is later expanded into multiple aliases.  */
  DEFTREECODE (USING_DECL, "using_decl", 'd', 0)
Index: cp/cp-tree.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/cp-tree.h,v
retrieving revision 1.817.4.2
diff -p -r1.817.4.2 cp-tree.h
*** cp/cp-tree.h	14 Jun 2003 17:24:42 -0000	1.817.4.2
--- cp/cp-tree.h	15 Jun 2003 17:25:45 -0000
*************** extern void clear_out_block             
*** 4146,4151 ****
--- 4146,4152 ----
  extern tree begin_global_stmt_expr              (void);
  extern tree finish_global_stmt_expr             (tree);
  extern tree check_template_template_default_arg (tree);
+ extern tree build_decltype (tree);
  
  /* in tree.c */
  extern void lang_check_failed			(const char *, int,
Index: cp/error.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/error.c,v
retrieving revision 1.199.4.2
diff -p -r1.199.4.2 error.c
*** cp/error.c	14 Jun 2003 17:24:45 -0000	1.199.4.2
--- cp/error.c	15 Jun 2003 17:25:46 -0000
*************** dump_type (tree t, int flags)
*** 455,460 ****
--- 455,470 ----
        print_right_paren (scratch_buffer);
        break;
  
+     case DECLARED_TYPE:
+       output_add_string (scratch_buffer, "decltype");
+       print_left_paren (scratch_buffer);
+       if (TYPE_P (TYPE_FIELDS (t)))
+         dump_type (TYPE_FIELDS (t), flags);
+       else
+         dump_expr (TYPE_FIELDS (t), flags & ~TFF_EXPR_IN_PARENS);
+       print_right_paren (scratch_buffer);
+       break;
+ 
      default:
        sorry_for_unsupported_tree (t);
        /* Fall through to error.  */
*************** dump_type_prefix (tree t, int flags)
*** 664,669 ****
--- 674,680 ----
      case COMPLEX_TYPE:
      case VECTOR_TYPE:
      case TYPEOF_TYPE:
+     case DECLARED_TYPE:
        dump_type (t, flags);
        padding = before;
        break;
*************** dump_type_suffix (tree t, int flags)
*** 759,764 ****
--- 770,776 ----
      case COMPLEX_TYPE:
      case VECTOR_TYPE:
      case TYPEOF_TYPE:
+     case DECLARED_TYPE:
        break;
  
      default:
Index: cp/lex.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/lex.c,v
retrieving revision 1.300.4.2
diff -p -r1.300.4.2 lex.c
*** cp/lex.c	14 Jun 2003 17:24:46 -0000	1.300.4.2
--- cp/lex.c	15 Jun 2003 17:25:47 -0000
*************** static const struct resword reswords[] =
*** 308,313 ****
--- 308,314 ----
    { "const",		RID_CONST,	0 },
    { "const_cast",	RID_CONSTCAST,	0 },
    { "continue",		RID_CONTINUE,	0 },
+   { "decltype",         RID_DECLTYPE,   0 },
    { "default",		RID_DEFAULT,	0 },
    { "delete",		RID_DELETE,	0 },
    { "do",		RID_DO,		0 },
Index: cp/parser.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/parser.c,v
retrieving revision 1.48.4.2
diff -p -r1.48.4.2 parser.c
*** cp/parser.c	14 Jun 2003 17:24:47 -0000	1.48.4.2
--- cp/parser.c	15 Jun 2003 17:25:53 -0000
*************** cp_parser_type_specifier (cp_parser* par
*** 8553,8559 ****
  
     For the various keywords, the value returned is simply the
     TREE_IDENTIFIER representing the keyword.  For the first two
!    productions, the value returned is the indicated TYPE_DECL.  */
  
  static tree
  cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags)
--- 8553,8565 ----
  
     For the various keywords, the value returned is simply the
     TREE_IDENTIFIER representing the keyword.  For the first two
!    productions, the value returned is the indicated TYPE_DECL.
! 
!    C++0X Extension:
! 
!    simple-type-specifier:
!       decltype ( expression )
!       decltype ( type-id )  */
  
  static tree
  cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags)
*************** cp_parser_simple_type_specifier (cp_pars
*** 8582,8596 ****
        return cp_lexer_consume_token (parser->lexer)->value;
  
      case RID_TYPEOF:
        {
  	tree operand;
  
! 	/* Consume the `typeof' token.  */
  	cp_lexer_consume_token (parser->lexer);
! 	/* Parse the operand to `typeof'  */
! 	operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
  	/* If it is not already a TYPE, take its type.  */
! 	if (!TYPE_P (operand))
  	  operand = finish_typeof (operand);
  
  	return operand;
--- 8588,8609 ----
        return cp_lexer_consume_token (parser->lexer)->value;
  
      case RID_TYPEOF:
+     case RID_DECLTYPE:
        {
+         enum rid keyword = token->keyword;
  	tree operand;
  
! 	/* Consume the `typeof' or 'decltype' token.  */
  	cp_lexer_consume_token (parser->lexer);
! 	/* Parse the operand to `typeof' or 'decltype'.
!            FIXME:  For decltype, we should be more picky about
!                    presence of parens.  */
! 	operand = cp_parser_sizeof_operand (parser, keyword);
!         /* Don't be hungry in decltype evaluation.  */
!         if (keyword == RID_DECLTYPE)
!           operand = build_decltype (operand);
  	/* If it is not already a TYPE, take its type.  */
! 	else if (!TYPE_P (operand))
  	  operand = finish_typeof (operand);
  
  	return operand;
Index: cp/semantics.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/semantics.c,v
retrieving revision 1.298.4.2
diff -p -r1.298.4.2 semantics.c
*** cp/semantics.c	14 Jun 2003 17:24:49 -0000	1.298.4.2
--- cp/semantics.c	15 Jun 2003 17:25:54 -0000
*************** finish_typeof (expr)
*** 2181,2186 ****
--- 2181,2195 ----
    return type;
  }
  
+ /* Build the tree correspdonding to decltype(T).  */
+ tree
+ build_decltype(tree t)
+ {
+    tree type = make_aggr_type (TYPEOF_TYPE);
+    TYPE_FIELDS (type) = t;
+    return type;
+ }
+ 
  /* Compute the value of the `sizeof' operator.  */
  
  tree



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]