diff mbox series

[v3,10/19] give a type to wchar

Message ID 20181214001536.6259-11-luc.vanoostenryck@gmail.com (mailing list archive)
State Mainlined, archived
Headers show
Series predefined macros for intmax_t/intptr_t/... | expand

Commit Message

Luc Van Oostenryck Dec. 14, 2018, 12:15 a.m. UTC
This allows to use predefined_ctype() on wchar_t.

Note: currently __WCHAR_TYPE__ is defined to 'int'
      but this is incorrect on: i386, m68k, ppc32, ...

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 char.c   |  4 ++--
 lib.c    |  6 +++---
 lib.h    |  1 +
 symbol.h |  1 +
 target.c | 27 +++++++++++++++++++++++++--
 target.h |  3 +--
 6 files changed, 33 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/char.c b/char.c
index c52521bc8..f26b2a806 100644
--- a/char.c
+++ b/char.c
@@ -84,7 +84,7 @@  void get_char_constant(struct token *token, unsigned long long *val)
 		end = p + type - TOKEN_WIDE_CHAR;
 	}
 	p = parse_escape(p, &v, end,
-			type < TOKEN_WIDE_CHAR ? bits_in_char : bits_in_wchar, token->pos);
+			type < TOKEN_WIDE_CHAR ? bits_in_char : wchar_ctype->bit_size, token->pos);
 	if (p != end)
 		warning(token->pos,
 			"multi-character character constant");
@@ -113,7 +113,7 @@  struct token *get_string_constant(struct token *token, struct expression *expr)
 			done = next;
 		}
 	}
-	bits = is_wide ? bits_in_wchar : bits_in_char;
+	bits = is_wide ? wchar_ctype->bit_size: bits_in_char;
 	while (token != done) {
 		unsigned v;
 		const char *p = token->string->data;
diff --git a/lib.c b/lib.c
index 2a383f000..dde9f7c36 100644
--- a/lib.c
+++ b/lib.c
@@ -317,6 +317,7 @@  static enum { STANDARD_C89,
 int arch_m64 = ARCH_M64_DEFAULT;
 int arch_msize_long = 0;
 int arch_big_endian = ARCH_BIG_ENDIAN;
+int arch_mach = MACH_NATIVE;
 
 
 #define CMDLINE_INCLUDE 20
@@ -1224,14 +1225,12 @@  static void predefined_macros(void)
 		break;
 	}
 
-	predefined_sizeof("WCHAR", "_T", bits_in_wchar);
-	predefined_max("WCHAR", "", bits_in_wchar);
-	predefined_width("WCHAR",   bits_in_wchar);
 	predefine("__CHAR_BIT__", 1, "%d", bits_in_char);
 
 	predefined_ctype("SHORT",     &short_ctype, PTYPE_SIZEOF);
 	predefined_ctype("SHRT",      &short_ctype, PTYPE_MAX|PTYPE_WIDTH);
 	predefined_ctype("SCHAR",      &char_ctype, PTYPE_MAX|PTYPE_WIDTH);
+	predefined_ctype("WCHAR",      wchar_ctype, PTYPE_ALL_T|PTYPE_TYPE);
 
 	predefined_ctype("INT",         &int_ctype, PTYPE_ALL);
 	predefined_ctype("LONG",       &long_ctype, PTYPE_ALL);
@@ -1408,6 +1407,7 @@  struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list
 	list = NULL;
 	if (filelist) {
 		// Initialize type system
+		init_target();
 		init_ctype();
 
 		predefined_macros();
diff --git a/lib.h b/lib.h
index cde74e93e..a97e70b67 100644
--- a/lib.h
+++ b/lib.h
@@ -196,6 +196,7 @@  extern int funsigned_char;
 extern int arch_m64;
 extern int arch_msize_long;
 extern int arch_big_endian;
+extern int arch_mach;
 
 extern void dump_macro_definitions(void);
 extern struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **files);
diff --git a/symbol.h b/symbol.h
index f9fb4bc5a..287be628b 100644
--- a/symbol.h
+++ b/symbol.h
@@ -303,6 +303,7 @@  extern void init_symbols(void);
 extern void init_builtins(int stream);
 extern void declare_builtins(void);
 extern void init_ctype(void);
+extern void init_target(void);
 extern struct symbol *alloc_symbol(struct position, int type);
 extern void show_type(struct symbol *);
 extern const char *modifier_string(unsigned long mod);
diff --git a/target.c b/target.c
index 86a9e2e63..27503e244 100644
--- a/target.c
+++ b/target.c
@@ -2,9 +2,11 @@ 
 
 #include "symbol.h"
 #include "target.h"
+#include "machine.h"
 
 struct symbol *size_t_ctype = &uint_ctype;
 struct symbol *ssize_t_ctype = &int_ctype;
+struct symbol *wchar_ctype = &int_ctype;
 
 /*
  * For "__attribute__((aligned))"
@@ -22,8 +24,6 @@  int bits_in_long = 32;
 int bits_in_longlong = 64;
 int bits_in_longlonglong = 128;
 
-int bits_in_wchar = 32;
-
 int max_int_alignment = 4;
 
 /*
@@ -46,3 +46,26 @@  int pointer_alignment = 4;
  */
 int bits_in_enum = 32;
 int enum_alignment = 4;
+
+
+void init_target(void)
+{
+	switch (arch_mach) {
+	case MACH_X86_64:
+		if (arch_m64 == ARCH_LP64)
+			break;
+		/* fall through */
+	case MACH_I386:
+	case MACH_M68K:
+	case MACH_SPARC32:
+	case MACH_PPC32:
+		wchar_ctype = &long_ctype;
+		break;
+	case MACH_ARM:
+	case MACH_ARM64:
+		wchar_ctype = &uint_ctype;
+		break;
+	default:
+		break;
+	}
+}
diff --git a/target.h b/target.h
index 8326fa21a..74f51ffeb 100644
--- a/target.h
+++ b/target.h
@@ -3,6 +3,7 @@ 
 
 extern struct symbol *size_t_ctype;
 extern struct symbol *ssize_t_ctype;
+extern struct symbol *wchar_ctype;
 
 /*
  * For "__attribute__((aligned))"
@@ -20,8 +21,6 @@  extern int bits_in_long;
 extern int bits_in_longlong;
 extern int bits_in_longlonglong;
 
-extern int bits_in_wchar;
-
 extern int max_int_alignment;
 
 /*