6 #include "expression.h"
9 static const char *parse_escape(const char *p
, unsigned *val
, const char *end
, int bits
, struct position pos
)
20 case 'a': c
= '\a'; break;
21 case 'b': c
= '\b'; break;
22 case 't': c
= '\t'; break;
23 case 'n': c
= '\n'; break;
24 case 'v': c
= '\v'; break;
25 case 'f': c
= '\f'; break;
26 case 'r': c
= '\r'; break;
27 case 'e': c
= '\e'; break;
29 unsigned mask
= -(1U << (bits
- 4));
30 for (c
= 0; p
< end
; c
= (c
<< 4) + d
) {
37 "hex escape sequence out of range");
47 while (p
< end
&& (d
= *p
- '0') < 8) {
51 if ((c
& 0400) && bits
< 9)
53 "octal escape sequence out of range");
56 default: /* everything else is left as is */
57 warning(pos
, "unknown escape sequence: '\\%c'", c
);
63 break; /* those are legal, so no warnings */
65 *val
= c
& ~((~0U << (bits
- 1)) << 1);
69 void get_char_constant(struct token
*token
, unsigned long long *val
)
71 const char *p
= token
->embedded
, *end
;
73 int type
= token_type(token
);
77 p
= token
->string
->data
;
78 end
= p
+ token
->string
->length
- 1;
80 sparse_error(token
->pos
, "empty character constant");
85 case TOKEN_CHAR_EMBEDDED_0
... TOKEN_CHAR_EMBEDDED_3
:
86 end
= p
+ type
- TOKEN_CHAR
;
89 end
= p
+ type
- TOKEN_WIDE_CHAR
;
91 p
= parse_escape(p
, &v
, end
,
92 type
< TOKEN_WIDE_CHAR
? bits_in_char
: wchar_ctype
->bit_size
, token
->pos
);
95 "multi-character character constant");
99 struct token
*get_string_constant(struct token
*token
, struct expression
*expr
)
101 struct string
*string
= token
->string
;
102 struct token
*next
= token
->next
, *done
= NULL
;
103 int stringtype
= token_type(token
);
104 int is_wide
= stringtype
== TOKEN_WIDE_STRING
;
105 static char buffer
[MAX_STRING
];
111 switch (token_type(next
)) {
112 case TOKEN_WIDE_STRING
:
121 bits
= is_wide
? wchar_ctype
->bit_size
: bits_in_char
;
122 while (token
!= done
) {
124 const char *p
= token
->string
->data
;
125 const char *end
= p
+ token
->string
->length
- 1;
129 p
= parse_escape(p
, &v
, end
, bits
, token
->pos
);
130 if (len
< MAX_STRING
)
136 if (len
> MAX_STRING
) {
137 warning(token
->pos
, "trying to concatenate %d-character string (%d bytes max)", len
, MAX_STRING
);
141 if (esc_count
|| len
>= string
->length
) {
142 if (string
->immutable
|| len
>= string
->length
) /* can't cannibalize */
143 string
= __alloc_string(len
+1);
144 string
->length
= len
+1;
145 memcpy(string
->data
, buffer
, len
);
146 string
->data
[len
] = '\0';
148 expr
->string
= string
;
149 expr
->wide
= is_wide
;