cb22561995-10-11Fredrik Hübinette (Hubbe) /*\
06983f1996-09-22Fredrik Hübinette (Hubbe) ||| This file a part of Pike, and is copyright by Fredrik Hubinette ||| Pike is distributed as GPL (General Public License)
cb22561995-10-11Fredrik Hübinette (Hubbe) ||| See the files COPYING and DISCLAIMER for more information. \*/
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "global.h"
c439fc1996-11-13Fredrik Hübinette (Hubbe) RCSID("$Id: pike_types.c,v 1.8 1996/11/14 01:36:30 hubbe Exp $");
5267b71995-08-09Fredrik Hübinette (Hubbe) #include <ctype.h> #include "svalue.h"
06983f1996-09-22Fredrik Hübinette (Hubbe) #include "pike_types.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "stralloc.h" #include "stuff.h" #include "array.h" #include "program.h"
06983f1996-09-22Fredrik Hübinette (Hubbe) #include "constants.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "object.h"
06983f1996-09-22Fredrik Hübinette (Hubbe) #include "multiset.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "mapping.h" #include "macros.h" #include "error.h"
bce86c1996-02-25Fredrik Hübinette (Hubbe) static void internal_parse_type(char **s);
5267b71995-08-09Fredrik Hübinette (Hubbe) /* * basic types are represented by just their value in a string * basic type are string, int, float, object and program * arrays are coded like by the value T_ARRAY followed by the * data type, if the type is not known it is T_MIXED, ie: * T_ARRAY <data type> * mappings are followed by two arguments, the first is the type * for the indices, and the second is the type of the data, ie: * T_MAPPING <indice type> <data type>
06983f1996-09-22Fredrik Hübinette (Hubbe)  * multiset works similarly to arrays.
5267b71995-08-09Fredrik Hübinette (Hubbe)  * functions are _very_ special: * they are coded like this: * T_FUNCTION <arg type> <arg type> ... <arg type> T_MANY <arg type> <return type> * note that the type after T_MANY can be T_VOID * T_MIXED matches anything except T_VOID * T_UNKNOWN only matches T_MIXED and T_UNKNOWN */
06983f1996-09-22Fredrik Hübinette (Hubbe) struct pike_string *string_type_string; struct pike_string *int_type_string; struct pike_string *float_type_string; struct pike_string *function_type_string; struct pike_string *object_type_string; struct pike_string *program_type_string; struct pike_string *array_type_string; struct pike_string *multiset_type_string; struct pike_string *mapping_type_string; struct pike_string *mixed_type_string; struct pike_string *void_type_string; struct pike_string *any_type_string;
5267b71995-08-09Fredrik Hübinette (Hubbe)  void init_types() { string_type_string=parse_type("string"); int_type_string=parse_type("int"); object_type_string=parse_type("object"); program_type_string=parse_type("program"); float_type_string=parse_type("float"); mixed_type_string=parse_type("mixed"); array_type_string=parse_type("array");
06983f1996-09-22Fredrik Hübinette (Hubbe)  multiset_type_string=parse_type("multiset");
5267b71995-08-09Fredrik Hübinette (Hubbe)  mapping_type_string=parse_type("mapping"); function_type_string=parse_type("function"); void_type_string=parse_type("void");
f6f02d1995-10-16Fredrik Hübinette (Hubbe)  any_type_string=parse_type("void|mixed");
5267b71995-08-09Fredrik Hübinette (Hubbe) } static int type_length(char *t) { char *q=t;
041a731996-11-12Mirar (Pontus Hagland) 
5267b71995-08-09Fredrik Hübinette (Hubbe)  switch(EXTRACT_UCHAR(t++)) { default: fatal("error in type string.\n"); /*NOTREACHED*/ case T_FUNCTION: while(EXTRACT_UCHAR(t)!=T_MANY) t+=type_length(t); /* skip arguments */ t++; case T_MAPPING: case T_OR:
bce86c1996-02-25Fredrik Hübinette (Hubbe)  case T_AND:
5267b71995-08-09Fredrik Hübinette (Hubbe)  t+=type_length(t); case T_ARRAY:
06983f1996-09-22Fredrik Hübinette (Hubbe)  case T_MULTISET:
bce86c1996-02-25Fredrik Hübinette (Hubbe)  case T_NOT:
5267b71995-08-09Fredrik Hübinette (Hubbe)  t+=type_length(t); case T_INT: case T_FLOAT: case T_STRING: case T_PROGRAM: case T_MIXED: case T_VOID: case T_UNKNOWN: break;
d2c6081996-11-07Fredrik Hübinette (Hubbe)  case T_OBJECT: t+=sizeof(INT32); break;
5267b71995-08-09Fredrik Hübinette (Hubbe)  } return t-q; }
7260711996-06-21Fredrik Hübinette (Hubbe)  #define STACK_SIZE 100000 static unsigned char type_stack[STACK_SIZE]; static unsigned char *type_stackp=type_stack; static unsigned char *mark_stack[STACK_SIZE/4]; static unsigned char **mark_stackp=mark_stack; void reset_type_stack() { type_stackp=type_stack; mark_stackp=mark_stack; } void type_stack_mark() { *mark_stackp=type_stackp; mark_stackp++; if(mark_stackp > mark_stack + NELEM(mark_stack)) yyerror("Type mark stack overflow."); } unsigned char *pop_stack_mark() { mark_stackp--; if(mark_stackp<mark_stack) fatal("Type mark stack underflow\n"); return *mark_stackp; } void pop_type_stack() { type_stackp--; if(type_stackp<type_stack) fatal("Type stack underflow\n"); } void type_stack_pop_to_mark() { type_stackp=pop_stack_mark(); } void type_stack_reverse() { unsigned char *a,*b,tmp; a=pop_stack_mark(); b=type_stackp-1; while(b>a) { tmp=*a; *a=*b; *b=tmp; b--; a++; } } void push_type(unsigned char tmp) { *type_stackp=tmp; type_stackp++; if(type_stackp > type_stack + sizeof(type_stack)) yyerror("Type stack overflow."); }
d2c6081996-11-07Fredrik Hübinette (Hubbe) void push_type_int(unsigned INT32 i) { if(type_stackp + sizeof(i)> type_stack + sizeof(type_stack)) yyerror("Type stack overflow."); type_stack_mark(); MEMCPY(type_stackp, &i, sizeof(i)); type_stackp+=sizeof(i); type_stack_reverse(); }
7260711996-06-21Fredrik Hübinette (Hubbe) void push_unfinished_type(char *s) { int e; e=type_length(s); for(e--;e>=0;e--) push_type(s[e]); }
06983f1996-09-22Fredrik Hübinette (Hubbe) void push_finished_type(struct pike_string *type)
7260711996-06-21Fredrik Hübinette (Hubbe) { int e; for(e=type->len-1;e>=0;e--) push_type(type->str[e]); }
06983f1996-09-22Fredrik Hübinette (Hubbe) struct pike_string *pop_unfinished_type()
7260711996-06-21Fredrik Hübinette (Hubbe) { int len,e;
06983f1996-09-22Fredrik Hübinette (Hubbe)  struct pike_string *s;
7260711996-06-21Fredrik Hübinette (Hubbe)  len=type_stackp - pop_stack_mark(); s=begin_shared_string(len); for(e=0;e<len;e++) s->str[e] = *--type_stackp; return end_shared_string(s); }
06983f1996-09-22Fredrik Hübinette (Hubbe) struct pike_string *pop_type()
7260711996-06-21Fredrik Hübinette (Hubbe) { int len,e;
06983f1996-09-22Fredrik Hübinette (Hubbe)  struct pike_string *s;
7260711996-06-21Fredrik Hübinette (Hubbe)  len=type_stackp - type_stack; s=begin_shared_string(len); for(e=0;e<len;e++) s->str[e] = *--type_stackp; s=end_shared_string(s); reset_type_stack(); return s; }
b432bb1996-10-29Per Hedbor static void internal_parse_typeA(char **_s)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
b432bb1996-10-29Per Hedbor  unsigned char buf[80];
5267b71995-08-09Fredrik Hübinette (Hubbe)  unsigned int len;
b432bb1996-10-29Per Hedbor  unsigned char **s = (unsigned char **)_s;
bdb5091996-09-25Fredrik Hübinette (Hubbe)  while(ISSPACE(**s)) ++*s;
5267b71995-08-09Fredrik Hübinette (Hubbe)  len=0; for(len=0;isidchar(s[0][len]);len++) { if(len>=sizeof(buf)) error("Buffer overflow in parse_type\n"); buf[len] = s[0][len]; } buf[len]=0; *s += len; if(!strcmp(buf,"int")) push_type(T_INT); else if(!strcmp(buf,"float")) push_type(T_FLOAT);
d2c6081996-11-07Fredrik Hübinette (Hubbe)  else if(!strcmp(buf,"object")) { push_type_int(0); push_type(T_OBJECT); }
5267b71995-08-09Fredrik Hübinette (Hubbe)  else if(!strcmp(buf,"program")) push_type(T_PROGRAM); else if(!strcmp(buf,"string")) push_type(T_STRING); else if(!strcmp(buf,"void")) push_type(T_VOID); else if(!strcmp(buf,"mixed")) push_type(T_MIXED); else if(!strcmp(buf,"unknown")) push_type(T_UNKNOWN); else if(!strcmp(buf,"function")) {
bdb5091996-09-25Fredrik Hübinette (Hubbe)  while(ISSPACE(**s)) ++*s;
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(**s == '(') { ++*s;
bdb5091996-09-25Fredrik Hübinette (Hubbe)  while(ISSPACE(**s)) ++*s;
5267b71995-08-09Fredrik Hübinette (Hubbe)  type_stack_mark(); while(1) { if(**s == ':') { push_type(T_MANY); push_type(T_VOID); break; } type_stack_mark(); type_stack_mark(); type_stack_mark();
ec8c9e1996-11-01Fredrik Hübinette (Hubbe)  internal_parse_type(_s);
5267b71995-08-09Fredrik Hübinette (Hubbe)  type_stack_reverse(); if(**s==',') { ++*s;
bdb5091996-09-25Fredrik Hübinette (Hubbe)  while(ISSPACE(**s)) ++*s;
5267b71995-08-09Fredrik Hübinette (Hubbe)  } else if(s[0][0]=='.' && s[0][1]=='.' && s[0][2]=='.') { type_stack_reverse(); push_type(T_MANY); type_stack_reverse(); *s+=3;
bdb5091996-09-25Fredrik Hübinette (Hubbe)  while(ISSPACE(**s)) ++*s;
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(**s != ':') error("Missing ':' after ... in function type.\n"); break; } pop_stack_mark(); pop_stack_mark(); } ++*s; type_stack_mark();
ec8c9e1996-11-01Fredrik Hübinette (Hubbe)  internal_parse_type(_s); /* return type */
5267b71995-08-09Fredrik Hübinette (Hubbe)  type_stack_reverse(); if(**s != ')') error("Missing ')' in function type.\n"); ++*s;
bce86c1996-02-25Fredrik Hübinette (Hubbe)  type_stack_reverse(); }else{
0d202a1995-10-20Fredrik Hübinette (Hubbe)  push_type(T_MIXED);
5267b71995-08-09Fredrik Hübinette (Hubbe)  push_type(T_MIXED); push_type(T_MANY); } push_type(T_FUNCTION); } else if(!strcmp(buf,"mapping")) {
bdb5091996-09-25Fredrik Hübinette (Hubbe)  while(ISSPACE(**s)) ++*s;
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(**s == '(') {
4d06531996-05-04Fredrik Hübinette (Hubbe)  type_stack_mark();
5267b71995-08-09Fredrik Hübinette (Hubbe)  ++*s;
4d06531996-05-04Fredrik Hübinette (Hubbe)  type_stack_mark();
ec8c9e1996-11-01Fredrik Hübinette (Hubbe)  internal_parse_type(_s);
4d06531996-05-04Fredrik Hübinette (Hubbe)  type_stack_reverse();
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(**s != ':') error("Expecting ':'.\n"); ++*s;
4d06531996-05-04Fredrik Hübinette (Hubbe)  type_stack_mark();
ec8c9e1996-11-01Fredrik Hübinette (Hubbe)  internal_parse_type(_s);
4d06531996-05-04Fredrik Hübinette (Hubbe)  type_stack_reverse();
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(**s != ')') error("Expecting ')'.\n"); ++*s;
4d06531996-05-04Fredrik Hübinette (Hubbe)  type_stack_reverse();
5267b71995-08-09Fredrik Hübinette (Hubbe)  }else{ push_type(T_MIXED); push_type(T_MIXED); } push_type(T_MAPPING); } else if(!strcmp(buf,"array")) {
bdb5091996-09-25Fredrik Hübinette (Hubbe)  while(ISSPACE(**s)) ++*s;
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(**s == '(') { ++*s;
ec8c9e1996-11-01Fredrik Hübinette (Hubbe)  internal_parse_type(_s);
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(**s != ')') error("Expecting ')'.\n"); ++*s; }else{ push_type(T_MIXED); } push_type(T_ARRAY); }
06983f1996-09-22Fredrik Hübinette (Hubbe)  else if(!strcmp(buf,"multiset"))
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
bdb5091996-09-25Fredrik Hübinette (Hubbe)  while(ISSPACE(**s)) ++*s;
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(**s == '(') { ++*s;
ec8c9e1996-11-01Fredrik Hübinette (Hubbe)  internal_parse_type(_s);
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(**s != ')') error("Expecting ')'.\n"); ++*s; }else{ push_type(T_MIXED); }
06983f1996-09-22Fredrik Hübinette (Hubbe)  push_type(T_MULTISET);
5267b71995-08-09Fredrik Hübinette (Hubbe)  } else error("Couldn't parse type. (%s)\n",buf);
bdb5091996-09-25Fredrik Hübinette (Hubbe)  while(ISSPACE(**s)) ++*s;
bce86c1996-02-25Fredrik Hübinette (Hubbe) } static void internal_parse_typeB(char **s) {
bdb5091996-09-25Fredrik Hübinette (Hubbe)  while(ISSPACE(**s)) ++*s;
bce86c1996-02-25Fredrik Hübinette (Hubbe)  switch(**s) { case '!': ++*s; internal_parse_typeB(s); push_type(T_NOT); break; case '(': ++*s; internal_parse_typeB(s);
bdb5091996-09-25Fredrik Hübinette (Hubbe)  while(ISSPACE(**s)) ++*s;
bce86c1996-02-25Fredrik Hübinette (Hubbe)  if(**s != ')') error("Expecting ')'.\n"); break; default: internal_parse_typeA(s); } } static void internal_parse_typeCC(char **s) { internal_parse_typeB(s);
bdb5091996-09-25Fredrik Hübinette (Hubbe)  while(ISSPACE(**s)) ++*s;
bce86c1996-02-25Fredrik Hübinette (Hubbe) 
5267b71995-08-09Fredrik Hübinette (Hubbe)  while(**s == '*') { ++*s;
bdb5091996-09-25Fredrik Hübinette (Hubbe)  while(ISSPACE(**s)) ++*s;
bce86c1996-02-25Fredrik Hübinette (Hubbe)  push_type(T_ARRAY);
5267b71995-08-09Fredrik Hübinette (Hubbe)  }
bce86c1996-02-25Fredrik Hübinette (Hubbe) } static void internal_parse_typeC(char **s) { type_stack_mark(); type_stack_mark(); internal_parse_typeCC(s); type_stack_reverse();
5267b71995-08-09Fredrik Hübinette (Hubbe) 
bdb5091996-09-25Fredrik Hübinette (Hubbe)  while(ISSPACE(**s)) ++*s;
bce86c1996-02-25Fredrik Hübinette (Hubbe)  if(**s == '&') { ++*s; type_stack_mark(); internal_parse_typeC(s); type_stack_reverse(); type_stack_reverse(); push_type(T_AND); }else{ type_stack_reverse();
5267b71995-08-09Fredrik Hübinette (Hubbe)  }
bce86c1996-02-25Fredrik Hübinette (Hubbe) } static void internal_parse_type(char **s) { internal_parse_typeC(s);
5267b71995-08-09Fredrik Hübinette (Hubbe) 
bdb5091996-09-25Fredrik Hübinette (Hubbe)  while(ISSPACE(**s)) ++*s;
bce86c1996-02-25Fredrik Hübinette (Hubbe) 
5267b71995-08-09Fredrik Hübinette (Hubbe)  while(**s == '|') { ++*s;
bce86c1996-02-25Fredrik Hübinette (Hubbe)  internal_parse_typeC(s);
5267b71995-08-09Fredrik Hübinette (Hubbe)  push_type(T_OR); } } /* This function is used when adding simul efuns so that * the types for the functions can be easily stored in strings.
06983f1996-09-22Fredrik Hübinette (Hubbe)  * It takes a string on the exact same format as Pike and returns a type
5267b71995-08-09Fredrik Hübinette (Hubbe)  * struct. */
06983f1996-09-22Fredrik Hübinette (Hubbe) struct pike_string *parse_type(char *s)
5267b71995-08-09Fredrik Hübinette (Hubbe) { internal_parse_type(&s); if( *s ) fatal("Extra junk at end of type definition.\n"); return pop_type(); } #ifdef DEBUG void stupid_describe_type(char *a,INT32 len) { INT32 e; for(e=0;e<len;e++) { if(e) printf(" "); switch(EXTRACT_UCHAR(a+e)) { case T_INT: printf("int"); break; case T_FLOAT: printf("float"); break; case T_STRING: printf("string"); break; case T_PROGRAM: printf("program"); break;
d2c6081996-11-07Fredrik Hübinette (Hubbe)  case T_OBJECT: printf("object(%ld)",(long)EXTRACT_INT(a+e+1)); e+=sizeof(INT32); break;
5267b71995-08-09Fredrik Hübinette (Hubbe)  case T_FUNCTION: printf("function"); break; case T_ARRAY: printf("array"); break; case T_MAPPING: printf("mapping"); break;
06983f1996-09-22Fredrik Hübinette (Hubbe)  case T_MULTISET: printf("multiset"); break;
5267b71995-08-09Fredrik Hübinette (Hubbe)  case T_UNKNOWN: printf("unknown"); break; case T_MANY: printf("many"); break; case T_OR: printf("or"); break;
bce86c1996-02-25Fredrik Hübinette (Hubbe)  case T_AND: printf("and"); break; case T_NOT: printf("not"); break;
5267b71995-08-09Fredrik Hübinette (Hubbe)  case T_VOID: printf("void"); break; case T_MIXED: printf("mixed"); break; default: printf("%d",EXTRACT_UCHAR(a+e)); break; } } printf("\n"); }
0d202a1995-10-20Fredrik Hübinette (Hubbe) 
06983f1996-09-22Fredrik Hübinette (Hubbe) void simple_describe_type(struct pike_string *s)
0d202a1995-10-20Fredrik Hübinette (Hubbe) { stupid_describe_type(s->str,s->len); }
5267b71995-08-09Fredrik Hübinette (Hubbe) #endif char *low_describe_type(char *t) { switch(EXTRACT_UCHAR(t++)) {
bce86c1996-02-25Fredrik Hübinette (Hubbe)  case T_VOID: my_strcat("void"); break; case T_MIXED: my_strcat("mixed"); break; case T_UNKNOWN: my_strcat("unknown"); break; case T_INT: my_strcat("int"); break; case T_FLOAT: my_strcat("float"); break; case T_PROGRAM: my_strcat("program"); break;
d2c6081996-11-07Fredrik Hübinette (Hubbe)  case T_OBJECT: my_strcat("object"); /* Prog id */ break;
bce86c1996-02-25Fredrik Hübinette (Hubbe)  case T_STRING: my_strcat("string"); break;
5267b71995-08-09Fredrik Hübinette (Hubbe)  case T_FUNCTION: { int s; my_strcat("function("); s=0; while(EXTRACT_UCHAR(t) != T_MANY) { if(s++) my_strcat(", ");
bce86c1996-02-25Fredrik Hübinette (Hubbe)  t=low_describe_type(t);
5267b71995-08-09Fredrik Hübinette (Hubbe)  } t++;
bce86c1996-02-25Fredrik Hübinette (Hubbe)  if(EXTRACT_UCHAR(t) == T_VOID)
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
bce86c1996-02-25Fredrik Hübinette (Hubbe)  t++; }else{
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(s++) my_strcat(", ");
bce86c1996-02-25Fredrik Hübinette (Hubbe)  t=low_describe_type(t);
5267b71995-08-09Fredrik Hübinette (Hubbe)  my_strcat(" ..."); } my_strcat(" : ");
bce86c1996-02-25Fredrik Hübinette (Hubbe)  t=low_describe_type(t);
5267b71995-08-09Fredrik Hübinette (Hubbe)  my_strcat(")"); break; } case T_ARRAY:
bce86c1996-02-25Fredrik Hübinette (Hubbe)  if(EXTRACT_UCHAR(t)==T_MIXED) { my_strcat("array"); t++; }else{ t=low_describe_type(t); my_strcat("*"); }
5267b71995-08-09Fredrik Hübinette (Hubbe)  break;
06983f1996-09-22Fredrik Hübinette (Hubbe)  case T_MULTISET: my_strcat("multiset");
bce86c1996-02-25Fredrik Hübinette (Hubbe)  if(EXTRACT_UCHAR(t)!=T_MIXED) { my_strcat("("); t=low_describe_type(t); my_strcat(")"); }else{ t++; }
5267b71995-08-09Fredrik Hübinette (Hubbe)  break;
bce86c1996-02-25Fredrik Hübinette (Hubbe)  case T_NOT: my_strcat("!");
5267b71995-08-09Fredrik Hübinette (Hubbe)  t=low_describe_type(t); break; case T_OR: t=low_describe_type(t); my_strcat(" | "); t=low_describe_type(t); break;
bce86c1996-02-25Fredrik Hübinette (Hubbe)  case T_AND:
5267b71995-08-09Fredrik Hübinette (Hubbe)  t=low_describe_type(t);
bce86c1996-02-25Fredrik Hübinette (Hubbe)  my_strcat(" & ");
5267b71995-08-09Fredrik Hübinette (Hubbe)  t=low_describe_type(t);
bce86c1996-02-25Fredrik Hübinette (Hubbe)  break; case T_MAPPING: my_strcat("mapping"); if(EXTRACT_UCHAR(t)==T_MIXED && EXTRACT_UCHAR(t+1)==T_MIXED) { t+=2; }else{ my_strcat("("); t=low_describe_type(t); my_strcat(":"); t=low_describe_type(t); my_strcat(")"); }
5267b71995-08-09Fredrik Hübinette (Hubbe)  break; } return t; }
06983f1996-09-22Fredrik Hübinette (Hubbe) struct pike_string *describe_type(struct pike_string *type)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
bce86c1996-02-25Fredrik Hübinette (Hubbe)  if(!type) return make_shared_string("mixed"); init_buf(); low_describe_type(type->str); return free_buf(); } static TYPE_T low_compile_type_to_runtime_type(char *t) { TYPE_T tmp;
5267b71995-08-09Fredrik Hübinette (Hubbe)  switch(EXTRACT_UCHAR(t)) { case T_OR:
bce86c1996-02-25Fredrik Hübinette (Hubbe)  t++; tmp=low_compile_type_to_runtime_type(t); if(tmp == low_compile_type_to_runtime_type(t+type_length(t))) return tmp;
5267b71995-08-09Fredrik Hübinette (Hubbe)  case T_MANY: case T_UNKNOWN:
bce86c1996-02-25Fredrik Hübinette (Hubbe)  case T_AND: case T_NOT:
5267b71995-08-09Fredrik Hübinette (Hubbe)  return T_MIXED; default: return EXTRACT_UCHAR(t); } }
06983f1996-09-22Fredrik Hübinette (Hubbe) TYPE_T compile_type_to_runtime_type(struct pike_string *s)
bce86c1996-02-25Fredrik Hübinette (Hubbe) { return low_compile_type_to_runtime_type(s->str); } #define A_EXACT 1 #define B_EXACT 2
5267b71995-08-09Fredrik Hübinette (Hubbe) /* * match two type strings, return zero if they don't match, and return * the part of 'a' that _did_ match if it did. */
bce86c1996-02-25Fredrik Hübinette (Hubbe) static char *low_match_types(char *a,char *b, int flags)
5267b71995-08-09Fredrik Hübinette (Hubbe) { char *ret; if(a == b) return a;
bce86c1996-02-25Fredrik Hübinette (Hubbe)  switch(EXTRACT_UCHAR(a))
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
bce86c1996-02-25Fredrik Hübinette (Hubbe)  case T_AND:
5267b71995-08-09Fredrik Hübinette (Hubbe)  a++;
bce86c1996-02-25Fredrik Hübinette (Hubbe)  ret=low_match_types(a,b,flags); if(!ret) return 0; a+=type_length(a); return low_match_types(a,b,flags); case T_OR:
5267b71995-08-09Fredrik Hübinette (Hubbe)  a++;
bce86c1996-02-25Fredrik Hübinette (Hubbe)  ret=low_match_types(a,b,flags);
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(ret) return ret; a+=type_length(a);
bce86c1996-02-25Fredrik Hübinette (Hubbe)  return low_match_types(a,b,flags); case T_NOT: if(low_match_types(a+1,b,flags | B_EXACT)) return 0; return a;
5267b71995-08-09Fredrik Hübinette (Hubbe)  }
bce86c1996-02-25Fredrik Hübinette (Hubbe)  switch(EXTRACT_UCHAR(b))
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
bce86c1996-02-25Fredrik Hübinette (Hubbe)  case T_AND:
5267b71995-08-09Fredrik Hübinette (Hubbe)  b++;
bce86c1996-02-25Fredrik Hübinette (Hubbe)  ret=low_match_types(a,b,flags); if(!ret) return 0; b+=type_length(b); return low_match_types(a,b,flags); case T_OR:
5267b71995-08-09Fredrik Hübinette (Hubbe)  b++;
bce86c1996-02-25Fredrik Hübinette (Hubbe)  ret=low_match_types(a,b,flags);
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(ret) return ret; b+=type_length(b);
bce86c1996-02-25Fredrik Hübinette (Hubbe)  return low_match_types(a,b,flags); case T_NOT: if(low_match_types(a,b+1, flags | A_EXACT)) return 0; return a;
5267b71995-08-09Fredrik Hübinette (Hubbe)  }
0d202a1995-10-20Fredrik Hübinette (Hubbe)  /* 'mixed' matches anything */
bce86c1996-02-25Fredrik Hübinette (Hubbe)  if(EXTRACT_UCHAR(a) == T_MIXED && !(flags & A_EXACT)) return a; if(EXTRACT_UCHAR(b) == T_MIXED && !(flags & B_EXACT)) return a;
d2c6081996-11-07Fredrik Hübinette (Hubbe)  /* Special case (tm) */ if(EXTRACT_UCHAR(a) == T_PROGRAM && EXTRACT_UCHAR(b)==T_FUNCTION) { return a; }
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(EXTRACT_UCHAR(a) != EXTRACT_UCHAR(b)) return 0; ret=a; switch(EXTRACT_UCHAR(a)) { case T_FUNCTION: a++; b++; while(EXTRACT_UCHAR(a)!=T_MANY || EXTRACT_UCHAR(b)!=T_MANY) { char *a_tmp,*b_tmp; if(EXTRACT_UCHAR(a)==T_MANY) { a_tmp=a+1; }else{ a_tmp=a; a+=type_length(a); } if(EXTRACT_UCHAR(b)==T_MANY) {
0d202a1995-10-20Fredrik Hübinette (Hubbe)  b_tmp=b+1;
5267b71995-08-09Fredrik Hübinette (Hubbe)  }else{ b_tmp=b; b+=type_length(b); }
bce86c1996-02-25Fredrik Hübinette (Hubbe)  if(!low_match_types(a_tmp, b_tmp, flags)) return 0;
5267b71995-08-09Fredrik Hübinette (Hubbe)  } /* check the 'many' type */ a++; b++;
0d202a1995-10-20Fredrik Hübinette (Hubbe)  if(EXTRACT_UCHAR(b)==T_VOID || EXTRACT_UCHAR(a)==T_VOID)
5267b71995-08-09Fredrik Hübinette (Hubbe)  { a+=type_length(a);
0d202a1995-10-20Fredrik Hübinette (Hubbe)  b+=type_length(b);
5267b71995-08-09Fredrik Hübinette (Hubbe)  }else{
bce86c1996-02-25Fredrik Hübinette (Hubbe)  if(!low_match_types(a,b,flags)) return 0;
5267b71995-08-09Fredrik Hübinette (Hubbe)  } /* check the returntype */
bce86c1996-02-25Fredrik Hübinette (Hubbe)  if(!low_match_types(a,b,flags)) return 0;
5267b71995-08-09Fredrik Hübinette (Hubbe)  break; case T_MAPPING:
bce86c1996-02-25Fredrik Hübinette (Hubbe)  if(!low_match_types(++a,++b,flags)) return 0; if(!low_match_types(a+type_length(a),b+type_length(b),flags)) return 0;
5267b71995-08-09Fredrik Hübinette (Hubbe)  break;
d2c6081996-11-07Fredrik Hübinette (Hubbe)  case T_OBJECT: a++; b++; if(!EXTRACT_INT(a) || !EXTRACT_INT(b)) break; if(EXTRACT_INT(a) != EXTRACT_INT(b)) return 0; break;
06983f1996-09-22Fredrik Hübinette (Hubbe)  case T_MULTISET:
5267b71995-08-09Fredrik Hübinette (Hubbe)  case T_ARRAY:
bce86c1996-02-25Fredrik Hübinette (Hubbe)  if(!low_match_types(++a,++b,flags)) return 0;
5267b71995-08-09Fredrik Hübinette (Hubbe)  case T_INT: case T_FLOAT: case T_STRING: case T_PROGRAM: case T_VOID: case T_MIXED: break; default: fatal("error in type string.\n"); } return ret; } /* * Return the return type from a function call. */ static int low_get_return_type(char *a,char *b) {
bce86c1996-02-25Fredrik Hübinette (Hubbe)  int tmp; switch(EXTRACT_UCHAR(a))
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
bce86c1996-02-25Fredrik Hübinette (Hubbe)  case T_OR:
7260711996-06-21Fredrik Hübinette (Hubbe)  {
06983f1996-09-22Fredrik Hübinette (Hubbe)  struct pike_string *o1,*o2;
7260711996-06-21Fredrik Hübinette (Hubbe)  a++; o1=o2=0; type_stack_mark(); if(low_get_return_type(a,b)) { o1=pop_unfinished_type(); type_stack_mark(); } if(low_get_return_type(a+type_length(a),b)) o2=pop_unfinished_type(); else pop_stack_mark(); if(o1 == o2) { if(!o1) { return 0; }else{ push_finished_type(o1); } } else if(o1 == mixed_type_string || o2 == mixed_type_string) { push_type(T_MIXED); } else { if(o1) push_finished_type(o1); if(o2) push_finished_type(o2); if(o1 && o2) push_type(T_OR); } if(o1) free_string(o1); if(o2) free_string(o2); return 1; }
5267b71995-08-09Fredrik Hübinette (Hubbe) 
bce86c1996-02-25Fredrik Hübinette (Hubbe)  case T_AND:
5267b71995-08-09Fredrik Hübinette (Hubbe)  a++;
bce86c1996-02-25Fredrik Hübinette (Hubbe)  type_stack_mark();
5267b71995-08-09Fredrik Hübinette (Hubbe)  tmp=low_get_return_type(a,b);
bce86c1996-02-25Fredrik Hübinette (Hubbe)  type_stack_pop_to_mark(); if(!tmp) return 0; return low_get_return_type(a+type_length(a),b);
5267b71995-08-09Fredrik Hübinette (Hubbe) 
bce86c1996-02-25Fredrik Hübinette (Hubbe)  case T_ARRAY:
5267b71995-08-09Fredrik Hübinette (Hubbe)  a++; tmp=low_get_return_type(a,b); if(!tmp) return 0; push_type(T_ARRAY); return 1; }
bce86c1996-02-25Fredrik Hübinette (Hubbe)  a=low_match_types(a,b,0);
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(a) { switch(EXTRACT_UCHAR(a)) { case T_FUNCTION: a++; while(EXTRACT_UCHAR(a)!=T_MANY) a+=type_length(a); a++; a+=type_length(a); push_unfinished_type(a); return 1;
d2c6081996-11-07Fredrik Hübinette (Hubbe)  case T_PROGRAM: push_type_int(0); push_type(T_OBJECT); return 1;
5267b71995-08-09Fredrik Hübinette (Hubbe)  default: push_type(T_MIXED); return 1; } } return 0; }
06983f1996-09-22Fredrik Hübinette (Hubbe) int match_types(struct pike_string *a,struct pike_string *b)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
bce86c1996-02-25Fredrik Hübinette (Hubbe)  return 0!=low_match_types(a->str, b->str,0);
5267b71995-08-09Fredrik Hübinette (Hubbe) }
2a50961995-08-23Fredrik Hübinette (Hubbe) 
5267b71995-08-09Fredrik Hübinette (Hubbe)  /* FIXME, add the index */
06983f1996-09-22Fredrik Hübinette (Hubbe) static struct pike_string *low_index_type(char *t)
5267b71995-08-09Fredrik Hübinette (Hubbe) { switch(EXTRACT_UCHAR(t++)) { default: reference_shared_string(mixed_type_string); return mixed_type_string; case T_OR: {
06983f1996-09-22Fredrik Hübinette (Hubbe)  struct pike_string *a,*b;
5267b71995-08-09Fredrik Hübinette (Hubbe)  a=low_index_type(t); t+=type_length(t); b=low_index_type(t); if(!b) return a; if(!a) return b; push_finished_type(b); push_finished_type(a); push_type(T_OR); return pop_type(); }
bce86c1996-02-25Fredrik Hübinette (Hubbe)  case T_AND: return low_index_type(t+type_length(t));
5267b71995-08-09Fredrik Hübinette (Hubbe)  case T_STRING: /* always int */
06983f1996-09-22Fredrik Hübinette (Hubbe)  case T_MULTISET: /* always int */
5267b71995-08-09Fredrik Hübinette (Hubbe)  reference_shared_string(int_type_string); return int_type_string; case T_MAPPING: t+=type_length(t); case T_ARRAY: return make_shared_binary_string(t, type_length(t)); } }
06983f1996-09-22Fredrik Hübinette (Hubbe) struct pike_string *index_type(struct pike_string *type)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
06983f1996-09-22Fredrik Hübinette (Hubbe)  struct pike_string *t;
5267b71995-08-09Fredrik Hübinette (Hubbe)  t=low_index_type(type->str); if(!t) copy_shared_string(t,mixed_type_string); return t; } static int low_check_indexing(char *type, char *index_type) { switch(EXTRACT_UCHAR(type++)) { case T_OR: return low_check_indexing(type,index_type) || low_check_indexing(type+type_length(type),index_type);
bce86c1996-02-25Fredrik Hübinette (Hubbe)  case T_AND: return low_check_indexing(type,index_type) && low_check_indexing(type+type_length(type),index_type); case T_NOT: return !low_check_indexing(type,index_type);
5267b71995-08-09Fredrik Hübinette (Hubbe)  case T_STRING: case T_ARRAY:
bce86c1996-02-25Fredrik Hübinette (Hubbe)  return !!low_match_types(int_type_string->str, index_type,0);
5267b71995-08-09Fredrik Hübinette (Hubbe)  case T_OBJECT:
bce86c1996-02-25Fredrik Hübinette (Hubbe)  return !!low_match_types(string_type_string->str, index_type,0);
5267b71995-08-09Fredrik Hübinette (Hubbe) 
06983f1996-09-22Fredrik Hübinette (Hubbe)  case T_MULTISET:
5267b71995-08-09Fredrik Hübinette (Hubbe)  case T_MAPPING:
bce86c1996-02-25Fredrik Hübinette (Hubbe)  return !!low_match_types(type,index_type,0);
5267b71995-08-09Fredrik Hübinette (Hubbe)  case T_MIXED: return 1; default: return 0; } }
06983f1996-09-22Fredrik Hübinette (Hubbe) int check_indexing(struct pike_string *type, struct pike_string *index_type)
5267b71995-08-09Fredrik Hübinette (Hubbe) { return low_check_indexing(type->str, index_type->str); } /* Count the number of arguments for a funciton type. * return -1-n if the function can take number of arguments * >= n (varargs) */
06983f1996-09-22Fredrik Hübinette (Hubbe) int count_arguments(struct pike_string *s)
5267b71995-08-09Fredrik Hübinette (Hubbe) { int num; char *q; q=s->str; if(EXTRACT_UCHAR(q) != T_FUNCTION) return MAX_LOCAL; q++; num=0; while(EXTRACT_UCHAR(q)!=T_MANY) { num++; q+=type_length(q); } q++; if(EXTRACT_UCHAR(q)!=T_VOID) return ~num; return num; }
06983f1996-09-22Fredrik Hübinette (Hubbe) struct pike_string *check_call(struct pike_string *args, struct pike_string *type)
5267b71995-08-09Fredrik Hübinette (Hubbe) { reset_type_stack(); if(low_get_return_type(type->str,args->str)) { return pop_type(); }else{ return 0; } } void check_array_type(struct array *a) {
99946c1996-02-17Fredrik Hübinette (Hubbe)  push_type(T_MIXED);
5267b71995-08-09Fredrik Hübinette (Hubbe) }
06983f1996-09-22Fredrik Hübinette (Hubbe) struct pike_string *get_type_of_svalue(struct svalue *s)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
06983f1996-09-22Fredrik Hübinette (Hubbe)  struct pike_string *ret;
5267b71995-08-09Fredrik Hübinette (Hubbe)  switch(s->type) { case T_FUNCTION:
bdb5091996-09-25Fredrik Hübinette (Hubbe)  if(s->subtype == FUNCTION_BUILTIN)
5267b71995-08-09Fredrik Hübinette (Hubbe)  { ret=s->u.efun->type; }else{ struct program *p; p=s->u.object->prog; if(!p) { ret=int_type_string; }else{ ret=ID_FROM_INT(p,s->subtype)->type; } } reference_shared_string(ret); return ret; case T_ARRAY: check_array_type(s->u.array); push_type(T_ARRAY); return pop_type();
06983f1996-09-22Fredrik Hübinette (Hubbe)  case T_MULTISET: check_array_type(s->u.multiset->ind); push_type(T_MULTISET);
5267b71995-08-09Fredrik Hübinette (Hubbe)  return pop_type(); case T_MAPPING:
6557ff1996-06-09Fredrik Hübinette (Hubbe)  push_type(T_MIXED); push_type(T_MIXED);
5267b71995-08-09Fredrik Hübinette (Hubbe)  push_type(T_MAPPING); return pop_type();
041a731996-11-12Mirar (Pontus Hagland)  case T_OBJECT: push_type_int(0); push_type(T_OBJECT); return pop_type();
5267b71995-08-09Fredrik Hübinette (Hubbe)  default: push_type(s->type); return pop_type(); } } char *get_name_of_type(int t) { switch(t) { case T_LVALUE: return "lvalue"; case T_INT: return "int"; case T_STRING: return "string"; case T_ARRAY: return "array"; case T_OBJECT: return "object"; case T_MAPPING: return "mapping";
06983f1996-09-22Fredrik Hübinette (Hubbe)  case T_MULTISET: return "multiset";
5267b71995-08-09Fredrik Hübinette (Hubbe)  case T_FUNCTION: return "function"; case T_FLOAT: return "float"; default: return "unknown"; } }
06983f1996-09-22Fredrik Hübinette (Hubbe) void cleanup_pike_types()
5267b71995-08-09Fredrik Hübinette (Hubbe) { free_string(string_type_string); free_string(int_type_string); free_string(float_type_string); free_string(function_type_string); free_string(object_type_string); free_string(program_type_string); free_string(array_type_string);
06983f1996-09-22Fredrik Hübinette (Hubbe)  free_string(multiset_type_string);
5267b71995-08-09Fredrik Hübinette (Hubbe)  free_string(mapping_type_string); free_string(mixed_type_string); free_string(void_type_string);
f6f02d1995-10-16Fredrik Hübinette (Hubbe)  free_string(any_type_string);
5267b71995-08-09Fredrik Hübinette (Hubbe) }