changeset 98:5211b774b8b5

implemeted selftype expression. add CbC-exanples/selftype.c
author Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
date Tue, 24 Jan 2012 03:25:13 +0900
parents 4d6300120c29
children 98e1e78a6b5a
files CbC-examples/selftype.c gcc/c-family/c-common.c gcc/c-family/c-common.h gcc/c-parser.c gcc/cbc-tree.h
diffstat 5 files changed, 76 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CbC-examples/selftype.c	Tue Jan 24 03:25:13 2012 +0900
@@ -0,0 +1,29 @@
+
+/*
+struct node {
+  int num;
+  struct node *child;
+  //  struct node **array_node;
+};
+*/
+
+#include <stdio.h>
+
+struct node {
+  int num;
+  selftype *child;
+};
+
+int main() {
+  struct node n, nc;
+  n.num = 1;
+  nc.num = 2;
+  n.child = &nc;
+
+  printf("n.num = %d\n",n.num);
+  printf("n.child.num = %d\n",n.child->num);
+
+  return 0;
+}
+
+
--- a/gcc/c-family/c-common.c	Fri Jan 20 04:48:59 2012 +0900
+++ b/gcc/c-family/c-common.c	Tue Jan 24 03:25:13 2012 +0900
@@ -424,6 +424,7 @@
   /* CbC project */
   { "__code",         RID_CbC_CODE,   0 },
   { "__rectype",       RID_CbC_REC,    0},
+  { "selftype",       RID_CbC_SELFTYPE,    0},
 #endif
   { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
   { "__builtin_offsetof", RID_OFFSETOF, 0 },
--- a/gcc/c-family/c-common.h	Fri Jan 20 04:48:59 2012 +0900
+++ b/gcc/c-family/c-common.h	Tue Jan 24 03:25:13 2012 +0900
@@ -157,7 +157,9 @@
 
 #ifndef noCbC
   /* Continuation based C */
-  RID_CbC_CODE, RID_CbC_ENV, RID_CbC_RET,RID_CbC_REC,
+  RID_CbC_CODE, RID_CbC_ENV, RID_CbC_RET,
+  /* extention of construction : __rectype, selftype  */
+  RID_CbC_REC,RID_CbC_SELFTYPE,
 #endif
   /* Named address support, mapping the keyword to a particular named address
      number.  Named address space 0 is reserved for the generic address.  If
--- a/gcc/c-parser.c	Fri Jan 20 04:48:59 2012 +0900
+++ b/gcc/c-parser.c	Tue Jan 24 03:25:13 2012 +0900
@@ -2123,6 +2123,26 @@
 	  
 	  c_parser_consume_token (parser);
 	  break;
+	case RID_CbC_SELFTYPE:
+	  if (!typespec_ok)
+	    goto out;
+	  attrs_ok = true;
+	  seen_type = true;
+	  enum tree_code code = RECORD_TYPE;
+	  location_t loc = c_parser_peek_token (parser)->location;  
+
+	  c_parser_set_source_position_from_token (c_parser_peek_token (parser));     
+	  tree value;
+	  struct c_typespec ret;
+	  //   ret = parser_xref_tag (ident_loc, code, ident);   
+	  value = make_node (IDENTIFIER_NODE); 
+	  ret = parser_xref_tag (loc, code, value);
+	  //	  IS_SELFTYPE (ret.spec) = 1;
+	  ret.spec->base.lang_flag_3 = 1;
+	  //	  ret = lookup_tag (code, value, 0, &loc);
+	  declspecs_add_type (loc, specs, ret);
+	  c_parser_consume_token (parser);
+	  break;
 #endif
 	case RID_ENUM:
 	  if (!typespec_ok)
@@ -2459,6 +2479,25 @@
 	  /* Parse some comma-separated declarations, but not the
 	     trailing semicolon if any.  */
 	  decls = c_parser_struct_declaration (parser);
+
+#ifndef noCbC
+	  /*  If tree type of d_type is selftype, d_type modify to RECORD_TYPE: (d_type = type). */
+	  if(TREE_CODE(decls) == FIELD_DECL && TREE_CODE(type) == RECORD_TYPE) {
+	    //  tree d_type = TREE_TYPE(decls); 
+	    tree tmptype = decls; 
+	    while(TREE_TYPE (tmptype)) {
+	      //	      if(IS_SELFTYPE (TREE_TYPE (d_type))) {
+	      if(tmptype->common.type->base.lang_flag_3) {
+		TREE_TYPE (tmptype) = type;
+		break;
+	      } else if(TREE_CODE (TREE_TYPE((tmptype))) != POINTER_TYPE) {
+		break;
+	      }
+	      tmptype = TREE_TYPE(decls);
+	    }
+	  }
+#endif
+
 	  contents = chainon (decls, contents);
 	  /* If no semicolon follows, either we have a parse error or
 	     are at the end of the struct or union and should
--- a/gcc/cbc-tree.h	Fri Jan 20 04:48:59 2012 +0900
+++ b/gcc/cbc-tree.h	Tue Jan 24 03:25:13 2012 +0900
@@ -12,3 +12,7 @@
 extern tree cbc_return_f;
 extern tree cbc_env;
 extern location_t cbc_return;
+
+/* flag of selftype is lang_flag_3. */
+#define IS_SELFTYPE(NODE) TYPE_LANG_FLAG_3 (RECORD_OR_UNION_CHECK (NODE))
+