#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
#include <sys/cdefs.h>
#if defined(__RCSID)
__RCSID("$NetBSD: tree.c,v 1.716 2026/07/15 05:35:09 rillig Exp $");
#endif
#include <float.h>
#include <limits.h>
#include <math.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include "lint1.h"
typedef struct integer_constraints {
int64_t smin;
int64_t smax;
uint64_t umin;
uint64_t umax;
uint64_t bclr;
} integer_constraints;
static int64_t
s64_min(int64_t a, int64_t b)
{
return a < b ? a : b;
}
static int64_t
s64_max(int64_t a, int64_t b)
{
return a > b ? a : b;
}
static uint64_t
s64_abs(int64_t x)
{
return x >= 0 ? (uint64_t)x : -(uint64_t)x;
}
static int64_t
s64_shr(int64_t x, unsigned amount)
{
return x >= 0
? (int64_t)((uint64_t)x >> amount)
: (int64_t)~(~(uint64_t)x >> amount);
}
static uint64_t
u64_min(uint64_t a, uint64_t b)
{
return a < b ? a : b;
}
static uint64_t
u64_max(uint64_t a, uint64_t b)
{
return a > b ? a : b;
}
static uint64_t
u64_fill_right(uint64_t x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
return x;
}
static unsigned
u64_width(uint64_t x)
{
unsigned m = 0;
if (x >> 32 != 0)
x >>= 32, m += 32;
if (x >> 16 != 0)
x >>= 16, m += 16;
if (x >> 8 != 0)
x >>= 8, m += 8;
if (x >> 4 != 0)
x >>= 4, m += 4;
if (x >> 2 != 0)
x >>= 2, m += 2;
if (x >> 1 != 0)
x >>= 1, m++;
if (x > 0)
m++;
return m;
}
static int
portable_rank_cmp(tspec_t t1, tspec_t t2)
{
const ttab_t *p1 = type_properties(t1), *p2 = type_properties(t2);
lint_assert(p1->tt_rank_kind == p2->tt_rank_kind);
lint_assert(p1->tt_rank_value > 0);
lint_assert(p2->tt_rank_value > 0);
return (int)p1->tt_rank_value - (int)p2->tt_rank_value;
}
static unsigned
width_in_bits(const type_t *tp)
{
lint_assert(is_integer(tp->t_tspec));
return tp->t_bitfield
? tp->t_bit_field_width
: size_in_bits(tp->t_tspec);
}
static uint64_t
ui_max_value(const type_t *tp)
{
return value_bits(width_in_bits(tp));
}
static int64_t
si_max_value(const type_t *tp)
{
return (int64_t)(ui_max_value(tp) >> 1);
}
static int64_t
si_min_value(const type_t *tp)
{
return -si_max_value(tp) - 1;
}
static int64_t
si_mult_sat(const type_t *tp, int64_t l, int64_t r)
{
uint64_t al = s64_abs(l);
uint64_t ar = s64_abs(r);
bool neg = (l >= 0) != (r >= 0);
int64_t max = si_max_value(tp);
uint64_t max_prod = (uint64_t)max + (neg ? 1 : 0);
if (al == 0 || ar <= max_prod / al)
return l * r;
else if (neg)
return -1 - max;
else
return max;
}
static int64_t
si_plus_sat(const type_t *tp, int64_t a, int64_t b)
{
if (b >= 0) {
int64_t max = si_max_value(tp);
return a <= max - b ? a + b : max;
} else {
int64_t min = si_min_value(tp);
return a >= min - b ? a + b : min;
}
}
static int64_t
si_minus_sat(const type_t *tp, int64_t a, int64_t b)
{
if (b >= 0) {
int64_t min = si_min_value(tp);
return a >= min + b ? a - b : min;
} else {
int64_t max = si_max_value(tp);
return a <= max + b ? a - b : max;
}
}
static bool
ic_maybe_signed(const type_t *tp, integer_constraints ic)
{
return !is_uinteger(tp->t_tspec) && ic.bclr >> 63 == 0;
}
static bool
ic_maybe_signed_binary(const type_t *tp,
integer_constraints a, integer_constraints b)
{
return !is_uinteger(tp->t_tspec) && (a.bclr & b.bclr) >> 63 == 0;
}
static integer_constraints
ic_any(const type_t *tp)
{
integer_constraints c;
unsigned width = width_in_bits(tp);
uint64_t vbits = value_bits(width);
if (is_uinteger(tp->t_tspec)) {
c.smin = width < 64 ? 0 : INT64_MIN;
c.smax = width < 64 ? (int64_t)vbits : INT64_MAX;
c.umin = 0;
c.umax = vbits;
c.bclr = ~c.umax;
} else {
c.smin = -1 - (int64_t)(vbits >> 1);
c.smax = (int64_t)(vbits >> 1);
c.umin = 0;
c.umax = UINT64_MAX;
c.bclr = 0;
}
return c;
}
static integer_constraints
ic_mult(const type_t *tp, integer_constraints a, integer_constraints b)
{
integer_constraints c;
if (ic_maybe_signed_binary(tp, a, b)) {
int64_t ll = si_mult_sat(tp, a.smin, b.smin);
int64_t lu = si_mult_sat(tp, a.smin, b.smax);
int64_t ul = si_mult_sat(tp, a.smax, b.smin);
int64_t uu = si_mult_sat(tp, a.smax, b.smax);
c.smin = s64_min(ll, s64_min(lu, s64_min(ul, uu)));
c.smax = s64_max(ll, s64_max(lu, s64_max(ul, uu)));
c.umin = c.smin >= 0 ? (uint64_t)c.smin : 0;
c.umax = c.smin >= 0 ? (uint64_t)c.smax : UINT64_MAX;
c.bclr = ~u64_fill_right(c.umax);
return c;
}
if (a.umax > 0 && b.umax > ic_any(tp).umax / a.umax)
return ic_any(tp);
c.smin = INT64_MIN;
c.smax = INT64_MAX;
c.umin = a.umin * b.umin;
c.umax = a.umax * b.umax;
c.bclr = ~u64_fill_right(c.umax);
return c;
}
static integer_constraints
ic_div(const type_t *tp, integer_constraints a, integer_constraints b)
{
if (ic_maybe_signed_binary(tp, a, b)) {
if (b.smin >= 0)
return a;
return ic_any(tp);
}
integer_constraints c;
c.smin = INT64_MIN;
c.smax = INT64_MAX;
c.umin = a.umin / u64_max(b.umax, 1);
c.umax = a.umax / u64_max(b.umin, 1);
c.bclr = ~u64_fill_right(c.umax);
return c;
}
static integer_constraints
ic_mod(const type_t *tp, integer_constraints a, integer_constraints b)
{
if (ic_maybe_signed_binary(tp, a, b)) {
uint64_t max_abs_b = u64_max(s64_abs(b.smin), s64_abs(b.smax));
if (max_abs_b >> 63 != 0 || max_abs_b == 0)
return a;
integer_constraints c;
c.smin = s64_max(a.smin, -(int64_t)(max_abs_b - 1));
c.smax = s64_min(a.smax, (int64_t)(max_abs_b - 1));
c.umin = 0;
c.umax = UINT64_MAX;
c.bclr = 0;
return c;
}
integer_constraints c;
c.smin = INT64_MIN;
c.smax = INT64_MAX;
c.umin = 0;
c.umax = b.umax - 1;
c.bclr = ~u64_fill_right(c.umax);
return c;
}
static integer_constraints
ic_plus(const type_t *tp, integer_constraints a, integer_constraints b)
{
if (ic_maybe_signed_binary(tp, a, b)) {
integer_constraints c;
c.smin = si_plus_sat(tp, a.smin, b.smin);
c.smax = si_plus_sat(tp, a.smax, b.smax);
c.umin = c.smin >= 0 ? (uint64_t)c.smin : 0;
c.umax = c.smin >= 0 ? (uint64_t)c.smax : UINT64_MAX;
c.bclr = 0;
return c;
}
uint64_t max = ui_max_value(tp);
integer_constraints c;
c.smin = INT64_MIN;
c.smax = INT64_MAX;
if (b.umax <= max - a.umax) {
c.umin = a.umin + b.umin;
c.umax = a.umax + b.umax;
} else {
c.umin = 0;
c.umax = max;
}
if (c.umax >> 63 == 0) {
c.smin = 0;
c.smax = (int64_t)c.umax;
}
c.bclr = ~u64_fill_right(c.umax);
return c;
}
static integer_constraints
ic_minus(const type_t *tp, integer_constraints a, integer_constraints b)
{
integer_constraints c;
c.smin = si_minus_sat(tp, a.smin, b.smax);
c.smax = si_minus_sat(tp, a.smax, b.smin);
if (ic_maybe_signed_binary(tp, a, b)) {
c.umin = c.smin >= 0 ? (uint64_t)c.smin : 0;
c.umax = c.smin >= 0 ? (uint64_t)c.smax : UINT64_MAX;
} else if (a.umin >= b.umax) {
c.umin = a.umin - b.umax;
c.umax = a.umax - b.umin;
} else {
c.umin = 0;
c.umax = is_uinteger(tp->t_tspec) ? ui_max_value(tp)
: UINT64_MAX;
}
c.bclr = ~u64_fill_right(c.umax);
return c;
}
static integer_constraints
ic_shl(const type_t *tp, integer_constraints a, integer_constraints b)
{
if (ic_maybe_signed(tp, a))
return ic_any(tp);
unsigned amount;
if (b.smin == b.smax && b.smin >= 0 && b.smin < 64)
amount = (unsigned)b.smin;
else if (b.umin == b.umax && b.umin < 64)
amount = (unsigned)b.umin;
else
return ic_any(tp);
integer_constraints c;
c.umin = a.umin << amount;
c.umax = a.umax << amount;
if (c.umax >> (width_in_bits(tp) - 1) == 0) {
c.smin = (int64_t)c.umin;
c.smax = (int64_t)c.umax;
} else {
c.smin = INT64_MIN;
c.smax = INT64_MAX;
}
c.bclr = a.bclr << amount | (((uint64_t)1 << amount) - 1);
return c;
}
static integer_constraints
ic_shr(const type_t *tp, integer_constraints a, integer_constraints b)
{
if (ic_maybe_signed(tp, a))
return ic_any(tp);
unsigned amount;
if (b.smin == b.smax && b.smin >= 0 && b.smin < 64)
amount = (unsigned)b.smin;
else if (b.umin == b.umax && b.umin < 64)
amount = (unsigned)b.umin;
else
return ic_any(tp);
integer_constraints c;
c.smin = s64_shr(a.smin, amount);
c.smax = s64_shr(a.smax, amount);
c.umin = a.umin >> amount;
c.umax = a.umax >> amount;
c.bclr = a.bclr >> amount | ~(~(uint64_t)0 >> amount);
return c;
}
static integer_constraints
ic_bitand(integer_constraints a, integer_constraints b)
{
integer_constraints c;
c.smin = a.smin & b.smin;
c.smax = a.smax & b.smax;
c.umin = a.umin & b.umin;
c.umax = a.umax & b.umax;
c.bclr = a.bclr | b.bclr;
return c;
}
static integer_constraints
ic_bitxor(const type_t *tp, integer_constraints a, integer_constraints b)
{
if (ic_maybe_signed_binary(tp, a, b))
return ic_any(tp);
integer_constraints c;
c.smin = a.smin & b.smin;
c.smax = a.smax | b.smax;
c.umin = a.umin & b.umin;
c.umax = a.umax | b.umax;
c.bclr = a.bclr & b.bclr;
return c;
}
static integer_constraints
ic_bitor(integer_constraints a, integer_constraints b)
{
integer_constraints c;
c.smin = a.smin | b.smin;
c.smax = a.smax | b.smax;
c.umin = a.umin | b.umin;
c.umax = a.umax | b.umax;
c.bclr = a.bclr & b.bclr;
return c;
}
static integer_constraints
ic_quest_colon(integer_constraints a, integer_constraints b)
{
integer_constraints c;
c.smin = s64_min(a.smin, b.smin);
c.smax = s64_max(a.smax, b.smax);
c.umin = u64_min(a.umin, b.umin);
c.umax = u64_max(a.umax, b.umax);
c.bclr = a.bclr & b.bclr;
return c;
}
static integer_constraints
ic_con(const type_t *tp, const val_t *v)
{
lint_assert(is_integer(tp->t_tspec));
int64_t si = v->u.integer;
uint64_t ui = (uint64_t)si;
integer_constraints c;
c.smin = si;
c.smax = si;
c.umin = ui;
c.umax = ui;
c.bclr = ~ui;
return c;
}
static integer_constraints
ic_cvt(const type_t *ntp, const type_t *otp, integer_constraints a)
{
unsigned new_width = width_in_bits(ntp);
unsigned old_width = width_in_bits(otp);
bool new_unsigned = is_uinteger(ntp->t_tspec);
bool old_unsigned = is_uinteger(otp->t_tspec);
if (new_width >= old_width && new_unsigned == old_unsigned)
return a;
if (new_width > old_width && old_unsigned)
return a;
if (new_unsigned && (~value_bits(new_width) & ~a.bclr) == 0)
return a;
return ic_any(ntp);
}
static integer_constraints
ic_unsigned_range(uint64_t minimum, uint64_t maximum)
{
integer_constraints c;
lint_assert(minimum <= maximum);
c.smin = maximum <= INT64_MAX ? (int64_t)minimum : INT64_MIN;
c.smax = maximum <= INT64_MAX ? (int64_t)maximum : INT64_MAX;
c.umin = minimum;
c.umax = maximum;
c.bclr = ~c.umax;
return c;
}
static integer_constraints
ic_signed_range(int64_t minimum, int64_t maximum)
{
integer_constraints c;
lint_assert(minimum <= maximum);
c.smin = minimum;
c.smax = maximum;
c.umin = minimum >= 0 ? (uint64_t)minimum : 0;
c.umax = minimum >= 0 ? (uint64_t)maximum : UINT64_MAX;
c.bclr = ~c.umax;
return c;
}
static integer_constraints
ic_call(const function_call *call)
{
if (!(call->func->tn_op == ADDR
&& call->func->u.ops.left->tn_op == NAME))
goto any;
const char *name = call->func->u.ops.left->u.sym->s_name;
if (strcmp(name, "strlen") == 0
|| strcmp(name, "strcspn") == 0
|| strcmp(name, "strspn") == 0
|| strcmp(name, "strlcpy") == 0
|| strcmp(name, "strlcat") == 0)
return ic_unsigned_range(0, INT_MAX - 1);
if ((strcmp(name, "read") == 0 || strcmp(name, "write") == 0)
&& call->args_len == 3
&& call->args[2]->tn_op == CON
&& is_uinteger(call->args[2]->tn_type->t_tspec)
&& call->args[2]->u.value.u.integer >= 0)
return ic_signed_range(-1, call->args[2]->u.value.u.integer);
if (strcmp(name, "__builtin_clz") == 0
|| strcmp(name, "__builtin_ctz") == 0
|| strcmp(name, "__builtin_clrsb") == 0)
return ic_unsigned_range(0, INT_SIZE - 1);
if (strcmp(name, "__builtin_ffs") == 0
|| strcmp(name, "__builtin_popcount") == 0)
return ic_unsigned_range(0, INT_SIZE);
if (strcmp(name, "__builtin_clzl") == 0
|| strcmp(name, "__builtin_ctzl") == 0
|| strcmp(name, "__builtin_clrsbl") == 0)
return ic_unsigned_range(0, LONG_SIZE - 1);
if (strcmp(name, "__builtin_ffsl") == 0
|| strcmp(name, "__builtin_popcountl") == 0)
return ic_unsigned_range(0, LONG_SIZE);
if (strcmp(name, "__builtin_clzll") == 0
|| strcmp(name, "__builtin_ctzll") == 0
|| strcmp(name, "__builtin_clrsbll") == 0)
return ic_unsigned_range(0, LLONG_SIZE - 1);
if (strcmp(name, "__builtin_ffsll") == 0
|| strcmp(name, "__builtin_popcountll") == 0)
return ic_unsigned_range(0, LLONG_SIZE);
if ((strcmp(name, "__builtin_clzg") == 0
|| strcmp(name, "__builtin_ctzg") == 0
|| strcmp(name, "__builtin_clrsbg") == 0)
&& call->args_len == 1
&& is_integer(call->args[0]->tn_type->t_tspec))
return ic_unsigned_range(0,
width_in_bits(call->args[0]->tn_type) - 1);
if ((strcmp(name, "__builtin_ffsg") == 0
|| strcmp(name, "__builtin_popcountg") == 0)
&& call->args_len == 1
&& is_integer(call->args[0]->tn_type->t_tspec))
return ic_unsigned_range(0,
width_in_bits(call->args[0]->tn_type));
if (strcmp(name, "__builtin_parity") == 0
|| strcmp(name, "__builtin_parityl") == 0
|| strcmp(name, "__builtin_parityll") == 0
|| strcmp(name, "__builtin_parityg") == 0)
return ic_unsigned_range(0, 1);
any:
return ic_any(call->func->tn_type->t_subt->t_subt);
}
static integer_constraints
ic_expr(const tnode_t *tn)
{
integer_constraints lc, rc;
lint_assert(is_integer(tn->tn_type->t_tspec));
switch (tn->tn_op) {
case MULT:
lc = ic_expr(tn->u.ops.left);
rc = ic_expr(tn->u.ops.right);
return ic_mult(tn->tn_type, lc, rc);
case DIV:
lc = ic_expr(tn->u.ops.left);
rc = ic_expr(tn->u.ops.right);
return ic_div(tn->tn_type, lc, rc);
case MOD:
lc = ic_expr(tn->u.ops.left);
rc = ic_expr(tn->u.ops.right);
return ic_mod(tn->tn_type, lc, rc);
case PLUS:
lc = ic_expr(tn->u.ops.left);
rc = ic_expr(tn->u.ops.right);
return ic_plus(tn->tn_type, lc, rc);
case MINUS:
if (tn->u.ops.left->tn_type->t_tspec == PTR)
return ic_any(tn->tn_type);
lc = ic_expr(tn->u.ops.left);
rc = ic_expr(tn->u.ops.right);
return ic_minus(tn->tn_type, lc, rc);
case SHL:
lc = ic_expr(tn->u.ops.left);
rc = ic_expr(tn->u.ops.right);
return ic_shl(tn->tn_type, lc, rc);
case SHR:
lc = ic_expr(tn->u.ops.left);
rc = ic_expr(tn->u.ops.right);
return ic_shr(tn->tn_type, lc, rc);
case BITAND:
lc = ic_expr(tn->u.ops.left);
rc = ic_expr(tn->u.ops.right);
return ic_bitand(lc, rc);
case BITXOR:
lc = ic_expr(tn->u.ops.left);
rc = ic_expr(tn->u.ops.right);
return ic_bitxor(tn->tn_type, lc, rc);
case BITOR:
lc = ic_expr(tn->u.ops.left);
rc = ic_expr(tn->u.ops.right);
return ic_bitor(lc, rc);
case QUEST:
lc = ic_expr(tn->u.ops.right->u.ops.left);
rc = ic_expr(tn->u.ops.right->u.ops.right);
return ic_quest_colon(lc, rc);
case CON:
return ic_con(tn->tn_type, &tn->u.value);
case CVT:
if (!is_integer(tn->u.ops.left->tn_type->t_tspec))
return ic_any(tn->tn_type);
lc = ic_expr(tn->u.ops.left);
return ic_cvt(tn->tn_type, tn->u.ops.left->tn_type, lc);
case CALL:
return ic_call(tn->u.call);
default:
return ic_any(tn->tn_type);
}
}
uint64_t
possible_bits(const tnode_t *tn)
{
return ~ic_expr(tn).bclr;
}
static struct {
size_t len;
size_t cap;
evaluation_mode *items;
} evaluation_modes;
void
push_evaluation_mode(evaluation_mode m)
{
if (evaluation_modes.len >= evaluation_modes.cap) {
evaluation_modes.cap = 2 * evaluation_modes.cap + 16;
evaluation_modes.items = xrealloc(
evaluation_modes.items,
evaluation_modes.cap * sizeof(*evaluation_modes.items));
}
evaluation_mode top = evaluation_modes.len > 0
? evaluation_modes.items[evaluation_modes.len - 1]
: EM_EVAL;
evaluation_modes.items[evaluation_modes.len++] =
(int)top < (int)m ? top : m;
}
void
pop_evaluation_mode(void)
{
evaluation_modes.len--;
}
bool
is_evaluation_mode(evaluation_mode m)
{
evaluation_mode top = evaluation_modes.len > 0
? evaluation_modes.items[evaluation_modes.len - 1]
: EM_EVAL;
return (int)top >= (int)m;
}
bool
attributes_contain(const attribute_list *attrs, const char *str)
{
for (size_t i = 0, n = attrs->len; i < n; i++) {
const attribute *attr = attrs->attrs + i;
if (attr->prefix == NULL && strcmp(attr->name, str) == 0)
return true;
}
return false;
}
type_t *
block_derive_type(type_t *tp, tspec_t t)
{
type_t *tp2 = block_zero_alloc(sizeof(*tp2), "type");
tp2->t_tspec = t;
tp2->t_subt = tp;
return tp2;
}
static type_t *
expr_derive_ptr_type(type_t *tp)
{
type_t *tp2 = expr_zero_alloc(sizeof(*tp2), "type");
tp2->t_tspec = PTR;
tp2->t_subt = tp;
return tp2;
}
static const char *
function_call_descr(const function_call *call)
{
if ((call->func->tn_op == ADDR || call->func->tn_op == LOAD)
&& call->func->u.ops.left->tn_op == NAME)
return call->func->u.ops.left->u.sym->s_name;
return type_name(call->func->tn_type->t_subt);
}
static size_t
str_len(const tnode_t *tn)
{
const buffer *buf = tn->u.str_literals;
if (tn->tn_type->t_subt->t_tspec != CHAR)
return buf->len;
quoted_iterator it = { .end = 0 };
size_t len = 0;
while (quoted_next(buf, &it))
len++;
return len;
}
static tnode_t *
build_op(op_t op, bool sys, type_t *type, tnode_t *ln, tnode_t *rn)
{
tnode_t *ntn = expr_alloc_tnode();
ntn->tn_op = op;
ntn->tn_type = type;
ntn->tn_sys = sys;
ntn->u.ops.left = ln;
ntn->u.ops.right = rn;
if (op == INDIR || op == FSEL) {
lint_assert(ln->tn_type->t_tspec == PTR);
tspec_t t = ln->tn_type->t_subt->t_tspec;
ntn->tn_lvalue = t != FUNC && t != VOID;
}
return ntn;
}
tnode_t *
build_constant(type_t *tp, val_t *v)
{
tnode_t *n = expr_alloc_tnode();
n->tn_op = CON;
n->tn_type = tp;
n->u.value = *v;
n->u.value.v_tspec = tp->t_tspec;
free(v);
return n;
}
static tnode_t *
build_integer_constant(tspec_t t, int64_t si)
{
tnode_t *n = expr_alloc_tnode();
n->tn_op = CON;
n->tn_type = gettyp(t);
n->u.value.v_tspec = t;
n->u.value.v_unsigned_since_c90 = false;
n->u.value.v_char_constant = false;
n->u.value.u.integer = si;
return n;
}
static void
fallback_symbol(sym_t *sym)
{
if (Tflag && fallback_symbol_strict_bool(sym))
return;
if (funcsym != NULL && (strcmp(sym->s_name, "__FUNCTION__") == 0 ||
strcmp(sym->s_name, "__PRETTY_FUNCTION__") == 0)) {
gnuism(316);
goto return_function_name;
}
if (funcsym != NULL && strcmp(sym->s_name, "__func__") == 0) {
if (!allow_c99)
warning(317);
return_function_name:
sym->s_type = block_derive_type(gettyp(CHAR), ARRAY);
sym->s_type->t_const = true;
sym->s_type->u.dimension = (int)strlen(funcsym->s_name) + 1;
return;
}
error(99, sym->s_name);
}
bool
is_compiler_builtin(const char *name)
{
if (allow_gcc) {
if (strncmp(name, "__atomic_", 9) == 0 ||
strncmp(name, "__builtin_", 10) == 0 ||
strcmp(name, "alloca") == 0 ||
strncmp(name, "__sync_", 7) == 0)
return true;
}
if (strncmp(name, "_mm_", 4) == 0)
return true;
if (strcmp(name, "__c11_atomic_init") == 0 ||
strcmp(name, "__c11_atomic_thread_fence") == 0 ||
strcmp(name, "__c11_atomic_signal_fence") == 0 ||
strcmp(name, "__c11_atomic_is_lock_free ") == 0 ||
strcmp(name, "__c11_atomic_store") == 0 ||
strcmp(name, "__c11_atomic_load") == 0 ||
strcmp(name, "__c11_atomic_exchange") == 0 ||
strcmp(name, "__c11_atomic_compare_exchange_strong") == 0 ||
strcmp(name, "__c11_atomic_compare_exchange_weak") == 0 ||
strcmp(name, "__c11_atomic_fetch_add") == 0 ||
strcmp(name, "__c11_atomic_fetch_sub") == 0 ||
strcmp(name, "__c11_atomic_fetch_and") == 0 ||
strcmp(name, "__c11_atomic_fetch_or") == 0 ||
strcmp(name, "__c11_atomic_fetch_xor") == 0 ||
strcmp(name, "__c11_atomic_fetch_nand ") == 0 ||
strcmp(name, "__c11_atomic_fetch_max") == 0 ||
strcmp(name, "__c11_atomic_fetch_min") == 0)
return true;
return false;
}
static bool
str_ends_with(const char *haystack, const char *needle)
{
size_t hlen = strlen(haystack);
size_t nlen = strlen(needle);
return nlen <= hlen &&
memcmp(haystack + hlen - nlen, needle, nlen) == 0;
}
static bool
is_gcc_bool_builtin(const char *name)
{
return strncmp(name, "__builtin_", 10) == 0 &&
(str_ends_with(name, "_overflow") ||
str_ends_with(name, "_overflow_p"));
}
static void
build_name_call(sym_t *sym)
{
if (is_compiler_builtin(sym->s_name)) {
if (allow_gcc && is_gcc_bool_builtin(sym->s_name))
sym->s_type = gettyp(BOOL);
} else if (allow_c99)
error(215, sym->s_name);
else if (!allow_trad)
warning(215, sym->s_name);
sym->s_type = block_derive_type(sym->s_type, FUNC);
}
tnode_t *
build_name(sym_t *sym, bool is_funcname)
{
if (sym->s_scl == NO_SCL && !in_gcc_attribute) {
sym->s_scl = EXTERN;
sym->s_def = DECL;
if (is_funcname)
build_name_call(sym);
else
fallback_symbol(sym);
}
lint_assert(sym->s_kind == SK_VCFT || sym->s_kind == SK_MEMBER);
tnode_t *n = expr_alloc_tnode();
n->tn_type = sym->s_type;
if (sym->s_scl == BOOL_CONST) {
n->tn_op = CON;
n->u.value.v_tspec = BOOL;
n->u.value.v_unsigned_since_c90 = false;
n->u.value.v_char_constant = false;
n->u.value.u.integer = sym->u.s_bool_constant ? 1 : 0;
} else if (sym->s_scl == ENUM_CONST) {
n->tn_op = CON;
n->u.value.v_tspec = INT;
n->u.value.v_unsigned_since_c90 = false;
n->u.value.v_char_constant = false;
n->u.value.u.integer = sym->u.s_enum_constant;
} else {
n->tn_op = NAME;
n->u.sym = sym;
if (sym->s_kind == SK_VCFT && sym->s_type->t_tspec != FUNC)
n->tn_lvalue = true;
}
return n;
}
tnode_t *
build_string(buffer *lit)
{
size_t value_len = lit->len;
if (lit->data != NULL) {
quoted_iterator it = { .end = 0 };
for (value_len = 0; quoted_next(lit, &it); value_len++)
continue;
}
type_t *tp = expr_zero_alloc(sizeof(*tp), "type");
tp->t_tspec = ARRAY;
tp->t_subt = gettyp(lit->data != NULL ? CHAR : WCHAR_TSPEC);
tp->u.dimension = (int)(value_len + 1);
tnode_t *n = expr_alloc_tnode();
n->tn_op = STRING;
n->tn_type = tp;
n->tn_lvalue = true;
n->u.str_literals = expr_zero_alloc(sizeof(*n->u.str_literals), "tnode.string");
n->u.str_literals->len = lit->len;
if (lit->data != NULL) {
n->u.str_literals->data = expr_zero_alloc(lit->len + 1,
"tnode.string.data");
(void)memcpy(n->u.str_literals->data, lit->data, lit->len + 1);
free(lit->data);
}
free(lit);
return n;
}
static bool
is_out_of_char_range(const tnode_t *tn)
{
return tn->tn_op == CON &&
!tn->u.value.v_char_constant &&
!(0 <= tn->u.value.u.integer &&
tn->u.value.u.integer < 1 << (CHAR_SIZE - 1));
}
static bool
check_nonportable_char_comparison(op_t op,
const tnode_t *ln, tspec_t lt,
const tnode_t *rn, tspec_t rt)
{
if (!(hflag || pflag))
return true;
if (lt == CHAR && is_out_of_char_range(rn)) {
char buf[128];
(void)snprintf(buf, sizeof(buf), "%s %d",
op_name(op), (int)rn->u.value.u.integer);
warning(230, buf);
return false;
}
if (rt == CHAR && is_out_of_char_range(ln)) {
char buf[128];
(void)snprintf(buf, sizeof(buf), "%d %s ?",
(int)ln->u.value.u.integer, op_name(op));
warning(230, buf);
return false;
}
return true;
}
static void
check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn)
{
tspec_t lt = ln->tn_type->t_tspec;
tspec_t rt = rn->tn_type->t_tspec;
if (ln->tn_op != CON && rn->tn_op != CON)
return;
if (!is_integer(lt) || !is_integer(rt))
return;
if (any_query_enabled && !in_system_header) {
if (lt == CHAR && rn->tn_op == CON &&
!rn->u.value.v_char_constant) {
query_message(14,
op_name(op), (int)rn->u.value.u.integer);
}
if (rt == CHAR && ln->tn_op == CON &&
!ln->u.value.v_char_constant) {
query_message(14,
op_name(op), (int)ln->u.value.u.integer);
}
}
if (!check_nonportable_char_comparison(op, ln, lt, rn, rt))
return;
if (is_uinteger(lt) && !is_uinteger(rt) &&
rn->tn_op == CON && rn->u.value.u.integer <= 0) {
if (rn->u.value.u.integer < 0) {
warning(162, op_name(op),
expr_type_name(ln), "negative constant");
} else if (op == LT || op == GE)
warning(162, op_name(op), expr_type_name(ln), "0");
return;
}
if (is_uinteger(rt) && !is_uinteger(lt) &&
ln->tn_op == CON && ln->u.value.u.integer <= 0) {
if (ln->u.value.u.integer < 0) {
warning(162, op_name(op),
"negative constant", expr_type_name(rn));
} else if (op == GT || op == LE)
warning(162, op_name(op), "0", expr_type_name(rn));
return;
}
}
static const tspec_t arith_rank[] = {
LDOUBLE, DOUBLE, FLOAT,
UINT128, INT128,
ULLONG, LLONG,
ULONG, LONG,
UINT, INT,
};
static tspec_t
usual_arithmetic_conversion_trad(tspec_t lt, tspec_t rt)
{
size_t i;
for (i = 0; arith_rank[i] != INT; i++)
if (lt == arith_rank[i] || rt == arith_rank[i])
break;
tspec_t t = arith_rank[i];
if (is_uinteger(lt) || is_uinteger(rt))
if (is_integer(t) && !is_uinteger(t))
return unsigned_type(t);
return t;
}
static tspec_t
usual_arithmetic_conversion_c90(tspec_t lt, tspec_t rt)
{
if (lt == rt)
return lt;
if (lt == LCOMPLEX || rt == LCOMPLEX)
return LCOMPLEX;
if (lt == DCOMPLEX || rt == DCOMPLEX)
return DCOMPLEX;
if (lt == FCOMPLEX || rt == FCOMPLEX)
return FCOMPLEX;
if (lt == LDOUBLE || rt == LDOUBLE)
return LDOUBLE;
if (lt == DOUBLE || rt == DOUBLE)
return DOUBLE;
if (lt == FLOAT || rt == FLOAT)
return FLOAT;
if (size_in_bits(lt) > size_in_bits(rt))
return lt;
if (size_in_bits(lt) < size_in_bits(rt))
return rt;
size_t i;
for (i = 3; arith_rank[i] != INT; i++)
if (arith_rank[i] == lt || arith_rank[i] == rt)
break;
if ((is_uinteger(lt) || is_uinteger(rt)) &&
!is_uinteger(arith_rank[i]))
i--;
return arith_rank[i];
}
static tnode_t *
apply_usual_arithmetic_conversions(op_t op, tnode_t *tn, tspec_t t)
{
type_t *ntp = expr_dup_type(tn->tn_type);
ntp->t_tspec = t;
if (tn->tn_op != CON) {
query_message(4, op_name(op),
expr_type_name(tn), type_name(ntp));
}
return convert(op, 0, ntp, tn);
}
static void
balance(op_t op, tnode_t **lnp, tnode_t **rnp)
{
tspec_t lt = (*lnp)->tn_type->t_tspec;
tspec_t rt = (*rnp)->tn_type->t_tspec;
if (!is_arithmetic(lt) || !is_arithmetic(rt))
return;
tspec_t t = allow_c90
? usual_arithmetic_conversion_c90(lt, rt)
: usual_arithmetic_conversion_trad(lt, rt);
if (modtab[op].m_comparison
&& is_integer(lt) && (*lnp)->tn_op != CON
&& is_floating(t) && (*rnp)->tn_op == CON)
warning(379, expr_type_name(*lnp),
(*rnp)->u.value.u.floating);
if (t != lt)
*lnp = apply_usual_arithmetic_conversions(op, *lnp, t);
if (t != rt)
*rnp = apply_usual_arithmetic_conversions(op, *rnp, t);
if (is_integer(t)) {
unsigned lw = width_in_bits((*lnp)->tn_type);
unsigned rw = width_in_bits((*rnp)->tn_type);
if (lw < rw)
*lnp = convert(NOOP, 0, (*rnp)->tn_type, *lnp);
if (rw < lw)
*rnp = convert(NOOP, 0, (*lnp)->tn_type, *rnp);
}
}
static tnode_t *
build_address(bool sys, tnode_t *tn)
{
if (tn->tn_op == INDIR &&
tn->u.ops.left->tn_type->t_tspec == PTR &&
tn->u.ops.left->tn_type->t_subt == tn->tn_type) {
return tn->u.ops.left;
}
return build_op(ADDR, sys, expr_derive_ptr_type(tn->tn_type),
tn, NULL);
}
static uint64_t
fold_unsigned_integer(op_t op, uint64_t l, uint64_t r,
uint64_t max_value, bool *overflow)
{
switch (op) {
case COMPL:
return ~l & max_value;
case UPLUS:
return +l;
case UMINUS:
return -l & max_value;
case MULT:
*overflow = r > 0 && l > max_value / r;
return l * r;
case DIV:
if (r == 0) {
if (is_evaluation_mode(EM_EVAL))
error(139);
return max_value;
}
return l / r;
case MOD:
if (r == 0) {
if (is_evaluation_mode(EM_EVAL))
error(140);
return 0;
}
return l % r;
case PLUS:
*overflow = l > max_value - r;
return l + r;
case MINUS:
*overflow = l < r;
return l - r;
case SHL:
return l << (r & 63);
case SHR:
return l >> (r & 63);
case LT:
return l < r ? 1 : 0;
case LE:
return l <= r ? 1 : 0;
case GT:
return l > r ? 1 : 0;
case GE:
return l >= r ? 1 : 0;
case EQ:
return l == r ? 1 : 0;
case NE:
return l != r ? 1 : 0;
case BITAND:
return l & r;
case BITXOR:
return l ^ r;
case BITOR:
return l | r;
default:
lint_assert(false);
}
}
static int64_t
fold_signed_integer(op_t op, int64_t l, int64_t r,
int64_t min_value, int64_t max_value, bool *overflow)
{
switch (op) {
case COMPL:
return ~l;
case UPLUS:
return +l;
case UMINUS:
*overflow = l == min_value;
return *overflow ? l : -l;
case MULT:;
uint64_t al = s64_abs(l);
uint64_t ar = s64_abs(r);
bool neg = (l >= 0) != (r >= 0);
uint64_t max_prod = (uint64_t)max_value + (neg ? 1 : 0);
if (al > 0 && ar > max_prod / al) {
*overflow = true;
return neg ? min_value : max_value;
}
return l * r;
case DIV:
if (r == 0) {
if (is_evaluation_mode(EM_EVAL))
error(139);
return max_value;
}
if (l == min_value && r == -1) {
*overflow = true;
return l;
}
return l / r;
case MOD:
if (r == 0) {
if (is_evaluation_mode(EM_EVAL))
error(140);
return 0;
}
if (l == min_value && r == -1) {
*overflow = true;
return 0;
}
return l % r;
case PLUS:
if (r > 0 && l > max_value - r) {
*overflow = true;
return max_value;
}
if (r < 0 && l < min_value - r) {
*overflow = true;
return min_value;
}
return l + r;
case MINUS:
if (r > 0 && l < min_value + r) {
*overflow = true;
return min_value;
}
if (r < 0 && l > max_value + r) {
*overflow = true;
return max_value;
}
return l - r;
case SHL:
return (int64_t)((uint64_t)l << (r & 63));
case SHR:
return s64_shr(l, r & 63);
case LT:
return l < r ? 1 : 0;
case LE:
return l <= r ? 1 : 0;
case GT:
return l > r ? 1 : 0;
case GE:
return l >= r ? 1 : 0;
case EQ:
return l == r ? 1 : 0;
case NE:
return l != r ? 1 : 0;
case BITAND:
return l & r;
case BITXOR:
return l ^ r;
case BITOR:
return l | r;
default:
lint_assert(false);
}
}
static tnode_t *
fold_constant_integer(tnode_t *tn)
{
lint_assert(has_operands(tn));
tspec_t t = tn->u.ops.left->tn_type->t_tspec;
int64_t l = tn->u.ops.left->u.value.u.integer;
int64_t r = is_binary(tn) ? tn->u.ops.right->u.value.u.integer : 0;
uint64_t mask = value_bits(size_in_bits(t));
int64_t res;
bool overflow = false;
if (!is_integer(t) || is_uinteger(t)) {
uint64_t u_res = fold_unsigned_integer(tn->tn_op,
(uint64_t)l, (uint64_t)r, mask, &overflow);
if (u_res > mask)
overflow = true;
res = (int64_t)u_res;
if (overflow && hflag) {
char buf[128];
if (is_binary(tn)) {
snprintf(buf, sizeof(buf), "%ju %s %ju",
(uintmax_t)l, op_name(tn->tn_op),
(uintmax_t)r);
} else {
snprintf(buf, sizeof(buf), "%s%ju",
op_name(tn->tn_op), (uintmax_t)l);
}
warning(141, buf, expr_type_name(tn));
}
} else {
int64_t max_value = (int64_t)(mask >> 1);
int64_t min_value = -max_value - 1;
res = fold_signed_integer(tn->tn_op,
l, r, min_value, max_value, &overflow);
if (res < min_value || res > max_value)
overflow = true;
if (overflow && hflag) {
char buf[128];
if (is_binary(tn)) {
snprintf(buf, sizeof(buf), "%jd %s %jd",
(intmax_t)l, op_name(tn->tn_op),
(intmax_t)r);
} else if (tn->tn_op == UMINUS && l < 0) {
snprintf(buf, sizeof(buf), "-(%jd)",
(intmax_t)l);
} else {
snprintf(buf, sizeof(buf), "%s%jd",
op_name(tn->tn_op), (intmax_t)l);
}
warning(141, buf, expr_type_name(tn));
}
}
val_t *v = xcalloc(1, sizeof(*v));
v->v_tspec = tn->tn_type->t_tspec;
v->u.integer = convert_integer(res, t, size_in_bits(t));
tnode_t *cn = build_constant(tn->tn_type, v);
if (tn->u.ops.left->tn_system_dependent)
cn->tn_system_dependent = true;
if (is_binary(tn) && tn->u.ops.right->tn_system_dependent)
cn->tn_system_dependent = true;
return cn;
}
static tnode_t *
build_struct_access(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
{
lint_assert(rn->tn_op == NAME);
lint_assert(is_member(rn->u.sym));
bool lvalue = op == ARROW || ln->tn_lvalue;
if (op == POINT)
ln = build_address(sys, ln);
else if (ln->tn_type->t_tspec != PTR) {
lint_assert(!allow_c90);
lint_assert(is_integer(ln->tn_type->t_tspec));
ln = convert(NOOP, 0, expr_derive_ptr_type(gettyp(VOID)), ln);
}
tnode_t *ctn = build_integer_constant(PTRDIFF_TSPEC,
rn->u.sym->u.s_member.sm_offset_in_bits / CHAR_SIZE);
type_t *ptr_tp = expr_derive_ptr_type(rn->tn_type);
tnode_t *ntn = build_op(PLUS, sys, ptr_tp, ln, ctn);
if (ln->tn_op == CON)
ntn = fold_constant_integer(ntn);
op_t nop = rn->tn_type->t_bitfield ? FSEL : INDIR;
ntn = build_op(nop, sys, ntn->tn_type->t_subt, ntn, NULL);
if (!lvalue)
ntn->tn_lvalue = false;
return ntn;
}
static bool
may_be_signed_min(const tnode_t *tn)
{
if (!is_integer(tn->tn_type->t_tspec))
return false;
if (is_uinteger(tn->tn_type->t_tspec))
return false;
integer_constraints c = ic_expr(tn);
return c.smin == si_min_value(tn->tn_type);
}
static tnode_t *
subt_size_in_bytes(type_t *tp)
{
lint_assert(tp->t_tspec == PTR);
tp = tp->t_subt;
int elem = 1;
while (tp->t_tspec == ARRAY) {
elem *= tp->u.dimension;
tp = tp->t_subt;
}
int elsz_in_bits = 0;
switch (tp->t_tspec) {
case FUNC:
error(110);
break;
case VOID:
gnuism(136);
break;
case STRUCT:
case UNION:
if ((elsz_in_bits = (int)tp->u.sou->sou_size_in_bits) == 0)
error(136);
break;
case ENUM:
if (is_incomplete(tp))
warning(136);
default:
if ((elsz_in_bits = size_in_bits(tp->t_tspec)) == 0)
error(136);
else
lint_assert(elsz_in_bits != -1);
break;
}
if (elem == 0 && elsz_in_bits != 0)
error(136);
if (elsz_in_bits == 0)
elsz_in_bits = CHAR_SIZE;
return build_integer_constant(PTRDIFF_TSPEC,
(int64_t)(elem * elsz_in_bits / CHAR_SIZE));
}
static tnode_t *
build_prepost_incdec(op_t op, bool sys, tnode_t *ln)
{
lint_assert(ln != NULL);
tnode_t *cn = ln->tn_type->t_tspec == PTR
? subt_size_in_bytes(ln->tn_type)
: build_integer_constant(INT, 1);
return build_op(op, sys, ln->tn_type, ln, cn);
}
static void
check_enum_array_index(const tnode_t *ln, const tnode_t *rn)
{
if (ln->tn_op != ADDR)
return;
ln = ln->u.ops.left;
if (ln->tn_op != NAME && ln->tn_op != STRING)
return;
const type_t *ltp = ln->tn_type;
if (ltp->t_tspec != ARRAY || ltp->t_incomplete_array)
return;
if (rn->tn_op != CVT || !rn->tn_type->t_is_enum)
return;
if (rn->u.ops.left->tn_op != LOAD)
return;
const type_t *rtp = rn->u.ops.left->tn_type;
const sym_t *ec = rtp->u.enumer->en_first_enumerator;
const sym_t *max_ec = ec;
lint_assert(ec != NULL);
for (ec = ec->s_next; ec != NULL; ec = ec->s_next)
if (ec->u.s_enum_constant > max_ec->u.s_enum_constant)
max_ec = ec;
int64_t max_enum_value = max_ec->u.s_enum_constant;
lint_assert(INT_MIN <= max_enum_value && max_enum_value <= INT_MAX);
int max_array_index = ltp->u.dimension - 1;
size_t nonnull_dimension = ln->tn_op == STRING
? str_len(ln)
: ln->u.sym->u.s_array_nonnull_dimension;
if (nonnull_dimension > 0)
max_array_index = (int)nonnull_dimension - 1;
if (max_enum_value == max_array_index)
return;
if (max_enum_value == max_array_index + 1 &&
(strstr(max_ec->s_name, "MAX") != NULL ||
strstr(max_ec->s_name, "max") != NULL ||
strstr(max_ec->s_name, "NUM") != NULL ||
strstr(max_ec->s_name, "num") != NULL ||
strncmp(max_ec->s_name, "COUNT_", 6) == 0 ||
str_ends_with(max_ec->s_name, "_COUNT") ||
strncmp(max_ec->s_name, "LAST_", 5) == 0 ||
str_ends_with(max_ec->s_name, "_end") ||
strncmp(max_ec->s_name, "N_", 2) == 0))
return;
warning(348, (int)max_enum_value, max_ec->s_name, type_name(rtp),
max_array_index);
print_previous_declaration(max_ec);
}
static tnode_t *
build_plus_minus(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
{
if (rn->tn_type->t_tspec == PTR && is_integer(ln->tn_type->t_tspec)) {
tnode_t *tmp = ln;
ln = rn;
rn = tmp;
query_message(5);
}
tspec_t lt = ln->tn_type->t_tspec;
tspec_t rt = rn->tn_type->t_tspec;
if (lt == PTR && rt != PTR) {
lint_assert(is_integer(rt));
check_ctype_macro_invocation(ln, rn);
check_enum_array_index(ln, rn);
tnode_t *elsz = subt_size_in_bytes(ln->tn_type);
tspec_t szt = elsz->tn_type->t_tspec;
if (rt != szt && rt != unsigned_type(szt))
rn = convert(NOOP, 0, elsz->tn_type, rn);
tnode_t *prod = build_op(MULT, sys, rn->tn_type, rn, elsz);
if (rn->tn_op == CON)
prod = fold_constant_integer(prod);
return build_op(op, sys, ln->tn_type, ln, prod);
}
if (rt == PTR) {
lint_assert(lt == PTR);
lint_assert(op == MINUS);
type_t *ptrdiff = gettyp(PTRDIFF_TSPEC);
tnode_t *raw_diff = build_op(op, sys, ptrdiff, ln, rn);
if (ln->tn_op == CON && rn->tn_op == CON)
raw_diff = fold_constant_integer(raw_diff);
tnode_t *elsz = subt_size_in_bytes(ln->tn_type);
balance(NOOP, &raw_diff, &elsz);
return build_op(DIV, sys, ptrdiff, raw_diff, elsz);
}
return build_op(op, sys, ln->tn_type, ln, rn);
}
static tnode_t *
build_bit_shift(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
{
if (!allow_c90 && rn->tn_type->t_tspec != INT)
rn = convert(NOOP, 0, gettyp(INT), rn);
return build_op(op, sys, ln->tn_type, ln, rn);
}
static bool
is_null_pointer(const tnode_t *tn)
{
tspec_t t = tn->tn_type->t_tspec;
return ((t == PTR && tn->tn_type->t_subt->t_tspec == VOID)
|| is_integer(t))
&& (tn->tn_op == CON && tn->u.value.u.integer == 0);
}
static type_t *
merge_qualifiers(type_t *tp1, const type_t *tp2)
{
lint_assert(tp1->t_tspec == PTR);
lint_assert(tp2->t_tspec == PTR);
bool c1 = tp1->t_subt->t_const;
bool c2 = tp2->t_subt->t_const;
bool v1 = tp1->t_subt->t_volatile;
bool v2 = tp2->t_subt->t_volatile;
if (c1 == (c1 | c2) && v1 == (v1 | v2))
return tp1;
type_t *nstp = expr_dup_type(tp1->t_subt);
nstp->t_const |= c2;
nstp->t_volatile |= v2;
type_t *ntp = expr_dup_type(tp1);
ntp->t_subt = nstp;
return ntp;
}
static tnode_t *
build_colon(bool sys, tnode_t *ln, tnode_t *rn)
{
tspec_t lt = ln->tn_type->t_tspec;
tspec_t rt = rn->tn_type->t_tspec;
type_t *tp;
if (is_arithmetic(lt) && is_arithmetic(rt))
tp = ln->tn_type;
else if (lt == BOOL && rt == BOOL)
tp = ln->tn_type;
else if (lt == VOID || rt == VOID)
tp = gettyp(VOID);
else if (is_struct_or_union(lt)) {
lint_assert(is_struct_or_union(rt));
lint_assert(ln->tn_type->u.sou == rn->tn_type->u.sou);
if (is_incomplete(ln->tn_type)) {
error(138, op_name(COLON));
return NULL;
}
tp = ln->tn_type;
} else if (lt == PTR && is_integer(rt)) {
if (rt != PTRDIFF_TSPEC)
rn = convert(NOOP, 0, gettyp(PTRDIFF_TSPEC), rn);
tp = ln->tn_type;
} else if (rt == PTR && is_integer(lt)) {
if (lt != PTRDIFF_TSPEC)
ln = convert(NOOP, 0, gettyp(PTRDIFF_TSPEC), ln);
tp = rn->tn_type;
} else if (lt == PTR && is_null_pointer(rn))
tp = merge_qualifiers(ln->tn_type, rn->tn_type);
else if (rt == PTR && is_null_pointer(ln))
tp = merge_qualifiers(rn->tn_type, ln->tn_type);
else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID)
tp = merge_qualifiers(ln->tn_type, rn->tn_type);
else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID)
tp = merge_qualifiers(rn->tn_type, ln->tn_type);
else {
tp = merge_qualifiers(ln->tn_type, rn->tn_type);
}
return build_op(COLON, sys, tp, ln, rn);
}
static bool
is_cast_redundant(const tnode_t *tn)
{
const type_t *ntp = tn->tn_type, *otp = tn->u.ops.left->tn_type;
tspec_t nt = ntp->t_tspec, ot = otp->t_tspec;
if (nt == BOOL || ot == BOOL)
return nt == BOOL && ot == BOOL;
if (is_integer(nt) && is_integer(ot)) {
unsigned int nw = width_in_bits(ntp), ow = width_in_bits(otp);
if (is_uinteger(nt) == is_uinteger(ot))
return nw >= ow;
return is_uinteger(ot) && nw > ow;
}
if (is_complex(nt) || is_complex(ot))
return is_complex(nt) && is_complex(ot) &&
size_in_bits(nt) >= size_in_bits(ot);
if (is_floating(nt) && is_floating(ot))
return size_in_bits(nt) >= size_in_bits(ot);
if (nt == PTR && ot == PTR) {
if (!ntp->t_subt->t_const && otp->t_subt->t_const)
return false;
if (!ntp->t_subt->t_volatile && otp->t_subt->t_volatile)
return false;
if (ntp->t_subt->t_tspec == VOID ||
otp->t_subt->t_tspec == VOID ||
types_compatible(ntp->t_subt, otp->t_subt,
false, false, NULL))
return true;
}
return false;
}
static bool
is_assignment(op_t op)
{
return op == ASSIGN ||
op == MULASS ||
op == DIVASS ||
op == MODASS ||
op == ADDASS ||
op == SUBASS ||
op == SHLASS ||
op == SHRASS ||
op == ANDASS ||
op == XORASS ||
op == ORASS ||
op == RETURN ||
op == INIT;
}
static tnode_t *
build_assignment(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
{
tspec_t lt = ln->tn_type->t_tspec;
tspec_t rt = rn->tn_type->t_tspec;
if (is_assignment(rn->tn_op))
query_message(10, op_name(op), op_name(rn->tn_op));
if ((op == ADDASS || op == SUBASS) && lt == PTR) {
lint_assert(is_integer(rt));
tnode_t *ctn = subt_size_in_bytes(ln->tn_type);
if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
rn = convert(NOOP, 0, ctn->tn_type, rn);
rn = build_op(MULT, sys, rn->tn_type, rn, ctn);
if (rn->u.ops.left->tn_op == CON)
rn = fold_constant_integer(rn);
}
if ((op == ASSIGN || op == RETURN || op == INIT) &&
(lt == STRUCT || rt == STRUCT)) {
lint_assert(lt == rt);
lint_assert(ln->tn_type->u.sou == rn->tn_type->u.sou);
if (is_incomplete(ln->tn_type)) {
if (op == RETURN)
error(212);
else
error(138, op_name(op));
return NULL;
}
}
if (op == SHLASS && hflag && allow_trad && allow_c90
&& portable_rank_cmp(lt, rt) < 0)
warning(118, tspec_name(lt), "<<=", tspec_name(rt));
if (op != SHLASS && op != SHRASS
&& (op == ASSIGN || lt != PTR)
&& (lt != rt || (ln->tn_type->t_bitfield && rn->tn_op == CON))) {
rn = convert(op, 0, ln->tn_type, rn);
rt = lt;
}
if (lt == PTR && ln->tn_type->t_subt->t_tspec != VOID
&& rt == PTR && rn->tn_type->t_subt->t_tspec == VOID
&& !is_null_pointer(rn))
query_message(20, type_name(ln->tn_type));
if (any_query_enabled && rn->tn_op == CVT && rn->tn_cast &&
types_compatible(ln->tn_type, rn->tn_type, false, false, NULL) &&
is_cast_redundant(rn)) {
query_message(7, expr_type_name(rn->u.ops.left),
type_name(rn->tn_type));
}
return build_op(op, sys, ln->tn_type, ln, rn);
}
static tnode_t *
build_real_imag(op_t op, bool sys, tnode_t *ln)
{
lint_assert(ln != NULL);
if (ln->tn_op == NAME) {
mark_as_used(ln->u.sym, false, false);
mark_as_set(ln->u.sym);
}
tspec_t t;
switch (ln->tn_type->t_tspec) {
case LCOMPLEX:
t = LDOUBLE;
break;
case DCOMPLEX:
t = DOUBLE;
break;
case FCOMPLEX:
t = FLOAT;
break;
default:
error(276, op == REAL ? "real" : "imag",
type_name(ln->tn_type));
return NULL;
}
tnode_t *ntn = build_op(op, sys, gettyp(t), ln, NULL);
ntn->tn_lvalue = true;
return ntn;
}
static bool
is_confusing_precedence(op_t op, const tnode_t *operand, op_t *cop)
{
if (operand->tn_parenthesized)
return false;
op_t oop = operand->tn_op;
if (op == SHL || op == SHR) {
if (oop == PLUS || oop == MINUS)
return *cop = oop, true;
return false;
}
if (op == LOGOR) {
if (oop == LOGAND)
return *cop = oop, true;
return false;
}
lint_assert(op == BITAND || op == BITXOR || op == BITOR);
if (oop != op
&& (oop == PLUS || oop == MINUS || oop == BITAND || oop == BITXOR))
return *cop = oop, true;
return false;
}
static void
check_precedence_confusion(tnode_t *tn)
{
tnode_t *ln, *rn;
if (!hflag)
return;
debug_node(tn);
lint_assert(is_binary(tn));
for (ln = tn->u.ops.left; ln->tn_op == CVT; ln = ln->u.ops.left)
continue;
for (rn = tn->u.ops.right; rn->tn_op == CVT; rn = rn->u.ops.left)
continue;
op_t cop;
if (is_confusing_precedence(tn->tn_op, ln, &cop) ||
is_confusing_precedence(tn->tn_op, rn, &cop)) {
warning(169, op_name(tn->tn_op), op_name(cop));
}
}
static tnode_t *
fold_constant_compare_zero(tnode_t *tn)
{
val_t *v = xcalloc(1, sizeof(*v));
v->v_tspec = tn->tn_type->t_tspec;
lint_assert(v->v_tspec == INT || (Tflag && v->v_tspec == BOOL));
lint_assert(has_operands(tn));
bool l = constant_is_nonzero(tn->u.ops.left);
bool r = is_binary(tn) && constant_is_nonzero(tn->u.ops.right);
switch (tn->tn_op) {
case NOT:
v->u.integer = !l ? 1 : 0;
break;
case LOGAND:
v->u.integer = l && r ? 1 : 0;
break;
case LOGOR:
v->u.integer = l || r ? 1 : 0;
break;
default:
lint_assert(false);
}
return build_constant(tn->tn_type, v);
}
static long double
floating_error_value(tspec_t t, long double lv)
{
if (t == FLOAT)
return lv < 0 ? -FLT_MAX : FLT_MAX;
if (t == DOUBLE)
return lv < 0 ? -DBL_MAX : DBL_MAX;
long double max = LDBL_MAX;
return lv < 0 ? -max : max;
}
static bool
is_floating_overflow(tspec_t t, long double val)
{
if (fpe != 0 || isfinite(val) == 0)
return true;
if (t == FLOAT && (val > FLT_MAX || val < -FLT_MAX))
return true;
if (t == DOUBLE && (val > DBL_MAX || val < -DBL_MAX))
return true;
return false;
}
static tnode_t *
fold_constant_floating(tnode_t *tn)
{
fpe = 0;
tspec_t t = tn->tn_type->t_tspec;
val_t *v = xcalloc(1, sizeof(*v));
v->v_tspec = t;
lint_assert(is_floating(t));
lint_assert(has_operands(tn));
lint_assert(t == tn->u.ops.left->tn_type->t_tspec);
lint_assert(!is_binary(tn) || t == tn->u.ops.right->tn_type->t_tspec);
long double lv = tn->u.ops.left->u.value.u.floating;
long double rv = is_binary(tn) ? tn->u.ops.right->u.value.u.floating
: 0.0;
switch (tn->tn_op) {
case UPLUS:
v->u.floating = lv;
break;
case UMINUS:
v->u.floating = -lv;
break;
case MULT:
v->u.floating = lv * rv;
break;
case DIV:
if (rv == 0.0) {
if (is_evaluation_mode(EM_EVAL))
error(139);
v->u.floating = floating_error_value(t, lv);
} else {
v->u.floating = lv / rv;
}
break;
case PLUS:
v->u.floating = lv + rv;
break;
case MINUS:
v->u.floating = lv - rv;
break;
case LT:
v->u.integer = lv < rv ? 1 : 0;
break;
case LE:
v->u.integer = lv <= rv ? 1 : 0;
break;
case GE:
v->u.integer = lv >= rv ? 1 : 0;
break;
case GT:
v->u.integer = lv > rv ? 1 : 0;
break;
case EQ:
v->u.integer = lv == rv ? 1 : 0;
break;
case NE:
v->u.integer = lv != rv ? 1 : 0;
break;
default:
lint_assert(false);
}
lint_assert(fpe != 0 || isnan(v->u.floating) == 0);
if (is_complex(v->v_tspec)) {
fpe = 0;
} else if (is_floating_overflow(t, v->u.floating)) {
warning(142, op_name(tn->tn_op));
v->u.floating = floating_error_value(t, v->u.floating);
fpe = 0;
}
return build_constant(tn->tn_type, v);
}
static void
use(const tnode_t *tn)
{
if (tn == NULL)
return;
switch (tn->tn_op) {
case NAME:
mark_as_used(tn->u.sym, false , false );
break;
case CON:
case STRING:
break;
case CALL:;
const function_call *call = tn->u.call;
for (size_t i = 0, n = call->args_len; i < n; i++)
use(call->args[i]);
break;
default:
lint_assert(has_operands(tn));
use(tn->u.ops.left);
if (is_binary(tn))
use(tn->u.ops.right);
}
}
tnode_t *
build_binary(tnode_t *ln, op_t op, bool sys, tnode_t *rn)
{
const mod_t *mp = &modtab[op];
if (ln == NULL || (mp->m_binary && rn == NULL))
return NULL;
if (mp->m_value_context || mp->m_compares_with_zero)
ln = cconv(ln);
if (mp->m_binary && op != ARROW && op != POINT)
rn = cconv(rn);
if (mp->m_comparison)
check_integer_comparison(op, ln, rn);
if (mp->m_value_context || mp->m_compares_with_zero)
ln = promote(op, false, ln);
if (mp->m_binary && op != ARROW && op != POINT &&
op != ASSIGN && op != RETURN && op != INIT)
rn = promote(op, false, rn);
if (mp->m_warn_if_left_unsigned_in_c90 &&
ln->tn_op == CON && ln->u.value.v_unsigned_since_c90) {
warning(218, op_name(op));
ln->u.value.v_unsigned_since_c90 = false;
}
if (mp->m_warn_if_right_unsigned_in_c90 &&
rn->tn_op == CON && rn->u.value.v_unsigned_since_c90) {
warning(218, op_name(op));
rn->u.value.v_unsigned_since_c90 = false;
}
if (mp->m_balance_operands || (!allow_c90 && (op == SHL || op == SHR)))
balance(op, &ln, &rn);
if (!typeok(op, NULL, 0, ln, rn))
return NULL;
tnode_t *ntn;
switch (op) {
case POINT:
case ARROW:
ntn = build_struct_access(op, sys, ln, rn);
break;
case UMINUS:
if (any_query_enabled && may_be_signed_min(ln))
query_message(25, expr_type_name(ln));
ntn = build_op(op, sys, ln->tn_type, ln, rn);
break;
case INCAFT:
case DECAFT:
case INCBEF:
case DECBEF:
ntn = build_prepost_incdec(op, sys, ln);
break;
case ADDR:
ntn = build_address(sys, ln);
break;
case INDIR:
ntn = build_op(INDIR, sys, ln->tn_type->t_subt, ln, NULL);
break;
case PLUS:
case MINUS:
ntn = build_plus_minus(op, sys, ln, rn);
break;
case SHL:
case SHR:
ntn = build_bit_shift(op, sys, ln, rn);
break;
case COLON:
ntn = build_colon(sys, ln, rn);
break;
case ASSIGN:
case MULASS:
case DIVASS:
case MODASS:
case ADDASS:
case SUBASS:
case SHLASS:
case SHRASS:
case ANDASS:
case XORASS:
case ORASS:
case RETURN:
case INIT:
ntn = build_assignment(op, sys, ln, rn);
break;
case COMMA:
query_message(12,
expr_type_name(ln), expr_type_name(rn));
case QUEST:
ntn = build_op(op, sys, rn->tn_type, ln, rn);
break;
case REAL:
case IMAG:
ntn = build_real_imag(op, sys, ln);
break;
default:
lint_assert(mp->m_binary == (rn != NULL));
if ((op == NOT || op == LOGAND || op == LOGOR)
&& ln->tn_op == ASSIGN && ln->u.ops.right->tn_op == CON) {
warning(382, expr_type_name(ln), op_name(op),
is_nonzero_val(&ln->u.ops.right->u.value)
? "true" : "false");
}
if ((op == LOGAND || op == LOGOR)
&& rn->tn_op == ASSIGN && rn->u.ops.right->tn_op == CON) {
warning(382, expr_type_name(rn), op_name(op),
is_nonzero_val(&rn->u.ops.right->u.value)
? "true" : "false");
}
type_t *rettp = mp->m_returns_bool
? gettyp(Tflag ? BOOL : INT) : ln->tn_type;
ntn = build_op(op, sys, rettp, ln, rn);
break;
}
if (ntn == NULL)
return NULL;
if (mp->m_possible_precedence_confusion)
check_precedence_confusion(ntn);
if (mp->m_fold_constant_operands && ln->tn_op == CON) {
if (!mp->m_binary || rn->tn_op == CON) {
if (mp->m_compares_with_zero)
ntn = fold_constant_compare_zero(ntn);
else if (is_floating(ntn->tn_type->t_tspec))
ntn = fold_constant_floating(ntn);
else
ntn = fold_constant_integer(ntn);
} else if (op == QUEST) {
lint_assert(has_operands(rn));
use(ln->u.value.u.integer != 0
? rn->u.ops.right : rn->u.ops.left);
ntn = ln->u.value.u.integer != 0
? rn->u.ops.left : rn->u.ops.right;
}
}
return ntn;
}
tnode_t *
build_unary(op_t op, bool sys, tnode_t *tn)
{
return build_binary(tn, op, sys, NULL);
}
static bool
are_members_compatible(const sym_t *a, const sym_t *b)
{
if (a->u.s_member.sm_offset_in_bits != b->u.s_member.sm_offset_in_bits)
return false;
const type_t *atp = a->s_type;
const type_t *btp = b->s_type;
bool w = false;
if (!types_compatible(atp, btp, false, false, &w) && !w)
return false;
if (a->s_bitfield != b->s_bitfield)
return false;
if (a->s_bitfield) {
if (atp->t_bit_field_width != btp->t_bit_field_width)
return false;
if (atp->t_bit_field_offset != btp->t_bit_field_offset)
return false;
}
return true;
}
static bool
all_members_compatible(const sym_t *msym)
{
for (const sym_t *csym = msym;
csym != NULL; csym = csym->s_symtab_next) {
if (!is_member(csym))
continue;
if (strcmp(msym->s_name, csym->s_name) != 0)
continue;
for (const sym_t *sym = csym->s_symtab_next;
sym != NULL; sym = sym->s_symtab_next) {
if (is_member(sym)
&& strcmp(csym->s_name, sym->s_name) == 0
&& !are_members_compatible(csym, sym))
return false;
}
}
return true;
}
sym_t *
find_member(const struct_or_union *sou, const char *name)
{
for (sym_t *mem = sou->sou_first_member;
mem != NULL; mem = mem->s_next) {
lint_assert(is_member(mem));
lint_assert(mem->u.s_member.sm_containing_type == sou);
if (strcmp(mem->s_name, name) == 0)
return mem;
}
for (sym_t *mem = sou->sou_first_member;
mem != NULL; mem = mem->s_next) {
if (is_struct_or_union(mem->s_type->t_tspec)
&& mem->s_name == unnamed) {
sym_t *nested_mem =
find_member(mem->s_type->u.sou, name);
if (nested_mem != NULL)
return nested_mem;
}
}
return NULL;
}
static void
remove_unknown_member(tnode_t *tn, sym_t *msym)
{
error(101, type_name(tn->tn_type), msym->s_name);
symtab_remove_forever(msym);
msym->s_kind = SK_MEMBER;
msym->s_scl = STRUCT_MEMBER;
struct_or_union *sou = expr_zero_alloc(sizeof(*sou),
"struct_or_union");
sou->sou_tag = expr_zero_alloc(sizeof(*sou->sou_tag), "sym");
sou->sou_tag->s_name = unnamed;
msym->u.s_member.sm_containing_type = sou;
}
static sym_t *
struct_or_union_member(tnode_t *tn, op_t op, sym_t *msym)
{
const type_t *tp = NULL;
if (op == POINT && is_struct_or_union(tn->tn_type->t_tspec))
tp = tn->tn_type;
if (op == ARROW && tn->tn_type->t_tspec == PTR
&& is_struct_or_union(tn->tn_type->t_subt->t_tspec))
tp = tn->tn_type->t_subt;
struct_or_union *sou = tp != NULL ? tp->u.sou : NULL;
if (sou != NULL) {
sym_t *nested_mem = find_member(sou, msym->s_name);
if (nested_mem != NULL)
return nested_mem;
}
if (msym->s_scl == NO_SCL) {
remove_unknown_member(tn, msym);
return msym;
}
bool eq = all_members_compatible(msym);
if (sou != NULL) {
if (eq && !allow_c90)
warning(102, msym->s_name);
else
error(102, msym->s_name);
return msym;
}
if (eq) {
if (op == POINT) {
if (!allow_c90)
warning(103, expr_type_name(tn));
else
error(103, expr_type_name(tn));
} else {
if (!allow_c90 && tn->tn_type->t_tspec == PTR)
warning(104, expr_type_name(tn));
else
error(104, expr_type_name(tn));
}
} else {
if (!allow_c90)
error(105, op == POINT ? "object" : "pointer");
else
error(111, op_name(op));
}
return msym;
}
tnode_t *
build_member_access(tnode_t *ln, op_t op, bool sys, sbuf_t *member)
{
sym_t *msym;
if (ln == NULL)
return NULL;
if (op == ARROW)
ln = cconv(ln);
msym = struct_or_union_member(ln, op, getsym(member));
return build_binary(ln, op, sys, build_name(msym, false));
}
tnode_t *
cconv(tnode_t *tn)
{
if (tn->tn_type->t_tspec == ARRAY) {
if (!tn->tn_lvalue) {
gnuism(114, "", op_name(ADDR));
}
tn = build_op(ADDR, tn->tn_sys,
expr_derive_ptr_type(tn->tn_type->t_subt), tn, NULL);
}
if (tn->tn_type->t_tspec == FUNC)
tn = build_address(tn->tn_sys, tn);
if (tn->tn_lvalue) {
type_t *tp = expr_dup_type(tn->tn_type);
tp->t_const = tp->t_volatile = false;
tn = build_op(LOAD, tn->tn_sys, tp, tn, NULL);
}
return tn;
}
const tnode_t *
before_conversion(const tnode_t *tn)
{
while (tn->tn_op == CVT && !tn->tn_cast)
tn = tn->u.ops.left;
return tn;
}
static bool
typeok_point(const tnode_t *ln, const type_t *ltp, tspec_t lt)
{
if (is_struct_or_union(lt))
return true;
if (lt == FUNC || lt == VOID || ltp->t_bitfield)
goto wrong;
if (ln->tn_lvalue)
return true;
wrong:
if (!allow_c90)
error(111, op_name(POINT));
return false;
}
static bool
typeok_arrow(tspec_t lt)
{
if (lt == PTR || (!allow_c90 && is_integer(lt)))
return true;
if (!allow_c90)
error(111, op_name(ARROW));
return false;
}
static bool
typeok_incdec(op_t op, const tnode_t *tn, const type_t *tp)
{
if (!tn->tn_lvalue) {
if (tn->tn_op == CVT && tn->tn_cast &&
tn->u.ops.left->tn_op == LOAD)
error(163);
error(114, "", op_name(op));
return false;
}
if (tp->t_const && allow_c90)
warning(115, "", op_name(op));
return true;
}
static bool
typeok_address(op_t op, const tnode_t *tn, const type_t *tp, tspec_t t)
{
if (t == ARRAY || t == FUNC) {
} else if (!tn->tn_lvalue) {
if (tn->tn_op == CVT && tn->tn_cast &&
tn->u.ops.left->tn_op == LOAD)
error(163);
error(114, "", op_name(op));
return false;
} else if (is_scalar(t)) {
if (tp->t_bitfield) {
error(112);
return false;
}
} else if (t != STRUCT && t != UNION) {
error(111, op_name(op));
return false;
}
if (tn->tn_op == NAME && tn->u.sym->s_register) {
error(113, tn->u.sym->s_name);
return false;
}
return true;
}
static bool
typeok_indir(const type_t *tp, tspec_t t)
{
if (t != PTR) {
error(96, type_name(tp));
return false;
}
return true;
}
static void
warn_incompatible_types(op_t op,
const type_t *ltp, tspec_t lt,
const type_t *rtp, tspec_t rt)
{
bool binary = modtab[op].m_binary;
if (lt == VOID || (binary && rt == VOID)) {
error(109);
} else if (op == ASSIGN)
error(171, type_name(ltp), type_name(rtp));
else if (binary)
error(107, op_name(op), type_name(ltp), type_name(rtp));
else {
lint_assert(rt == NO_TSPEC);
error(108, op_name(op), type_name(ltp));
}
}
static bool
typeok_plus(op_t op,
const type_t *ltp, tspec_t lt,
const type_t *rtp, tspec_t rt)
{
if ((lt == PTR && !is_integer(rt)) || (rt == PTR && !is_integer(lt))) {
warn_incompatible_types(op, ltp, lt, rtp, rt);
return false;
}
return true;
}
static bool
typeok_minus(op_t op,
const type_t *ltp, tspec_t lt,
const type_t *rtp, tspec_t rt)
{
if ((lt == PTR && rt != PTR && !is_integer(rt)) ||
(lt != PTR && rt == PTR)) {
warn_incompatible_types(op, ltp, lt, rtp, rt);
return false;
}
if (lt == PTR && rt == PTR &&
!types_compatible(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
error(116);
}
return true;
}
static void
typeok_shr(const tnode_t *ln, tspec_t lt,
const tnode_t *rn, tspec_t rt)
{
tspec_t olt = before_conversion(ln)->tn_type->t_tspec;
tspec_t ort = before_conversion(rn)->tn_type->t_tspec;
if (is_uinteger(olt))
return;
if (pflag) {
integer_constraints lc = ic_expr(ln);
if (lc.bclr >> 63 != 0)
return;
if (ln->tn_op != CON)
warning(117, ">>", expr_type_name(ln));
else if (ln->u.value.u.integer < 0)
warning(120, ">>", expr_type_name(ln));
return;
}
if (allow_trad && allow_c90 && hflag && is_uinteger(ort)
&& !(ln->tn_op == CON && ln->u.value.u.integer >= 0))
warning(118, tspec_name(lt), ">>", tspec_name(rt));
if (allow_trad && allow_c90 && hflag && !is_uinteger(ort)
&& portable_rank_cmp(lt, rt) < 0
&& !(ln->tn_op == CON && ln->u.value.u.integer >= 0))
warning(118, tspec_name(lt), ">>", tspec_name(rt));
}
static void
typeok_shl_signed_to_msb(const tnode_t *ln, const tnode_t *rn)
{
integer_constraints lc = ic_expr(ln);
int64_t n;
unsigned lw = width_in_bits(ln->tn_type);
if (!is_uinteger(ln->tn_type->t_tspec)
&& ln->tn_op != CON
&& ((lc.smin == 0 && lc.smax != 0 && lc.smax != INT64_MAX
&& (lc.smax & (lc.smax + 1)) == 0)
|| (lc.smin != INT64_MAX && lc.smax != INT64_MIN
&& lc.smin + 1 == -lc.smax))
&& rn->tn_op == CON
&& (n = rn->u.value.u.integer, 1 <= n && n <= lw)
&& u64_width((uint64_t)lc.smax - (uint64_t)lc.smin) + n == lw)
warning(117, "<<", expr_type_name(ln));
}
static void
typeok_shl(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
{
if (hflag && allow_trad && allow_c90 && portable_rank_cmp(lt, rt) < 0)
warning(118, tspec_name(lt), "<<", tspec_name(rt));
typeok_shl_signed_to_msb(ln, rn);
}
static void
typeok_shift(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
{
if (rn->tn_op != CON)
return;
if (!is_uinteger(rt) && rn->u.value.u.integer < 0)
warning(121);
else if ((uint64_t)rn->u.value.u.integer == size_in_bits(lt))
warning(267,
(unsigned)rn->u.value.u.integer, expr_type_name(ln));
else if ((uint64_t)rn->u.value.u.integer > size_in_bits(lt)) {
warning(122, (unsigned long long)rn->u.value.u.integer,
(unsigned long long)size_in_bits(lt),
expr_type_name(ln));
}
}
static bool
is_typeok_eq(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
{
if (lt == PTR && is_null_pointer(rn))
return true;
if (rt == PTR && is_null_pointer(ln))
return true;
return false;
}
static void
warn_incompatible_pointers(op_t op, const type_t *ltp, const type_t *rtp)
{
lint_assert(ltp->t_tspec == PTR);
lint_assert(rtp->t_tspec == PTR);
tspec_t lt = ltp->t_subt->t_tspec;
tspec_t rt = rtp->t_subt->t_tspec;
if (is_struct_or_union(lt) && is_struct_or_union(rt)) {
if (op == RETURN)
warning(244);
else {
warning(245, type_name(ltp),
op_name(op), type_name(rtp));
}
} else {
if (op == RETURN)
warning(184, type_name(ltp), type_name(rtp));
else {
warning(124,
type_name(ltp), type_name(rtp), op_name(op));
}
}
}
static void
check_pointer_comparison(op_t op, const tnode_t *ln, const tnode_t *rn)
{
type_t *ltp = ln->tn_type, *rtp = rn->tn_type;
tspec_t lst = ltp->t_subt->t_tspec, rst = rtp->t_subt->t_tspec;
if (lst == VOID || rst == VOID) {
if (!allow_trad && !allow_c99 &&
(lst == FUNC || rst == FUNC)) {
const char *lsts, *rsts;
*(lst == FUNC ? &lsts : &rsts) = "function pointer";
*(lst == VOID ? &lsts : &rsts) = "'void *'";
warning(274, lsts, rsts);
}
return;
}
if (!types_compatible(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
warn_incompatible_pointers(op, ltp, rtp);
return;
}
if (lst == FUNC && rst == FUNC) {
if (!allow_trad && !allow_c99 && op != EQ && op != NE)
warning(125);
}
}
static bool
typeok_compare(op_t op,
const tnode_t *ln, const type_t *ltp, tspec_t lt,
const tnode_t *rn, const type_t *rtp, tspec_t rt)
{
if (lt == PTR && rt == PTR) {
check_pointer_comparison(op, ln, rn);
return true;
}
if (lt != PTR && rt != PTR)
return true;
if (!is_integer(lt) && !is_integer(rt)) {
warn_incompatible_types(op, ltp, lt, rtp, rt);
return false;
}
const char *lx = lt == PTR ? "pointer" : "integer";
const char *rx = rt == PTR ? "pointer" : "integer";
warning(123, lx, type_name(ltp), rx, type_name(rtp), op_name(op));
return true;
}
static bool
typeok_quest(tspec_t lt, const tnode_t *rn)
{
if (!is_scalar(lt)) {
error(170);
return false;
}
lint_assert(before_conversion(rn)->tn_op == COLON);
return true;
}
static void
typeok_colon_pointer(const type_t *ltp, const type_t *rtp)
{
type_t *lstp = ltp->t_subt;
type_t *rstp = rtp->t_subt;
tspec_t lst = lstp->t_tspec;
tspec_t rst = rstp->t_tspec;
if ((lst == VOID && rst == FUNC) || (lst == FUNC && rst == VOID)) {
if (!allow_trad && !allow_c99)
warning(305, "function pointer", "'void *'",
op_name(COLON));
return;
}
if (pointer_types_are_compatible(lstp, rstp, true))
return;
if (!types_compatible(lstp, rstp, true, false, NULL))
warn_incompatible_pointers(COLON, ltp, rtp);
}
static bool
typeok_colon(const tnode_t *ln, const type_t *ltp, tspec_t lt,
const tnode_t *rn, const type_t *rtp, tspec_t rt)
{
if (is_arithmetic(lt) && is_arithmetic(rt))
return true;
if (lt == BOOL && rt == BOOL)
return true;
if (lt == STRUCT && rt == STRUCT && ltp->u.sou == rtp->u.sou)
return true;
if (lt == UNION && rt == UNION && ltp->u.sou == rtp->u.sou)
return true;
if (lt == PTR && is_null_pointer(rn))
return true;
if (rt == PTR && is_null_pointer(ln))
return true;
if ((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)) {
const char *lx = lt == PTR ? "pointer" : "integer";
const char *rx = rt == PTR ? "pointer" : "integer";
warning(123, lx, type_name(ltp),
rx, type_name(rtp), op_name(COLON));
return true;
}
if (lt == VOID || rt == VOID) {
if (lt != VOID || rt != VOID)
warning(126, expr_type_name(ln), expr_type_name(rn));
return true;
}
if (lt == PTR && rt == PTR) {
typeok_colon_pointer(ltp, rtp);
return true;
}
error(126, expr_type_name(ln), expr_type_name(rn));
return false;
}
static bool
has_constant_member(const type_t *tp)
{
lint_assert(is_struct_or_union(tp->t_tspec));
for (sym_t *m = tp->u.sou->sou_first_member;
m != NULL; m = m->s_next) {
const type_t *mtp = m->s_type;
if (mtp->t_const)
return true;
if (is_struct_or_union(mtp->t_tspec) &&
has_constant_member(mtp))
return true;
}
return false;
}
static bool
typeok_assign(op_t op, const tnode_t *ln, const type_t *ltp, tspec_t lt)
{
if (op == RETURN || op == INIT || op == FARG)
return true;
if (!ln->tn_lvalue) {
if (ln->tn_op == CVT && ln->tn_cast &&
ln->u.ops.left->tn_op == LOAD)
error(163);
error(114, "left ", op_name(op));
return false;
} else if (ltp->t_const
|| (is_struct_or_union(lt) && has_constant_member(ltp))) {
if (allow_c90)
warning(115, "left ", op_name(op));
}
return true;
}
static bool
typeok_scalar(op_t op, const mod_t *mp,
const type_t *ltp, tspec_t lt,
const type_t *rtp, tspec_t rt)
{
if (mp->m_takes_bool && lt == BOOL && rt == BOOL)
return true;
if (mp->m_requires_integer) {
if (!is_integer(lt) || (mp->m_binary && !is_integer(rt))) {
warn_incompatible_types(op, ltp, lt, rtp, rt);
return false;
}
} else if (mp->m_requires_integer_or_complex) {
if ((!is_integer(lt) && !is_complex(lt)) ||
(mp->m_binary && (!is_integer(rt) && !is_complex(rt)))) {
warn_incompatible_types(op, ltp, lt, rtp, rt);
return false;
}
} else if (mp->m_requires_scalar) {
if (!is_scalar(lt) || (mp->m_binary && !is_scalar(rt))) {
warn_incompatible_types(op, ltp, lt, rtp, rt);
return false;
}
} else if (mp->m_requires_arith) {
if (!is_arithmetic(lt) ||
(mp->m_binary && !is_arithmetic(rt))) {
warn_incompatible_types(op, ltp, lt, rtp, rt);
return false;
}
}
return true;
}
static void
check_assign_void_pointer(op_t op, int arg,
tspec_t lt, tspec_t lst,
tspec_t rt, tspec_t rst)
{
if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID)))
return;
if (!(!allow_trad && !allow_c99 && (lst == FUNC || rst == FUNC)))
return;
const char *lts, *rts;
*(lst == FUNC ? <s : &rts) = "function pointer";
*(lst == VOID ? <s : &rts) = "'void *'";
switch (op) {
case INIT:
case RETURN:
warning(303, rts, lts);
break;
case FARG:
warning(304, rts, lts, arg);
break;
default:
warning(305, rts, lts, op_name(op));
break;
}
}
static bool
is_direct_function_call(const tnode_t *tn, const char **out_name)
{
if (tn->tn_op == CALL
&& tn->u.call->func->tn_op == ADDR
&& tn->u.call->func->u.ops.left->tn_op == NAME) {
*out_name = tn->u.call->func->u.ops.left->u.sym->s_name;
return true;
}
return false;
}
static bool
is_unconst_function(const char *name)
{
return strcmp(name, "memchr") == 0 ||
strcmp(name, "strchr") == 0 ||
strcmp(name, "strpbrk") == 0 ||
strcmp(name, "strrchr") == 0 ||
strcmp(name, "strstr") == 0;
}
static bool
is_const_char_pointer(const tnode_t *tn)
{
if (tn->tn_op == CVT &&
tn->u.ops.left->tn_op == ADDR &&
tn->u.ops.left->u.ops.left->tn_op == STRING)
return true;
const type_t *tp = before_conversion(tn)->tn_type;
return tp->t_tspec == PTR &&
tp->t_subt->t_tspec == CHAR &&
tp->t_subt->t_const;
}
static bool
is_const_pointer(const tnode_t *tn)
{
const type_t *tp = before_conversion(tn)->tn_type;
return tp->t_tspec == PTR && tp->t_subt->t_const;
}
static void
check_unconst_function(const type_t *lstp, const tnode_t *rn)
{
const char *function_name;
if (lstp->t_tspec == CHAR && !lstp->t_const &&
is_direct_function_call(rn, &function_name) &&
is_unconst_function(function_name) &&
rn->u.call->args_len >= 1 &&
is_const_char_pointer(rn->u.call->args[0])) {
warning(346, function_name);
}
if (!lstp->t_const &&
is_direct_function_call(rn, &function_name) &&
strcmp(function_name, "bsearch") == 0 &&
rn->u.call->args_len >= 2 &&
is_const_pointer(rn->u.call->args[1])) {
warning(346, function_name);
}
}
static bool
check_assign_void_pointer_compat(op_t op, const function_call *call, int arg,
tspec_t lt,
const type_t *lstp, tspec_t lst,
const tnode_t *rn,
const type_t *rtp, tspec_t rt,
const type_t *rstp, tspec_t rst)
{
if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
types_compatible(lstp, rstp,
true, false, NULL))))
return false;
char qualifiers[32];
snprintf(qualifiers, sizeof(qualifiers), "%s%s",
!lstp->t_const && rstp->t_const ? " const" : "",
!lstp->t_volatile && rstp->t_volatile ? " volatile" : "");
if (allow_c90 && qualifiers[0] != '\0') {
switch (op) {
case INIT:
case RETURN:
warning(182, op_name(op),
qualifiers + 1, type_name(rtp));
break;
case FARG:
warning(383, type_name(rtp), arg,
function_call_descr(call), qualifiers + 1);
break;
default:
warning(128, op_name(op),
qualifiers + 1, type_name(rtp));
break;
}
}
if (allow_c90)
check_unconst_function(lstp, rn);
return true;
}
static bool
check_assign_pointer_integer(op_t op, int arg,
const type_t *ltp, tspec_t lt,
const type_t *rtp, tspec_t rt)
{
if (!((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)))
return false;
const char *lx = lt == PTR ? "pointer" : "integer";
const char *rx = rt == PTR ? "pointer" : "integer";
switch (op) {
case INIT:
case RETURN:
warning(183,
lx, type_name(ltp), rx, type_name(rtp), op_name(op));
break;
case FARG:
warning(154,
lx, type_name(ltp), rx, type_name(rtp), arg);
break;
default:
warning(123,
lx, type_name(ltp), rx, type_name(rtp), op_name(op));
break;
}
return true;
}
static bool
check_assign_pointer(op_t op, int arg,
const type_t *ltp, tspec_t lt,
const type_t *rtp, tspec_t rt)
{
if (!(lt == PTR && rt == PTR))
return false;
if (op == FARG)
warning(153, type_name(rtp), type_name(ltp), arg);
else
warn_incompatible_pointers(op, ltp, rtp);
return true;
}
static void
warn_assign(op_t op, int arg,
const type_t *ltp, tspec_t lt,
const type_t *rtp, tspec_t rt)
{
switch (op) {
case INIT:
error(185, type_name(ltp), type_name(rtp));
break;
case RETURN:
error(211, type_name(ltp), type_name(rtp));
break;
case FARG:
warning(155, type_name(rtp), type_name(ltp), arg);
break;
default:
warn_incompatible_types(op, ltp, lt, rtp, rt);
break;
}
}
static bool
check_assign_types_compatible(op_t op, const function_call *call, int arg,
const tnode_t *ln, const tnode_t *rn)
{
tspec_t lt, rt, lst = NO_TSPEC, rst = NO_TSPEC;
type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL;
if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
lst = (lstp = ltp->t_subt)->t_tspec;
if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
rst = (rstp = rtp->t_subt)->t_tspec;
if (lt == BOOL && is_scalar(rt))
return true;
if (is_arithmetic(lt) && (is_arithmetic(rt) || rt == BOOL))
return true;
if (is_struct_or_union(lt) && is_struct_or_union(rt))
return ltp->u.sou == rtp->u.sou;
if (lt == PTR && is_null_pointer(rn)) {
if (is_integer(rn->tn_type->t_tspec))
query_message(15, type_name(ltp));
return true;
}
check_assign_void_pointer(op, arg, lt, lst, rt, rst);
if (check_assign_void_pointer_compat(op, call, arg,
lt, lstp, lst, rn, rtp, rt, rstp, rst))
return true;
if (check_assign_pointer_integer(op, arg, ltp, lt, rtp, rt))
return true;
if (check_assign_pointer(op, arg, ltp, lt, rtp, rt))
return true;
warn_assign(op, arg, ltp, lt, rtp, rt);
return false;
}
static bool
has_side_effect(const tnode_t *tn)
{
op_t op = tn->tn_op;
if (modtab[op].m_has_side_effect)
return true;
if (op == CVT && tn->tn_type->t_tspec == VOID)
return has_side_effect(tn->u.ops.left);
if (op == LOGAND || op == LOGOR)
return has_side_effect(tn->u.ops.right);
if (op == QUEST)
return has_side_effect(tn->u.ops.right);
if (op == COLON || op == COMMA) {
return has_side_effect(tn->u.ops.left) ||
has_side_effect(tn->u.ops.right);
}
return false;
}
static bool
is_void_cast(const tnode_t *tn)
{
return tn->tn_op == CVT && tn->tn_cast &&
tn->tn_type->t_tspec == VOID;
}
static bool
is_local_symbol(const tnode_t *tn)
{
return tn->tn_op == LOAD &&
tn->u.ops.left->tn_op == NAME &&
tn->u.ops.left->u.sym->s_scl == AUTO;
}
static bool
is_int_constant_zero(const tnode_t *tn)
{
return tn->tn_op == CON &&
tn->tn_type->t_tspec == INT &&
tn->u.value.u.integer == 0;
}
static void
check_null_effect(const tnode_t *tn)
{
if (hflag &&
!has_side_effect(tn) &&
!(is_void_cast(tn) && is_local_symbol(tn->u.ops.left)) &&
!(is_void_cast(tn) && is_int_constant_zero(tn->u.ops.left))) {
warning(129);
}
}
static bool
typeok_op(op_t op, const function_call *call, int arg,
const tnode_t *ln, const type_t *ltp, tspec_t lt,
const tnode_t *rn, const type_t *rtp, tspec_t rt)
{
switch (op) {
case ARROW:
return typeok_arrow(lt);
case POINT:
return typeok_point(ln, ltp, lt);
case INCBEF:
case DECBEF:
case INCAFT:
case DECAFT:
return typeok_incdec(op, ln, ltp);
case INDIR:
return typeok_indir(ltp, lt);
case ADDR:
return typeok_address(op, ln, ltp, lt);
case PLUS:
return typeok_plus(op, ltp, lt, rtp, rt);
case MINUS:
return typeok_minus(op, ltp, lt, rtp, rt);
case SHL:
typeok_shl(ln, lt, rn, rt);
goto shift;
case SHR:
typeok_shr(ln, lt, rn, rt);
shift:
typeok_shift(ln, lt, rn, rt);
break;
case LT:
case LE:
case GT:
case GE:
compare:
return typeok_compare(op, ln, ltp, lt, rn, rtp, rt);
case EQ:
case NE:
if (is_typeok_eq(ln, lt, rn, rt))
break;
goto compare;
case QUEST:
return typeok_quest(lt, rn);
case COLON:
return typeok_colon(ln, ltp, lt, rn, rtp, rt);
case ASSIGN:
case INIT:
case FARG:
case RETURN:
if (!check_assign_types_compatible(op, call, arg, ln, rn))
return false;
goto assign;
case MULASS:
case DIVASS:
case MODASS:
goto assign;
case ADDASS:
case SUBASS:
if ((lt == PTR && !is_integer(rt)) || rt == PTR) {
warn_incompatible_types(op, ltp, lt, rtp, rt);
return false;
}
goto assign;
case SHLASS:
goto assign;
case SHRASS:
if (pflag && !is_uinteger(lt) &&
!(!allow_c90 && is_uinteger(rt))) {
warning(117, op_name(op), expr_type_name(rn));
}
goto assign;
case ANDASS:
case XORASS:
case ORASS:
assign:
return typeok_assign(op, ln, ltp, lt);
case COMMA:
if (!modtab[ln->tn_op].m_has_side_effect)
check_null_effect(ln);
break;
default:
break;
}
return true;
}
static void
check_bad_enum_operation(op_t op, const tnode_t *ln, const tnode_t *rn)
{
if (!eflag)
return;
if (op == PLUS &&
((ln->tn_type->t_is_enum && rn->tn_type->t_tspec == PTR) ||
(rn->tn_type->t_is_enum && ln->tn_type->t_tspec == PTR))) {
return;
}
warning(241, op_name(op));
}
static void
check_enum_type_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
{
const mod_t *mp = &modtab[op];
if (ln->tn_type->u.enumer != rn->tn_type->u.enumer) {
switch (op) {
case INIT:
warning(210,
type_name(ln->tn_type), type_name(rn->tn_type));
break;
case FARG:
warning(156,
type_name(ln->tn_type), type_name(rn->tn_type),
arg);
break;
case RETURN:
warning(211,
type_name(ln->tn_type), type_name(rn->tn_type));
break;
default:
warning(130, expr_type_name(before_conversion(ln)),
op_name(op),
expr_type_name(before_conversion(rn)));
break;
}
} else if (Pflag && eflag && mp->m_comparison && op != EQ && op != NE)
warning(243, op_name(op), type_name(ln->tn_type));
}
static void
check_enum_int_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
{
if (!eflag)
return;
switch (op) {
case INIT:
if (!rn->tn_type->t_is_enum && rn->tn_op == CON &&
is_integer(rn->tn_type->t_tspec) &&
rn->u.value.u.integer == 0) {
return;
}
warning(277, type_name(ln->tn_type), expr_type_name(rn));
break;
case FARG:
warning(278,
type_name(ln->tn_type), expr_type_name(rn), arg);
break;
case RETURN:
warning(279, type_name(ln->tn_type), expr_type_name(rn));
break;
default:
warning(242, type_name(ln->tn_type), expr_type_name(rn),
op_name(op));
break;
}
}
static void
typeok_enum(op_t op, const mod_t *mp, int arg,
const tnode_t *ln, const type_t *ltp,
const tnode_t *rn, const type_t *rtp)
{
if (mp->m_bad_on_enum &&
(ltp->t_is_enum || (mp->m_binary && rtp->t_is_enum))) {
check_bad_enum_operation(op, ln, rn);
} else if (mp->m_valid_on_enum &&
(ltp->t_is_enum && rtp != NULL && rtp->t_is_enum)) {
check_enum_type_mismatch(op, arg, ln, rn);
} else if (mp->m_valid_on_enum &&
(ltp->t_is_enum || (rtp != NULL && rtp->t_is_enum))) {
check_enum_int_mismatch(op, arg, ln, rn);
}
}
bool
typeok(op_t op, const function_call *call, int arg,
const tnode_t *ln, const tnode_t *rn)
{
const mod_t *mp = &modtab[op];
type_t *ltp = ln->tn_type;
tspec_t lt = ltp->t_tspec;
type_t *rtp = mp->m_binary ? rn->tn_type : NULL;
tspec_t rt = mp->m_binary ? rtp->t_tspec : NO_TSPEC;
if (Tflag && !typeok_scalar_strict_bool(op, mp, arg, ln, rn))
return false;
if (!typeok_scalar(op, mp, ltp, lt, rtp, rt))
return false;
if (!typeok_op(op, call, arg, ln, ltp, lt, rn, rtp, rt))
return false;
typeok_enum(op, mp, arg, ln, ltp, rn, rtp);
return true;
}
static tspec_t
promote_trad(tspec_t t)
{
if (t == UCHAR || t == USHORT)
return UINT;
if (t == CHAR || t == SCHAR || t == SHORT)
return INT;
if (t == FLOAT)
return DOUBLE;
if (t == ENUM)
return INT;
return t;
}
static tspec_t
promote_c90(const tnode_t *tn, tspec_t t, bool farg)
{
if (tn->tn_type->t_bitfield) {
unsigned int width = tn->tn_type->t_bit_field_width;
unsigned int int_width = size_in_bits(INT);
if (width < int_width)
return INT;
if (width == int_width)
return is_uinteger(t) ? UINT : INT;
return t;
}
if (t == CHAR || t == SCHAR)
return INT;
if (t == UCHAR)
return size_in_bits(CHAR) < size_in_bits(INT) ? INT : UINT;
if (t == SHORT)
return INT;
if (t == USHORT)
return size_in_bits(SHORT) < size_in_bits(INT) ? INT : UINT;
if (t == ENUM)
return INT;
if (farg && t == FLOAT)
return DOUBLE;
return t;
}
tnode_t *
promote(op_t op, bool farg, tnode_t *tn)
{
const type_t *otp = tn->tn_type;
tspec_t ot = otp->t_tspec;
if (!is_arithmetic(ot))
return tn;
tspec_t nt = allow_c90 ? promote_c90(tn, ot, farg) : promote_trad(ot);
if (nt == ot)
return tn;
type_t *ntp = expr_dup_type(gettyp(nt));
ntp->t_tspec = nt;
ntp->t_is_enum = otp->t_is_enum;
if (ntp->t_is_enum)
ntp->u.enumer = otp->u.enumer;
return convert(op, 0, ntp, tn);
}
static void
check_lossy_floating_to_integer_conversion(
op_t op, int arg, const type_t *tp, const tnode_t *tn)
{
long double x = tn->u.value.u.floating;
long double oob = powl(2.0L,
width_in_bits(tp) - (is_uinteger(tp->t_tspec) ? 0 : 1));
if (is_uinteger(tp->t_tspec)
? x >= 0.0L && x < oob && x == (uint64_t)x
: x >= -oob && x < oob && x == (int64_t)x)
return;
if (op == FARG)
warning(380, x, x, type_name(tp), arg);
else
warning(381, x, x, type_name(tp));
}
static void
convert_integer_from_floating(
op_t op, int arg, const type_t *tp, const tnode_t *tn)
{
if (op == CVT)
query_message(2, type_name(tn->tn_type), type_name(tp));
else
query_message(1, type_name(tn->tn_type), type_name(tp));
if (tn->tn_op == CON && op != CVT)
check_lossy_floating_to_integer_conversion(op, arg, tp, tn);
}
static bool
should_warn_about_prototype_conversion(tspec_t nt,
tspec_t ot, const tnode_t *ptn)
{
if (nt == ot)
return false;
if (nt == ENUM && ot == INT)
return false;
if (is_floating(nt) != is_floating(ot) ||
portable_rank_cmp(nt, ot) != 0) {
if (!is_integer(ot))
return true;
return portable_rank_cmp(ot, INT) > 0;
}
if (!hflag)
return false;
if (ptn->tn_op == CON && is_integer(nt) &&
signed_type(nt) == signed_type(ot) &&
!msb(ptn->u.value.u.integer, ot))
return false;
return true;
}
static void
check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp,
tnode_t *tn)
{
if (!is_arithmetic(nt) || !is_arithmetic(ot))
return;
if (nt == CHAR || nt == SCHAR || nt == UCHAR ||
nt == SHORT || nt == USHORT)
return;
tnode_t *ptn = promote(NOOP, true, tn);
ot = ptn->tn_type->t_tspec;
if (should_warn_about_prototype_conversion(nt, ot, ptn)) {
warning(259, arg, expr_type_name(tn), type_name(tp));
}
}
static bool
can_represent(const type_t *tp, const tnode_t *tn)
{
uint64_t nmask = value_bits(width_in_bits(tp));
if (!is_uinteger(tp->t_tspec))
nmask >>= 1;
integer_constraints c = ic_expr(tn);
if ((~c.bclr & ~nmask) == 0)
return true;
integer_constraints tpc = ic_any(tp);
if (is_uinteger(tp->t_tspec)
? tpc.umin <= c.umin && tpc.umax >= c.umax
: tpc.smin <= c.smin && tpc.smax >= c.smax)
return true;
debug_enter();
debug_step("type '%s' cannot represent:", type_name(tp));
debug_node(tn);
debug_leave();
return false;
}
static bool
should_warn_about_integer_conversion(const type_t *ntp, tspec_t nt,
const tnode_t *otn, tspec_t ot)
{
if (aflag > 0 && portable_rank_cmp(nt, ot) < 0) {
if (ot == LONG || ot == ULONG
|| ot == LLONG || ot == ULLONG
|| ot == INT128 || ot == UINT128
|| aflag > 1)
return !can_represent(ntp, otn);
}
return false;
}
static void
convert_integer_from_integer(op_t op, int arg, tspec_t nt, tspec_t ot,
type_t *tp, tnode_t *tn)
{
if (tn->tn_op == CON)
return;
if (op == CVT)
return;
if (Pflag && pflag && aflag > 0 &&
portable_rank_cmp(nt, ot) > 0 &&
is_uinteger(nt) != is_uinteger(ot)) {
if (op == FARG)
warning(297, type_name(tp), arg);
else
warning(131, type_name(tp));
}
if (Pflag && portable_rank_cmp(nt, ot) > 0 &&
(tn->tn_op == PLUS || tn->tn_op == MINUS || tn->tn_op == MULT ||
tn->tn_op == SHL)) {
warning(324, type_name(gettyp(ot)), type_name(tp),
op_name(tn->tn_op));
}
if (should_warn_about_integer_conversion(tp, nt, tn, ot)) {
if (op == FARG)
warning(298, expr_type_name(tn), type_name(tp), arg);
else
warning(132, expr_type_name(tn), type_name(tp));
}
if (is_uinteger(nt) != is_uinteger(ot))
query_message(3, expr_type_name(tn), type_name(tp));
}
static void
convert_integer_from_pointer(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
{
if (tn->tn_op == CON)
return;
if (op != CVT)
return;
if (portable_rank_cmp(nt, PTR) >= 0)
return;
if (pflag && size_in_bits(nt) >= size_in_bits(PTR)) {
warning(134, type_name(tp));
} else {
warning(133, type_name(tp));
}
}
static bool
struct_starts_with(const type_t *struct_tp, const type_t *member_tp)
{
return struct_tp->u.sou->sou_first_member != NULL &&
types_compatible(struct_tp->u.sou->sou_first_member->s_type,
member_tp, true, false, NULL);
}
static bool
is_byte_array(const type_t *tp)
{
return tp->t_tspec == ARRAY &&
(tp->t_subt->t_tspec == CHAR || tp->t_subt->t_tspec == UCHAR);
}
static bool
union_contains(const type_t *utp, const type_t *mtp)
{
for (const sym_t *mem = utp->u.sou->sou_first_member;
mem != NULL; mem = mem->s_next) {
if (types_compatible(mem->s_type, mtp, true, false, NULL))
return true;
}
return false;
}
static bool
should_warn_about_pointer_cast(const type_t *nstp, tspec_t nst,
const type_t *ostp, tspec_t ost)
{
while (nst == ARRAY)
nstp = nstp->t_subt, nst = nstp->t_tspec;
while (ost == ARRAY)
ostp = ostp->t_subt, ost = ostp->t_tspec;
if (nst == STRUCT && ost == STRUCT &&
(struct_starts_with(nstp, ostp) ||
struct_starts_with(ostp, nstp)))
return false;
if (is_incomplete(nstp) || is_incomplete(ostp))
return false;
if (nst == CHAR || nst == UCHAR)
return false;
if (ost == CHAR || ost == UCHAR)
return false;
if (nst == STRUCT && ost == STRUCT) {
const sym_t *nmem = nstp->u.sou->sou_first_member;
const sym_t *omem = ostp->u.sou->sou_first_member;
while (nmem != NULL && omem != NULL &&
types_compatible(nmem->s_type, omem->s_type,
true, false, NULL))
nmem = nmem->s_next, omem = omem->s_next;
if (nmem != NULL && is_byte_array(nmem->s_type))
return false;
if (omem != NULL && is_byte_array(omem->s_type))
return false;
if (nmem == NULL && omem == NULL)
return false;
}
if (nst == UNION || ost == UNION) {
const type_t *union_tp = nst == UNION ? nstp : ostp;
const type_t *other_tp = nst == UNION ? ostp : nstp;
if (union_contains(union_tp, other_tp))
return false;
}
if (is_struct_or_union(nst) && is_struct_or_union(ost))
return nstp->u.sou != ostp->u.sou;
enum rank_kind rk1 = type_properties(nst)->tt_rank_kind;
enum rank_kind rk2 = type_properties(ost)->tt_rank_kind;
if (rk1 != rk2 || rk1 == RK_NONE)
return true;
return portable_rank_cmp(nst, ost) != 0;
}
static void
convert_pointer_from_pointer(type_t *ntp, tnode_t *tn)
{
const type_t *nstp = ntp->t_subt;
const type_t *otp = tn->tn_type;
const type_t *ostp = otp->t_subt;
tspec_t nst = nstp->t_tspec;
tspec_t ost = ostp->t_tspec;
if (nst == VOID || ost == VOID) {
if (!allow_trad && !allow_c99 && (nst == FUNC || ost == FUNC)) {
const char *nts, *ots;
*(nst == FUNC ? &nts : &ots) = "function pointer";
*(nst == VOID ? &nts : &ots) = "'void *'";
warning(303, ots, nts);
}
return;
}
if (nst == FUNC && ost == FUNC)
return;
if (nst == FUNC || ost == FUNC) {
warning(229, type_name(otp), type_name(ntp));
return;
}
if (hflag && alignment(nstp) > alignment(ostp) &&
!is_incomplete(ostp) && alignment(ostp) > 1 &&
!(nst == UNION && union_contains(nstp, ostp))) {
warning(135, type_name(otp), type_name(ntp),
alignment(ostp), alignment(nstp));
}
if (cflag && should_warn_about_pointer_cast(nstp, nst, ostp, ost)) {
warning(247, type_name(ostp), type_name(nstp));
}
}
tnode_t *
convert(op_t op, int arg, type_t *tp, tnode_t *tn)
{
tspec_t nt = tp->t_tspec;
tspec_t ot = tn->tn_type->t_tspec;
if (allow_trad && allow_c90 && op == FARG)
check_prototype_conversion(arg, nt, ot, tp, tn);
if (nt == BOOL) {
} else if (is_integer(nt)) {
if (ot == BOOL) {
} else if (is_integer(ot))
convert_integer_from_integer(op, arg, nt, ot, tp, tn);
else if (is_floating(ot))
convert_integer_from_floating(op, arg, tp, tn);
else if (ot == PTR)
convert_integer_from_pointer(op, nt, tp, tn);
} else if (is_floating(nt)) {
if (is_integer(ot) && op != CVT) {
query_message(19,
type_name(tn->tn_type), type_name(tp));
}
} else if (nt == PTR) {
if (is_null_pointer(tn)) {
} else if (ot == PTR && op == CVT)
convert_pointer_from_pointer(tp, tn);
}
tnode_t *ntn = expr_alloc_tnode();
ntn->tn_op = CVT;
ntn->tn_type = tp;
ntn->tn_cast = op == CVT;
ntn->tn_sys |= tn->tn_sys;
ntn->u.ops.right = NULL;
if (tn->tn_op != CON || nt == VOID) {
ntn->u.ops.left = tn;
} else {
ntn->tn_op = CON;
convert_constant(op, arg, ntn->tn_type, &ntn->u.value,
&tn->u.value);
}
return ntn;
}
static void
convert_constant_from_floating(op_t op, int arg, const type_t *ntp,
tspec_t nt, val_t *nv, val_t *ov)
{
long double max = 0.0, min = 0.0;
switch (nt) {
case CHAR:
max = TARG_CHAR_MAX; min = TARG_CHAR_MIN; break;
case UCHAR:
max = TARG_UCHAR_MAX; min = 0; break;
case SCHAR:
max = TARG_SCHAR_MAX; min = TARG_SCHAR_MIN; break;
case SHORT:
max = TARG_SHRT_MAX; min = TARG_SHRT_MIN; break;
case USHORT:
max = TARG_USHRT_MAX; min = 0; break;
case ENUM:
case INT:
max = TARG_INT_MAX; min = TARG_INT_MIN; break;
case UINT:
max = TARG_UINT_MAX; min = 0; break;
case LONG:
max = TARG_LONG_MAX; min = TARG_LONG_MIN; break;
case ULONG:
max = TARG_ULONG_MAX; min = 0; break;
case LLONG:
max = LLONG_MAX; min = LLONG_MIN; break;
case ULLONG:
max = ULLONG_MAX; min = 0; break;
case FLOAT:
case FCOMPLEX:
max = FLT_MAX; min = -FLT_MAX; break;
case DOUBLE:
case DCOMPLEX:
max = DBL_MAX; min = -DBL_MAX; break;
case LDOUBLE:
case LCOMPLEX:
max = LDBL_MAX; min = -max; break;
default:
lint_assert(false);
}
if (ov->u.floating > max || ov->u.floating < min) {
lint_assert(nt != LDOUBLE);
const char *ot_name = type_name(gettyp(ov->v_tspec));
const char *nt_name = type_name(ntp);
if (is_integer(nt))
goto after_warning;
if (op == FARG)
warning(295, ot_name, nt_name, arg);
else
warning(119, ot_name, nt_name);
after_warning:
ov->u.floating = ov->u.floating > 0 ? max : min;
}
if (nt == FLOAT || nt == FCOMPLEX)
nv->u.floating = (float)ov->u.floating;
else if (nt == DOUBLE || nt == DCOMPLEX)
nv->u.floating = (double)ov->u.floating;
else if (nt == LDOUBLE || nt == LCOMPLEX)
nv->u.floating = ov->u.floating;
else
nv->u.integer = (int64_t)ov->u.floating;
}
static bool
convert_constant_to_floating(tspec_t nt, val_t *nv,
tspec_t ot, const val_t *v)
{
if (nt == FLOAT) {
nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
(float)(uint64_t)v->u.integer : (float)v->u.integer;
} else if (nt == DOUBLE) {
nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
(double)(uint64_t)v->u.integer : (double)v->u.integer;
} else if (nt == LDOUBLE) {
nv->u.floating = (ot == PTR || is_uinteger(ot))
? (long double)(uint64_t)v->u.integer
: (long double)v->u.integer;
} else
return false;
return true;
}
static void
warn_constant_truncated(op_t op, const val_t *v)
{
char buf[256];
bool is_unsigned = is_uinteger(v->v_tspec);
int64_t val = v->u.integer;
unsigned long long abs_val = is_unsigned || val >= 0
? (unsigned long long)val
: -(unsigned long long)val;
const char *sign = is_unsigned || val >= 0 ? "" : "-";
snprintf(buf, sizeof(buf), "%s%#llx", sign, abs_val);
warning(306, buf, op_name(op));
}
static void
convert_constant_check_range_bitor(size_t nsz, size_t osz, const val_t *v,
uint64_t xmask, op_t op)
{
if (nsz < osz && (v->u.integer & xmask) != 0)
warn_constant_truncated(op, v);
}
static void
convert_constant_check_range_bitand(size_t nsz, size_t osz,
uint64_t xmask, const val_t *nv,
tspec_t ot, const val_t *v,
const type_t *tp, op_t op)
{
if (nsz > osz &&
(nv->u.integer & bit((unsigned int)(osz - 1))) != 0 &&
(nv->u.integer & xmask) != xmask) {
warning(309,
op_name(op), type_name(gettyp(ot)), type_name(tp));
} else if (nsz < osz &&
(v->u.integer & xmask) != xmask &&
(v->u.integer & xmask) != 0)
warn_constant_truncated(op, v);
}
static void
convert_constant_check_range_signed(op_t op, int arg,
const type_t *ntp, int64_t ov)
{
if (op == ASSIGN)
warning(164, (long long)ov, type_name(ntp));
else if (op == INIT)
warning(221, type_name(ntp), (long long)ov);
else if (op == FARG)
warning(296, (long long)ov, type_name(ntp), arg);
else if (modtab[op].m_comparison) {
} else
warning(222, (long long)ov, type_name(ntp));
}
static void
warn_constant_check_range_truncated(op_t op, int arg, const type_t *tp,
tspec_t ot)
{
if (op == ASSIGN && tp->t_bitfield)
warning(166);
else if (op == ASSIGN)
warning(165);
else if (op == INIT && tp->t_bitfield)
warning(180);
else if (op == INIT)
warning(178);
else if (op == CASE)
warning(196, tspec_name(ot), type_name(tp));
else if (op == FARG)
warning(295, type_name(gettyp(ot)), type_name(tp), arg);
else
warning(119, type_name(gettyp(ot)), type_name(tp));
}
static void
warn_constant_check_range_loss(op_t op, int arg, const type_t *tp,
tspec_t ot)
{
if (op == ASSIGN && tp->t_bitfield)
warning(166);
else if (op == INIT && tp->t_bitfield)
warning(11);
else if (op == CASE)
warning(196, tspec_name(ot), type_name(tp));
else if (op == FARG)
warning(295, type_name(gettyp(ot)), type_name(tp), arg);
else
warning(119, type_name(gettyp(ot)), type_name(tp));
}
static void
convert_constant_check_range(tspec_t ot, const type_t *tp, tspec_t nt,
op_t op, int arg, const val_t *v, val_t *nv)
{
unsigned int obitsz, nbitsz;
uint64_t xmask, xmsk1;
obitsz = size_in_bits(ot);
nbitsz = tp->t_bitfield ? tp->t_bit_field_width : size_in_bits(nt);
xmask = value_bits(nbitsz) ^ value_bits(obitsz);
xmsk1 = value_bits(nbitsz) ^ value_bits(obitsz - 1);
if (op == ORASS || op == BITOR || op == BITXOR) {
convert_constant_check_range_bitor(
nbitsz, obitsz, v, xmask, op);
} else if (op == ANDASS || op == BITAND) {
convert_constant_check_range_bitand(
nbitsz, obitsz, xmask, nv, ot, v, tp, op);
} else if (nt != PTR && is_uinteger(nt) &&
ot != PTR && !is_uinteger(ot) && v->u.integer < 0)
convert_constant_check_range_signed(op, arg, tp, v->u.integer);
else if (nv->u.integer != v->u.integer && nbitsz <= obitsz &&
(v->u.integer & xmask) != 0 &&
(is_uinteger(ot) || (v->u.integer & xmsk1) != xmsk1))
warn_constant_check_range_truncated(op, arg, tp, ot);
else if (nv->u.integer != v->u.integer)
warn_constant_check_range_loss(op, arg, tp, ot);
}
void
convert_constant(op_t op, int arg, const type_t *ntp, val_t *nv, val_t *ov)
{
tspec_t ot = ov->v_tspec;
tspec_t nt = nv->v_tspec = ntp->t_tspec;
bool range_check = false;
if (nt == BOOL) {
nv->v_unsigned_since_c90 = false;
nv->u.integer = is_nonzero_val(ov) ? 1 : 0;
return;
}
if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE)
convert_constant_from_floating(op, arg, ntp, nt, nv, ov);
else if (!convert_constant_to_floating(nt, nv, ot, ov)) {
range_check = true;
nv->u.integer = ov->u.integer;
}
if (allow_trad && allow_c90 && ov->v_unsigned_since_c90 &&
(is_floating(nt) || (
(is_integer(nt) && !is_uinteger(nt) &&
portable_rank_cmp(nt, ot) > 0)))) {
warning(157);
ov->v_unsigned_since_c90 = false;
}
if (is_integer(nt)) {
unsigned int size = ntp->t_bitfield
? ntp->t_bit_field_width : size_in_bits(nt);
nv->u.integer = convert_integer(nv->u.integer, nt, size);
}
if (range_check && op != CVT)
convert_constant_check_range(ot, ntp, nt, op, arg, ov, nv);
}
tnode_t *
build_sizeof(const type_t *tp)
{
unsigned int size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, size_in_bytes);
tn->tn_system_dependent = true;
debug_step("build_sizeof '%s' = %u", type_name(tp), size_in_bytes);
return tn;
}
tnode_t *
build_offsetof(const type_t *tp, designation dn)
{
unsigned int offset_in_bits = 0;
if (!is_struct_or_union(tp->t_tspec)) {
error(111, "offsetof");
goto proceed;
}
for (size_t i = 0; i < dn.dn_len; i++) {
const designator *dr = dn.dn_items + i;
if (dr->dr_kind == DK_SUBSCRIPT) {
if (tp->t_tspec != ARRAY)
goto proceed;
tp = tp->t_subt;
offset_in_bits += (unsigned)dr->dr_subscript
* type_size_in_bits(tp);
} else {
if (!is_struct_or_union(tp->t_tspec))
goto proceed;
const char *name = dr->dr_member->s_name;
sym_t *mem = find_member(tp->u.sou, name);
if (mem == NULL) {
error(101, name, type_name(tp));
goto proceed;
}
tp = mem->s_type;
offset_in_bits += mem->u.s_member.sm_offset_in_bits;
}
}
free(dn.dn_items);
proceed:;
unsigned int offset_in_bytes = offset_in_bits / CHAR_SIZE;
tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, offset_in_bytes);
tn->tn_system_dependent = true;
return tn;
}
unsigned int
type_size_in_bits(const type_t *tp)
{
unsigned int elem = 1;
bool flex = false;
lint_assert(tp != NULL);
while (tp->t_tspec == ARRAY) {
flex = true;
elem *= tp->u.dimension;
tp = tp->t_subt;
}
if (elem == 0 && !flex) {
error(143);
elem = 1;
}
unsigned int elsz;
switch (tp->t_tspec) {
case VOID:
error(146);
elsz = 1;
break;
case FUNC:
error(144, type_name(tp));
elsz = 1;
break;
case STRUCT:
case UNION:
if (is_incomplete(tp)) {
error(143);
elsz = 1;
} else
elsz = tp->u.sou->sou_size_in_bits;
break;
case ENUM:
if (is_incomplete(tp)) {
warning(143);
}
default:
if (tp->t_bitfield)
error(145);
elsz = size_in_bits(tp->t_tspec);
lint_assert(elsz > 0);
break;
}
return elem * elsz;
}
tnode_t *
build_alignof(const type_t *tp)
{
if (tp->t_tspec == FUNC) {
error(144, type_name(tp));
return NULL;
}
if (tp->t_tspec == VOID) {
error(146);
return NULL;
}
if (is_incomplete(tp)) {
error(143);
return NULL;
}
if (tp->t_bitfield) {
error(145);
return NULL;
}
return build_integer_constant(SIZEOF_TSPEC, (int64_t)alignment(tp));
}
static tnode_t *
cast_to_union(tnode_t *otn, bool sys, type_t *ntp)
{
if (!allow_gcc) {
error(328);
return NULL;
}
for (const sym_t *m = ntp->u.sou->sou_first_member;
m != NULL; m = m->s_next) {
if (types_compatible(m->s_type, otn->tn_type,
false, false, NULL)) {
tnode_t *ntn = build_op(CVT, sys, ntp, otn, NULL);
ntn->tn_cast = true;
return ntn;
}
}
error(329, type_name(otn->tn_type), type_name(ntp));
return NULL;
}
static tnode_t *
null_pointer_offset(tnode_t *tn)
{
uint64_t off = 0;
const tnode_t *n = tn;
while ((n->tn_op == PLUS || n->tn_op == MINUS)
&& is_integer(n->u.ops.right->tn_type->t_tspec)) {
off += (uint64_t)n->u.ops.right->u.value.u.integer;
n = n->u.ops.left;
}
if (n->tn_type->t_tspec == PTR
&& n->tn_op == ADDR
&& n->u.ops.left->tn_op == INDIR
&& n->u.ops.left->u.ops.left->tn_op == CON
&& n->u.ops.left->u.ops.left->tn_type->t_tspec == PTR) {
off += (uint64_t)n->u.ops.left->u.ops.left->u.value.u.integer;
return build_integer_constant(SIZEOF_TSPEC, (int64_t)off);
}
return tn;
}
tnode_t *
cast(tnode_t *tn, bool sys, type_t *tp)
{
if (tn == NULL)
return NULL;
tn = cconv(tn);
lint_assert(tp != NULL);
tspec_t nt = tp->t_tspec;
tspec_t ot = tn->tn_type->t_tspec;
if (nt == VOID) {
} else if (nt == UNION)
return cast_to_union(tn, sys, tp);
else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
if (!(allow_gcc && nt == STRUCT))
goto invalid_cast;
} else if (is_struct_or_union(ot))
goto invalid_cast;
else if (ot == VOID) {
error(148);
return NULL;
} else if (is_integer(nt) && is_scalar(ot)) {
tn = null_pointer_offset(tn);
} else if (is_floating(nt) && is_arithmetic(ot)) {
} else if (nt == PTR && is_integer(ot)) {
} else if (nt == PTR && ot == PTR) {
if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
if (hflag)
warning(275, type_name(tn->tn_type));
}
} else
goto invalid_cast;
if (any_query_enabled
&& types_compatible(tp, tn->tn_type, false, false, NULL))
query_message(6, expr_type_name(tn), type_name(tp));
tn = convert(CVT, 0, tp, tn);
tn->tn_cast = true;
tn->tn_sys = sys;
return tn;
invalid_cast:
error(147, expr_type_name(tn), type_name(tp));
return NULL;
}
void
add_function_argument(function_call *call, tnode_t *arg)
{
if (arg == NULL)
arg = build_integer_constant(INT, 0);
if (call->args_len >= call->args_cap) {
call->args_cap += 8;
tnode_t **new_args = expr_zero_alloc(
call->args_cap * sizeof(*call->args), "tnode*[]");
if (call->args_len > 0)
memcpy(new_args, call->args,
call->args_len * sizeof(*call->args));
call->args = new_args;
}
call->args[call->args_len++] = arg;
}
static tnode_t *
convert_prototype_argument(const function_call *call, int arg,
type_t *tp, tnode_t *tn)
{
tnode_t *ln = xcalloc(1, sizeof(*ln));
ln->tn_type = expr_unqualified_type(tp);
ln->tn_lvalue = true;
if (typeok(FARG, call, arg, ln, tn)) {
bool dowarn;
if (!types_compatible(tp, tn->tn_type,
true, false, (dowarn = false, &dowarn)) || dowarn)
tn = convert(FARG, arg, tp, tn);
}
free(ln);
return tn;
}
static void
convert_function_arguments(const function_call *call)
{
type_t *ftp = call->func->tn_type->t_subt;
int npar = 0;
for (const sym_t *p = ftp->u.params; p != NULL; p = p->s_next)
npar++;
int narg = (int)call->args_len;
const sym_t *param = ftp->u.params;
if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
error(150, narg, narg != 1 ? "arguments" : "argument", npar);
param = NULL;
}
for (int i = 0; i < narg; i++) {
tnode_t *arg = call->args[i];
tspec_t at = arg->tn_type->t_tspec;
if (at == VOID) {
error(151, i + 1);
return;
}
if (is_struct_or_union(at) && is_incomplete(arg->tn_type)) {
error(152, i + 1);
return;
}
if (is_integer(at) &&
arg->tn_type->t_is_enum &&
is_incomplete(arg->tn_type)) {
warning(152, i + 1);
}
arg = cconv(arg);
call->args[i] = arg;
arg = param != NULL
? convert_prototype_argument(call,
i + 1, param->s_type, arg)
: promote(NOOP, true, arg);
call->args[i] = arg;
if (param != NULL)
param = param->s_next;
}
}
static bool
is_gcc_generic_atomic(const char *name)
{
return strcmp(name, "__atomic_load_n") == 0
|| strcmp(name, "__atomic_exchange_n") == 0
|| strcmp(name, "__atomic_add_fetch") == 0
|| strcmp(name, "__atomic_sub_fetch") == 0
|| strcmp(name, "__atomic_and_fetch") == 0
|| strcmp(name, "__atomic_xor_fetch") == 0
|| strcmp(name, "__atomic_or_fetch") == 0
|| strcmp(name, "__atomic_nand_fetch") == 0
|| strcmp(name, "__atomic_fetch_add") == 0
|| strcmp(name, "__atomic_fetch_sub") == 0
|| strcmp(name, "__atomic_fetch_and") == 0
|| strcmp(name, "__atomic_fetch_xor") == 0
|| strcmp(name, "__atomic_fetch_or") == 0
|| strcmp(name, "__atomic_fetch_nand") == 0;
}
static type_t *
return_type(const function_call *call)
{
const tnode_t *func = call->func;
if (allow_gcc
&& func->tn_op == ADDR
&& func->u.ops.left->tn_op == NAME
&& is_gcc_generic_atomic(func->u.ops.left->u.sym->s_name)
&& call->args_len > 0
&& call->args[0]->tn_type->t_tspec == PTR)
return call->args[0]->tn_type->t_subt;
return func->tn_type->t_subt->t_subt;
}
tnode_t *
build_function_call(tnode_t *func, bool sys, function_call *call)
{
if (func == NULL)
return NULL;
call->func = func;
check_ctype_function_call(call);
func = cconv(func);
call->func = func;
if (func->tn_type->t_tspec != PTR ||
func->tn_type->t_subt->t_tspec != FUNC) {
error(149, expr_type_name(func));
return NULL;
}
convert_function_arguments(call);
tnode_t *ntn = expr_alloc_tnode();
ntn->tn_op = CALL;
ntn->tn_type = return_type(call);
ntn->tn_sys = sys;
ntn->u.call = call;
return ntn;
}
val_t *
integer_constant(tnode_t *tn, bool required)
{
if (tn != NULL)
tn = cconv(tn);
if (tn != NULL)
tn = promote(NOOP, false, tn);
val_t *v = xcalloc(1, sizeof(*v));
if (tn == NULL) {
lint_assert(seen_error);
debug_step("constant node is null; returning 1 instead");
v->v_tspec = INT;
v->u.integer = 1;
return v;
}
v->v_tspec = tn->tn_type->t_tspec;
if (tn->tn_op == CON) {
lint_assert(tn->tn_type->t_tspec == tn->u.value.v_tspec);
if (is_integer(tn->u.value.v_tspec)) {
v->v_unsigned_since_c90 =
tn->u.value.v_unsigned_since_c90;
v->u.integer = tn->u.value.u.integer;
return v;
}
v->u.integer = (int64_t)tn->u.value.u.floating;
} else
v->u.integer = 1;
if (required)
error(55);
else
c99ism(318);
if (!is_integer(v->v_tspec))
v->v_tspec = INT;
return v;
}
void
expr(tnode_t *tn, bool used, bool cond, bool free_expr, bool is_do_while,
const char *stmt_kind)
{
if (tn == NULL) {
expr_free_all();
return;
}
if (dcs->d_kind != DLK_EXTERN && !is_do_while)
check_statement_reachable(stmt_kind);
check_expr_misc(tn, used, cond, !cond, false, false, false);
if (tn->tn_op == ASSIGN && !tn->tn_parenthesized) {
if (hflag && cond)
warning(159);
}
if (!modtab[tn->tn_op].m_has_side_effect) {
if (tn->tn_op != COMMA && !used && !cond)
check_null_effect(tn);
}
debug_node(tn);
if (free_expr)
expr_free_all();
}
static void
check_array_index(const tnode_t *indir, bool taking_address)
{
const tnode_t *plus, *arr, *idx;
if (indir->tn_op == INDIR
&& (plus = indir->u.ops.left, plus->tn_op == PLUS)
&& plus->u.ops.left->tn_op == ADDR
&& (arr = plus->u.ops.left->u.ops.left, true)
&& (arr->tn_op == STRING || arr->tn_op == NAME)
&& arr->tn_type->t_tspec == ARRAY
&& (idx = plus->u.ops.right, idx->tn_op == CON)
&& (!is_incomplete(arr->tn_type) || idx->u.value.u.integer < 0))
goto proceed;
return;
proceed:;
int elsz = length_in_bits(arr->tn_type->t_subt, NULL);
if (elsz == 0)
return;
elsz /= CHAR_SIZE;
int64_t con = is_uinteger(idx->tn_type->t_tspec)
? (int64_t)((uint64_t)idx->u.value.u.integer / elsz)
: idx->u.value.u.integer / elsz;
int dim = arr->tn_type->u.dimension + (taking_address ? 1 : 0);
if (!is_uinteger(idx->tn_type->t_tspec) && con < 0)
warning(167, (intmax_t)con);
else if (dim > 0 && (uint64_t)con >= (uint64_t)dim)
warning(168, (uintmax_t)con, dim - 1);
}
static void
check_expr_addr(const tnode_t *ln, bool szof, bool fcall)
{
if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
if (!szof)
mark_as_set(ln->u.sym);
mark_as_used(ln->u.sym, fcall, szof);
}
check_array_index(ln, true);
}
static bool
is_asm_around(void)
{
for (decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing)
if (dl->d_asm)
return true;
return false;
}
static void
check_expr_side_effect(const tnode_t *ln, bool szof)
{
if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
scl_t sc = ln->u.sym->s_scl;
if (sc != EXTERN && sc != STATIC &&
!ln->u.sym->s_set && !szof && !is_asm_around()) {
warning(158, ln->u.sym->s_name);
mark_as_set(ln->u.sym);
}
mark_as_used(ln->u.sym, false, false);
}
}
static void
check_expr_assign(const tnode_t *ln, bool szof)
{
if (ln->tn_op == NAME && !szof && (reached || !warn_about_unreachable)) {
mark_as_set(ln->u.sym);
if (ln->u.sym->s_scl == EXTERN)
outusg(ln->u.sym);
}
check_array_index(ln, false);
}
static void
check_expr_call(const tnode_t *tn, const tnode_t *ln,
bool szof, bool vctx, bool cond, bool retval_discarded)
{
lint_assert(ln->tn_op == ADDR);
lint_assert(ln->u.ops.left->tn_op == NAME);
if (!szof && !is_compiler_builtin(ln->u.ops.left->u.sym->s_name))
outcall(tn, vctx || cond, retval_discarded);
const function_call *call = tn->u.call;
if (call->args_len == 4 || call->args_len == 5)
check_snprintb(call);
}
static void
check_expr_op(op_t op, const tnode_t *ln, bool szof, bool fcall, bool eqwarn)
{
switch (op) {
case ADDR:
check_expr_addr(ln, szof, fcall);
break;
case LOAD:
check_array_index(ln, false);
case INCBEF:
case DECBEF:
case INCAFT:
case DECAFT:
case ADDASS:
case SUBASS:
case MULASS:
case DIVASS:
case MODASS:
case ANDASS:
case ORASS:
case XORASS:
case SHLASS:
case SHRASS:
case REAL:
case IMAG:
check_expr_side_effect(ln, szof);
break;
case ASSIGN:
check_expr_assign(ln, szof);
break;
case EQ:
if (hflag && eqwarn)
warning(160);
break;
default:
break;
}
}
void
check_expr_misc(const tnode_t *tn, bool vctx, bool cond,
bool eqwarn, bool fcall, bool retval_discarded, bool szof)
{
if (tn == NULL)
return;
op_t op = tn->tn_op;
if (op == NAME || op == CON || op == STRING)
return;
bool is_direct = op == CALL
&& tn->u.call->func->tn_op == ADDR
&& tn->u.call->func->u.ops.left->tn_op == NAME;
if (op == CALL) {
const function_call *call = tn->u.call;
if (is_direct)
check_expr_call(tn, call->func,
szof, vctx, cond, retval_discarded);
bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
check_expr_misc(call->func, false, false, false, is_direct,
discard, szof);
for (size_t i = 0, n = call->args_len; i < n; i++)
check_expr_misc(call->args[i],
true, false, false, false, false, szof);
return;
}
lint_assert(has_operands(tn));
tnode_t *ln = tn->u.ops.left;
tnode_t *rn = tn->u.ops.right;
check_expr_op(op, ln, szof, fcall, eqwarn);
const mod_t *mp = &modtab[op];
bool cvctx = mp->m_value_context;
bool ccond = mp->m_compares_with_zero;
bool eq = mp->m_warn_if_operand_eq &&
!ln->tn_parenthesized &&
rn != NULL && !rn->tn_parenthesized;
if (op == COLON && tn->tn_type->t_tspec == VOID)
cvctx = ccond = false;
bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
check_expr_misc(ln, cvctx, ccond, eq, is_direct, discard, szof);
switch (op) {
case LOGAND:
case LOGOR:
check_expr_misc(rn, false, true, eq, false, false, szof);
break;
case COLON:
check_expr_misc(rn, cvctx, ccond, eq, false, false, szof);
break;
case COMMA:
check_expr_misc(rn, vctx, cond, false, false, false, szof);
break;
default:
if (mp->m_binary)
check_expr_misc(rn, true, false, eq, false, false,
szof);
break;
}
}
bool
constant_addr(const tnode_t *tn, const sym_t **symp, ptrdiff_t *offsp)
{
const sym_t *sym;
ptrdiff_t offs1, offs2;
tspec_t t, ot;
switch (tn->tn_op) {
case MINUS:
if (tn->u.ops.right->tn_op == CVT)
return constant_addr(tn->u.ops.right, symp, offsp);
if (tn->u.ops.right->tn_op != CON)
return false;
case PLUS:
offs1 = offs2 = 0;
if (tn->u.ops.left->tn_op == CON) {
offs1 = (ptrdiff_t)tn->u.ops.left->u.value.u.integer;
if (!constant_addr(tn->u.ops.right, &sym, &offs2))
return false;
} else if (tn->u.ops.right->tn_op == CON) {
offs2 = (ptrdiff_t)tn->u.ops.right->u.value.u.integer;
if (tn->tn_op == MINUS)
offs2 = -offs2;
if (!constant_addr(tn->u.ops.left, &sym, &offs1))
return false;
} else {
return false;
}
*symp = sym;
*offsp = offs1 + offs2;
return true;
case ADDR:
if (tn->u.ops.left->tn_op == NAME) {
*symp = tn->u.ops.left->u.sym;
*offsp = 0;
return true;
}
*symp = NULL;
*offsp = 0;
return true;
case CVT:
t = tn->tn_type->t_tspec;
ot = tn->u.ops.left->tn_type->t_tspec;
if ((!is_integer(t) && t != PTR) ||
(!is_integer(ot) && ot != PTR)) {
return false;
}
#if 0
else if (psize(t) != psize(ot))
return -1;
#endif
return constant_addr(tn->u.ops.left, symp, offsp);
default:
return false;
}
}
buffer *
cat_strings(buffer *s1, buffer *s2)
{
if ((s1->data != NULL) != (s2->data != NULL)) {
error(292);
return s1;
}
if (s1->data != NULL) {
while (s1->len + s2->len + 1 > s1->cap)
s1->cap *= 2;
s1->data = xrealloc(s1->data, s1->cap);
memcpy(s1->data + s1->len, s2->data, s2->len + 1);
free(s2->data);
}
s1->len += s2->len;
free(s2);
return s1;
}
typedef struct stmt_expr {
memory_pool se_mem;
sym_t *se_sym;
struct stmt_expr *se_enclosing;
} stmt_expr;
static stmt_expr *stmt_exprs;
void
begin_statement_expr(void)
{
debug_enter();
stmt_expr *se = xmalloc(sizeof(*se));
se->se_mem = expr_save_memory();
se->se_sym = NULL;
se->se_enclosing = stmt_exprs;
stmt_exprs = se;
}
void
do_statement_expr(tnode_t *tn)
{
block_level--;
mem_block_level--;
stmt_exprs->se_sym = tn != NULL
? mktempsym(block_dup_type(tn->tn_type))
: NULL;
mem_block_level++;
block_level++;
gnuism(320);
}
tnode_t *
end_statement_expr(void)
{
tnode_t *tn;
stmt_expr *se = stmt_exprs;
if (se->se_sym == NULL) {
tn = NULL;
goto end;
}
tn = build_name(se->se_sym, false);
(void)expr_save_memory();
expr_restore_memory(se->se_mem);
stmt_exprs = se->se_enclosing;
free(se);
end:
debug_leave();
return tn;
}
bool
in_statement_expr(void)
{
return stmt_exprs != NULL;
}