entity.c 22.5 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

Michael Beck's avatar
Michael Beck committed
6
7
8
9
/**
 * @file
 * @brief   Representation of all program known entities.
 * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
10
 */
11
12
#include <stdlib.h>
#include <stddef.h>
13

Michael Beck's avatar
Michael Beck committed
14
15
16
#include "xmalloc.h"
#include "entity_t.h"
#include "array.h"
17
#include "util.h"
Michael Beck's avatar
Michael Beck committed
18
19
20
21
#include "irhooks.h"
#include "irprog_t.h"
#include "ircons.h"
#include "tv_t.h"
22
23
24
#include "irdump.h"
#include "irgraph_t.h"
#include "callgraph.h"
25
#include "error.h"
Götz Lindenmaier's avatar
Götz Lindenmaier committed
26

27
/** The name of the unknown entity. */
28
#define UNKNOWN_ENTITY_NAME "unknown_entity"
29

30
31
32
33
34
ir_entity *get_unknown_entity(void)
{
	return irp->unknown_entity;
}

Matthias Braun's avatar
Matthias Braun committed
35
static ir_entity *intern_new_entity(ir_type *owner, ir_entity_kind kind,
36
                                    ident *name, ir_type *type)
Christian Schäfer's avatar
Christian Schäfer committed
37
{
Matthias Braun's avatar
Matthias Braun committed
38
	assert(owner != NULL);
39

40
	ir_entity *res = XMALLOCZ(ir_entity);
Matthias Braun's avatar
Matthias Braun committed
41
42
43
44
45
46
47
48
49
	res->kind        = k_entity;
	res->name        = name;
	res->type        = type;
	res->owner       = owner;
	res->entity_kind = kind;
	res->volatility  = volatility_non_volatile;
	res->aligned     = align_is_aligned;
	res->usage       = ir_usage_unknown;
	res->visibility  = ir_visibility_external;
50
#ifdef DEBUG_libfirm
Matthias Braun's avatar
Matthias Braun committed
51
	res->nr          = get_irp_new_node_nr();
52
53
54
#endif

	/* Remember entity in its owner. */
55
	if (is_compound_type(owner))
56
57
58
59
60
61
		add_compound_member(owner, res);

	res->visit = 0;
	return res;
}

62
ir_entity *new_entity(ir_type *owner, ident *name, ir_type *type)
63
{
Matthias Braun's avatar
Matthias Braun committed
64
	ir_entity *res;
65
	if (is_Method_type(type)) {
66
		ir_graph *irg = get_const_code_irg();
67
		symconst_symbol sym;
Matthias Braun's avatar
Matthias Braun committed
68
69
70
71
72
73
74
		res          = intern_new_entity(owner, IR_ENTITY_METHOD, name, type);
		sym.entity_p = res;
		ir_node *symconst
			= new_r_SymConst(irg, mode_P_code, sym, symconst_addr_ent);
		set_atomic_ent_value(res, symconst);
		res->linkage                   = IR_LINKAGE_CONSTANT;
		res->attr.mtd_attr.properties  = get_method_additional_properties(type);
Matthias Braun's avatar
Matthias Braun committed
75
76
77
78
		res->attr.mtd_attr.vtable_number = IR_VTABLE_NUM_NOT_SET;
		res->attr.mtd_attr.param_access  = NULL;
		res->attr.mtd_attr.param_weight  = NULL;
		res->attr.mtd_attr.irg           = NULL;
79
	} else if (is_compound_type(owner) && !(owner->flags & tf_segment)) {
80
		res = intern_new_entity(owner, IR_ENTITY_COMPOUND_MEMBER, name, type);
81
		res->attr.compound_member.offset = -1;
Matthias Braun's avatar
Matthias Braun committed
82
	} else {
83
		res = intern_new_entity(owner, IR_ENTITY_NORMAL, name, type);
84
85
	}

86
87
88
	hook_new_entity(res);
	return res;
}
89

90
91
92
93
94
95
static ident *make_parameter_entity_name(size_t pos)
{
	char buf[64];
	snprintf(buf, sizeof(buf), "parameter.%lu", (unsigned long) pos);
	return new_id_from_str(buf);
}
Christian Schäfer's avatar
Christian Schäfer committed
96

97
ir_entity *new_parameter_entity(ir_type *owner, size_t pos, ir_type *type)
98
{
Matthias Braun's avatar
Matthias Braun committed
99
	ident     *name = make_parameter_entity_name(pos);
Matthias Braun's avatar
Matthias Braun committed
100
	ir_entity *res  = intern_new_entity(owner, IR_ENTITY_PARAMETER, name, type);
101
	res->attr.compound_member.offset = -1;
102
	res->attr.parameter.number = pos;
103
104
	hook_new_entity(res);
	return res;
105
}
106

107
ir_entity *new_label_entity(ir_label_t label)
Matthias Braun's avatar
Matthias Braun committed
108
109
110
{
	ident *name = id_unique("label_%u");
	ir_type *global_type = get_glob_type();
Matthias Braun's avatar
Matthias Braun committed
111
112
	ir_entity *res = intern_new_entity(global_type, IR_ENTITY_LABEL, name,
	                                   get_code_type());
Matthias Braun's avatar
Matthias Braun committed
113
114
115
116
117
	res->attr.code_attr.label = label;
	hook_new_entity(res);
	return res;
}

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
ir_entity *new_alias_entity(ir_type *owner, ident *name, ir_entity *aliased,
                            ir_type *type)
{
	ir_entity *res = intern_new_entity(owner, IR_ENTITY_ALIAS, name, type);
	res->attr.alias.aliased = aliased;
	hook_new_entity(res);
	return res;
}

void set_entity_alias(ir_entity *entity, ir_entity *aliased)
{
	assert(get_entity_kind(entity) == IR_ENTITY_ALIAS);
	entity->attr.alias.aliased = aliased;
}

ir_entity *get_entity_alias(const ir_entity *entity)
{
	assert(get_entity_kind(entity) == IR_ENTITY_ALIAS);
	return entity->attr.alias.aliased;
}

139
140
141
/**
 * Free entity attributes.
 */
Matthias Braun's avatar
Matthias Braun committed
142
143
static void free_entity_attrs(ir_entity *ent)
{
144
145
146
	if (ent->overwrites != NULL) {
		DEL_ARR_F(ent->overwrites);
		ent->overwrites = NULL;
147
	}
148
149
150
151
152
	if (ent->overwrittenby != NULL) {
		DEL_ARR_F(ent->overwrittenby);
		ent->overwrittenby = NULL;
	}

Matthias Braun's avatar
Matthias Braun committed
153
154
155
	if (ent->initializer != NULL) {
		/* TODO: free initializers */
	}
156
	if (ent->entity_kind == IR_ENTITY_METHOD) {
157
158
159
160
161
162
163
164
165
		if (ent->attr.mtd_attr.param_access) {
			DEL_ARR_F(ent->attr.mtd_attr.param_access);
			ent->attr.mtd_attr.param_access = NULL;
		}
		if (ent->attr.mtd_attr.param_weight) {
			DEL_ARR_F(ent->attr.mtd_attr.param_weight);
			ent->attr.mtd_attr.param_weight = NULL;
		}
	}
166
}
Christian Schäfer's avatar
Christian Schäfer committed
167

168
169
170
171
172
173
174
/**
 * Creates a deep copy of an entity.
 */
static ir_entity *deep_entity_copy(ir_entity *old)
{
	ir_entity *newe = XMALLOC(ir_entity);

175
	*newe = *old;
Matthias Braun's avatar
Matthias Braun committed
176
177
	if (old->initializer != NULL) {
		/* FIXME: the initializers are NOT copied */
178
179
180
181
182
	} else if (is_method_entity(old)) {
		/* do NOT copy them, reanalyze. This might be the best solution */
		newe->attr.mtd_attr.param_access = NULL;
		newe->attr.mtd_attr.param_weight = NULL;
	}
183
184
	newe->overwrites    = NULL;
	newe->overwrittenby = NULL;
185
186
187
188

#ifdef DEBUG_libfirm
	newe->nr = get_irp_new_node_nr();
#endif
189
	hook_new_entity(newe);
190
191
	return newe;
}
192

193
ir_entity *copy_entity_own(ir_entity *old, ir_type *new_owner)
194
{
195
196
	assert(is_entity(old));
	assert(is_compound_type(new_owner));
197
	assert(get_type_state(new_owner) != layout_fixed);
198
199
200
201
	if (old->owner == new_owner)
		return old;

	/* create a deep copy so we are safe of aliasing and double-freeing. */
Matthias Braun's avatar
Matthias Braun committed
202
	ir_entity *newe = deep_entity_copy(old);
203
	newe->owner = new_owner;
204
	add_compound_member(new_owner, newe);
205

206
	return newe;
207
}
208

209
ir_entity *copy_entity_name(ir_entity *old, ident *new_name)
210
{
Matthias Braun's avatar
Matthias Braun committed
211
	assert(old->kind == k_entity);
212
213
214
	if (old->name == new_name)
		return old;

Matthias Braun's avatar
Matthias Braun committed
215
216
	ir_entity *newe = deep_entity_copy(old);
	newe->name    = new_name;
217
	newe->ld_name = NULL;
218
	add_compound_member(old->owner, newe);
219

220
	return newe;
221
}
222

223
void free_entity(ir_entity *ent)
224
{
225
	if (is_compound_type(ent->owner))
226
		remove_compound_member(ent->owner, ent);
227

Matthias Braun's avatar
Matthias Braun committed
228
	assert(ent->kind == k_entity);
229
	free_entity_attrs(ent);
230
#ifdef DEBUG_libfirm
231
	ent->kind = k_BAD;
232
#endif
233
	free(ent);
234
}
235

236
long get_entity_nr(const ir_entity *ent)
237
{
Matthias Braun's avatar
Matthias Braun committed
238
	assert(ent->kind == k_entity);
239
#ifdef DEBUG_libfirm
240
	return ent->nr;
241
#else
242
	return (long)PTR_TO_INT(ent);
243
#endif
244
}
245

246
247
const char *(get_entity_name)(const ir_entity *ent)
{
248
	return _get_entity_name(ent);
Matthias Braun's avatar
Matthias Braun committed
249
}
Christian Schäfer's avatar
Christian Schäfer committed
250

251
252
ident *(get_entity_ident)(const ir_entity *ent)
{
253
	return _get_entity_ident(ent);
Matthias Braun's avatar
Matthias Braun committed
254
}
Michael Beck's avatar
Michael Beck committed
255

256
257
void (set_entity_ident)(ir_entity *ent, ident *id)
{
258
	_set_entity_ident(ent, id);
Matthias Braun's avatar
Matthias Braun committed
259
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
260

261
262
ir_type *(get_entity_owner)(const ir_entity *ent)
{
263
	return _get_entity_owner(ent);
Matthias Braun's avatar
Matthias Braun committed
264
}
Christian Schäfer's avatar
Christian Schäfer committed
265

266
void set_entity_owner(ir_entity *ent, ir_type *owner)
267
{
268
269
	assert(is_entity(ent));
	assert(is_compound_type(owner));
270
271
272

	remove_compound_member(ent->owner, ent);
	add_compound_member(owner, ent);
273
	ent->owner = owner;
Matthias Braun's avatar
Matthias Braun committed
274
}
Christian Schäfer's avatar
Christian Schäfer committed
275

Matthias Braun's avatar
Matthias Braun committed
276
277
ident *(get_entity_ld_ident)(const ir_entity *ent)
{
278
	return _get_entity_ld_ident(ent);
Matthias Braun's avatar
Matthias Braun committed
279
}
280

281
282
void (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident)
{
283
	_set_entity_ld_ident(ent, ld_ident);
Matthias Braun's avatar
Matthias Braun committed
284
}
Christian Schäfer's avatar
Christian Schäfer committed
285

Matthias Braun's avatar
Matthias Braun committed
286
287
const char *(get_entity_ld_name)(const ir_entity *ent)
{
288
	return _get_entity_ld_name(ent);
Matthias Braun's avatar
Matthias Braun committed
289
}
290

291
292
293
294
295
int entity_has_ld_ident(const ir_entity *entity)
{
	return entity->ld_name != NULL;
}

296
297
ir_type *(get_entity_type)(const ir_entity *ent)
{
298
	return _get_entity_type(ent);
Matthias Braun's avatar
Matthias Braun committed
299
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
300

Matthias Braun's avatar
Matthias Braun committed
301
302
303
304
305
306
307
308
309
310
void set_entity_type(ir_entity *ent, ir_type *type)
{
	switch (ent->entity_kind) {
	case IR_ENTITY_METHOD:
		assert(is_Method_type(type));
		break;
	case IR_ENTITY_NORMAL:
		assert(!is_Method_type(type));
		break;
	case IR_ENTITY_LABEL:
311
		assert(type == get_code_type());
Matthias Braun's avatar
Matthias Braun committed
312
313
314
315
316
		break;
	case IR_ENTITY_COMPOUND_MEMBER:
		break;
	}
	ent->type = type;
Matthias Braun's avatar
Matthias Braun committed
317
}
Götz Lindenmaier's avatar
a    
Götz Lindenmaier committed
318

319
320
ir_volatility (get_entity_volatility)(const ir_entity *ent)
{
321
	return _get_entity_volatility(ent);
Matthias Braun's avatar
Matthias Braun committed
322
}
Götz Lindenmaier's avatar
a    
Götz Lindenmaier committed
323

324
325
void (set_entity_volatility)(ir_entity *ent, ir_volatility vol)
{
326
	_set_entity_volatility(ent, vol);
Matthias Braun's avatar
Matthias Braun committed
327
}
Götz Lindenmaier's avatar
a    
Götz Lindenmaier committed
328

329
const char *get_volatility_name(ir_volatility var)
330
{
331
#define X(a)    case a: return #a
332
333
334
335
	switch (var) {
	X(volatility_non_volatile);
	X(volatility_is_volatile);
	}
336
#undef X
Matthias Braun's avatar
Matthias Braun committed
337
	return "BAD VALUE";
338
}
339

340
341
ir_align (get_entity_aligned)(const ir_entity *ent)
{
Matthias Braun's avatar
Matthias Braun committed
342
343
	return _get_entity_aligned(ent);
}
344

345
346
void (set_entity_aligned)(ir_entity *ent, ir_align a)
{
Matthias Braun's avatar
Matthias Braun committed
347
348
	_set_entity_aligned(ent, a);
}
349

350
351
unsigned (get_entity_alignment)(const ir_entity *ent)
{
352
353
354
	return _get_entity_alignment(ent);
}

355
356
void (set_entity_alignment)(ir_entity *ent, unsigned alignment)
{
357
358
359
	_set_entity_alignment(ent, alignment);
}

Michael Beck's avatar
Michael Beck committed
360
361
362
363
364
365
366
367
const char *get_align_name(ir_align a)
{
#define X(a)    case a: return #a
	switch (a) {
	X(align_non_aligned);
	X(align_is_aligned);
	}
#undef X
Matthias Braun's avatar
Matthias Braun committed
368
	return "BAD VALUE";
369
}
Michael Beck's avatar
Michael Beck committed
370

371
void set_entity_label(ir_entity *ent, ir_label_t label)
372
{
Matthias Braun's avatar
Matthias Braun committed
373
	assert(ent->entity_kind == IR_ENTITY_LABEL);
374
375
376
377
378
	ent->attr.code_attr.label = label;
}

ir_label_t get_entity_label(const ir_entity *ent)
{
Matthias Braun's avatar
Matthias Braun committed
379
	assert(ent->entity_kind == IR_ENTITY_LABEL);
380
381
382
	return ent->attr.code_attr.label;
}

383
384
385
386
387
388
389
void set_entity_visibility(ir_entity *entity, ir_visibility visibility)
{
	entity->visibility = visibility;
}

ir_visibility get_entity_visibility(const ir_entity *entity)
{
390
	return (ir_visibility)entity->visibility;
391
392
}

Matthias Braun's avatar
Matthias Braun committed
393
394
395
396
397
398
399
400
401
void set_entity_linkage(ir_entity *entity, ir_linkage linkage)
{
	entity->linkage = linkage;
}

ir_linkage (get_entity_linkage)(const ir_entity *entity)
{
	return get_entity_linkage(entity);
}
402

Matthias Braun's avatar
Matthias Braun committed
403
404
405
406
void add_entity_linkage(ir_entity *entity, ir_linkage linkage)
{
	entity->linkage |= linkage;
}
407

Matthias Braun's avatar
Matthias Braun committed
408
409
410
411
void remove_entity_linkage(ir_entity *entity, ir_linkage linkage)
{
	entity->linkage &= ~linkage;
}
412

413
414
int (is_entity_compiler_generated)(const ir_entity *ent)
{
Michael Beck's avatar
Michael Beck committed
415
	return _is_entity_compiler_generated(ent);
416
}
417

418
419
void (set_entity_compiler_generated)(ir_entity *ent, int flag)
{
Michael Beck's avatar
Michael Beck committed
420
	_set_entity_compiler_generated(ent, flag);
421
}
422

423
424
ir_entity_usage (get_entity_usage)(const ir_entity *ent)
{
425
426
	return _get_entity_usage(ent);
}
Michael Beck's avatar
Michael Beck committed
427

428
429
void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags)
{
430
431
	_set_entity_usage(ent, flags);
}
432

433
ir_node *get_atomic_ent_value(const ir_entity *entity)
Matthias Braun's avatar
Matthias Braun committed
434
{
Matthias Braun's avatar
Matthias Braun committed
435
	assert(is_atomic_entity(entity));
Matthias Braun's avatar
Matthias Braun committed
436
437

	ir_initializer_t *initializer = get_entity_initializer(entity);
Matthias Braun's avatar
Matthias Braun committed
438
439
440
441
	if (initializer == NULL) {
		ir_type *type = get_entity_type(entity);
		return new_r_Unknown(get_const_code_irg(), get_type_mode(type));
	}
442

Matthias Braun's avatar
Matthias Braun committed
443
444
445
446
447
448
449
	switch (get_initializer_kind(initializer)) {
	case IR_INITIALIZER_NULL: {
		ir_type *type = get_entity_type(entity);
		ir_mode *mode = get_type_mode(type);
		return new_r_Const(get_const_code_irg(), get_mode_null(mode));
	}
	case IR_INITIALIZER_TARVAL: {
Matthias Braun's avatar
Matthias Braun committed
450
		ir_tarval *tv = get_initializer_tarval_value(initializer);
Matthias Braun's avatar
Matthias Braun committed
451
452
453
454
455
456
457
458
		return new_r_Const(get_const_code_irg(), tv);
	}
	case IR_INITIALIZER_CONST:
		return get_initializer_const_value(initializer);
	case IR_INITIALIZER_COMPOUND:
		panic("compound initializer in atomic entity not allowed (%+F)", entity);
	}

459
	panic("invalid initializer kind (%+F)", entity);
Matthias Braun's avatar
Matthias Braun committed
460
461
462
}

void set_atomic_ent_value(ir_entity *entity, ir_node *val)
463
{
Matthias Braun's avatar
Matthias Braun committed
464
465
	assert(is_atomic_entity(entity));
	assert(is_Dummy(val) || get_irn_mode(val) == get_type_mode(entity->type));
Matthias Braun's avatar
Matthias Braun committed
466
	ir_initializer_t *initializer = create_initializer_const(val);
Matthias Braun's avatar
Matthias Braun committed
467
468
	entity->initializer = initializer;
}
469

470
471
472
473
474
475
476
477
478
479
const char *get_initializer_kind_name(ir_initializer_kind_t ini)
{
#define X(a)    case a: return #a
	switch (ini) {
	X(IR_INITIALIZER_CONST);
	X(IR_INITIALIZER_TARVAL);
	X(IR_INITIALIZER_NULL);
	X(IR_INITIALIZER_COMPOUND);
	}
#undef X
Matthias Braun's avatar
Matthias Braun committed
480
	return "BAD VALUE";
481
482
}

483
484
485
486
487
488
489
490
491
492
493
494
static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };

ir_initializer_t *get_initializer_null(void)
{
	return &null_initializer;
}

ir_initializer_t *create_initializer_const(ir_node *value)
{
	struct obstack *obst = get_irg_obstack(get_const_code_irg());

	ir_initializer_t *initializer
495
		= (ir_initializer_t*)OALLOC(obst, ir_initializer_const_t);
496
497
498
499
500
501
	initializer->kind         = IR_INITIALIZER_CONST;
	initializer->consti.value = value;

	return initializer;
}

Matthias Braun's avatar
Matthias Braun committed
502
ir_initializer_t *create_initializer_tarval(ir_tarval *tv)
503
504
505
506
{
	struct obstack *obst = get_irg_obstack(get_const_code_irg());

	ir_initializer_t *initializer
507
		= (ir_initializer_t*)OALLOC(obst, ir_initializer_tarval_t);
508
509
510
511
512
513
	initializer->kind         = IR_INITIALIZER_TARVAL;
	initializer->tarval.value = tv;

	return initializer;
}

514
ir_initializer_t *create_initializer_compound(size_t n_entries)
515
516
517
{
	struct obstack *obst = get_irg_obstack(get_const_code_irg());

Matthias Braun's avatar
Matthias Braun committed
518
519
520
	size_t size = sizeof(ir_initializer_compound_t)
	            + n_entries * sizeof(ir_initializer_t*)
	            - sizeof(ir_initializer_t*);
521

522
523
	ir_initializer_t *initializer
		= (ir_initializer_t*)obstack_alloc(obst, size);
524
525
526
	initializer->kind                    = IR_INITIALIZER_COMPOUND;
	initializer->compound.n_initializers = n_entries;

Matthias Braun's avatar
Matthias Braun committed
527
	for (size_t i = 0; i < n_entries; ++i) {
528
529
530
531
532
533
534
535
536
		initializer->compound.initializers[i] = get_initializer_null();
	}

	return initializer;
}

ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
{
	assert(initializer->kind == IR_INITIALIZER_CONST);
537
	return skip_Id(initializer->consti.value);
538
539
}

Matthias Braun's avatar
Matthias Braun committed
540
ir_tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
541
542
543
544
545
{
	assert(initializer->kind == IR_INITIALIZER_TARVAL);
	return initializer->tarval.value;
}

546
size_t get_initializer_compound_n_entries(const ir_initializer_t *initializer)
547
548
549
550
551
552
{
	assert(initializer->kind == IR_INITIALIZER_COMPOUND);
	return initializer->compound.n_initializers;
}

void set_initializer_compound_value(ir_initializer_t *initializer,
553
                                    size_t index, ir_initializer_t *value)
554
555
556
557
558
559
560
561
{
	assert(initializer->kind == IR_INITIALIZER_COMPOUND);
	assert(index < initializer->compound.n_initializers);

	initializer->compound.initializers[index] = value;
}

ir_initializer_t *get_initializer_compound_value(
562
		const ir_initializer_t *initializer, size_t index)
563
564
565
566
567
568
569
570
571
572
573
574
575
576
{
	assert(initializer->kind == IR_INITIALIZER_COMPOUND);
	assert(index < initializer->compound.n_initializers);

	return initializer->compound.initializers[index];
}

ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
{
	return initializer->kind;
}

static void check_entity_initializer(ir_entity *entity)
{
Matthias Braun's avatar
Matthias Braun committed
577
578
#ifndef NDEBUG
	ir_initializer_t *initializer = entity->initializer;
579
	ir_type          *entity_tp   = get_entity_type(entity);
Matthias Braun's avatar
Matthias Braun committed
580
581
	switch (initializer->kind) {
	case IR_INITIALIZER_COMPOUND:
582
		assert(is_aggregate_type(entity_tp));
Matthias Braun's avatar
Matthias Braun committed
583
584
		break;
	case IR_INITIALIZER_CONST:
585
586
587
		/* methods are initialized by a SymConst */
		assert(is_atomic_type(entity_tp) || is_Method_type(entity_tp));
		break;
Matthias Braun's avatar
Matthias Braun committed
588
	case IR_INITIALIZER_TARVAL:
589
		assert(is_atomic_type(entity_tp));
Matthias Braun's avatar
Matthias Braun committed
590
591
592
593
		break;
	case IR_INITIALIZER_NULL:
		break;
	}
yb9976's avatar
yb9976 committed
594
595
#else
	(void)entity;
Matthias Braun's avatar
Matthias Braun committed
596
#endif
597
598
599
600
}

void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
{
Matthias Braun's avatar
Matthias Braun committed
601
	entity->initializer = initializer;
602
603
604
	check_entity_initializer(entity);
}

605
606
int has_entity_initializer(const ir_entity *entity)
{
Matthias Braun's avatar
Matthias Braun committed
607
	return entity->initializer != NULL;
608
609
}

610
611
ir_initializer_t *get_entity_initializer(const ir_entity *entity)
{
Matthias Braun's avatar
Matthias Braun committed
612
	return entity->initializer;
613
614
}

615
616
int (get_entity_offset)(const ir_entity *ent)
{
617
	return _get_entity_offset(ent);
618
}
Michael Beck's avatar
Michael Beck committed
619

620
621
void (set_entity_offset)(ir_entity *ent, int offset)
{
622
	_set_entity_offset(ent, offset);
623
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
624

625
unsigned (get_entity_bitfield_offset)(const ir_entity *ent)
626
{
627
	return _get_entity_bitfield_offset(ent);
628
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
629

630
void (set_entity_bitfield_offset)(ir_entity *ent, unsigned offset)
631
{
632
633
634
635
636
637
638
639
640
641
642
	_set_entity_bitfield_offset(ent, offset);
}

unsigned (get_entity_bitfield_size)(const ir_entity *ent)
{
	return _get_entity_bitfield_size(ent);
}

void (set_entity_bitfield_size)(ir_entity *ent, unsigned size)
{
	_set_entity_bitfield_size(ent, size);
643
}
Michael Beck's avatar
Michael Beck committed
644

645
646
void add_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
{
647
648
649
	if (ent->overwrites == NULL) {
		ent->overwrites = NEW_ARR_F(ir_entity*, 0);
	}
650
	ARR_APP1(ir_entity *, ent->overwrites, overwritten);
651
652
653
	if (overwritten->overwrittenby == NULL) {
		overwritten->overwrittenby = NEW_ARR_F(ir_entity*, 0);
	}
654
	ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
655
}
656

657
size_t get_entity_n_overwrites(const ir_entity *ent)
658
{
659
660
661
	if (ent->overwrites == NULL)
		return 0;
	return ARR_LEN(ent->overwrites);
662
}
663

664
size_t get_entity_overwrites_index(const ir_entity *ent, ir_entity *overwritten)
665
{
Matthias Braun's avatar
Matthias Braun committed
666
	for (size_t i = 0, n = get_entity_n_overwrites(ent); i < n; ++i) {
667
668
		if (get_entity_overwrites(ent, i) == overwritten)
			return i;
669
	}
670
	return (size_t)-1;
671
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
672

673
ir_entity *get_entity_overwrites(const ir_entity *ent, size_t pos)
674
{
675
676
	assert(pos < get_entity_n_overwrites(ent));
	return ent->overwrites[pos];
677
}
678

679
void set_entity_overwrites(ir_entity *ent, size_t pos, ir_entity *overwritten)
680
{
681
682
	assert(pos < get_entity_n_overwrites(ent));
	ent->overwrites[pos] = overwritten;
683
}
684

685
686
void remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
{
Matthias Braun's avatar
Matthias Braun committed
687
	for (size_t i = 0, n = get_entity_n_overwrites(ent); i < n; ++i) {
688
		if (ent->overwrites[i] == overwritten) {
689
			for (; i < n - 1; i++)
690
				ent->overwrites[i] = ent->overwrites[i+1];
691
			ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
692
693
			break;
		}
694
	}
695
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
696

697

698
size_t get_entity_n_overwrittenby(const ir_entity *ent)
699
{
700
701
	if (ent->overwrittenby == NULL)
		return 0;
702
	return ARR_LEN(ent->overwrittenby);
703
}
704

705
706
size_t get_entity_overwrittenby_index(const ir_entity *ent,
                                      ir_entity *overwrites)
707
{
Matthias Braun's avatar
Matthias Braun committed
708
	for (size_t i = 0, n = get_entity_n_overwrittenby(ent); i < n; ++i) {
709
710
		if (get_entity_overwrittenby(ent, i) == overwrites)
			return i;
711
	}
712
	return (size_t)-1;
713
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
714

715
ir_entity *get_entity_overwrittenby(const ir_entity *ent, size_t pos)
716
{
717
718
	assert(pos < get_entity_n_overwrittenby(ent));
	return ent->overwrittenby[pos];
719
}
720

721
void set_entity_overwrittenby(ir_entity *ent, size_t pos, ir_entity *overwrites)
722
{
723
724
	assert(pos < get_entity_n_overwrittenby(ent));
	ent->overwrittenby[pos] = overwrites;
725
}
726

727
728
void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites)
{
Matthias Braun's avatar
Matthias Braun committed
729
	for (size_t i = 0, n = get_entity_n_overwrittenby(ent); i < n; ++i) {
730
		if (ent->overwrittenby[i] == overwrites) {
731
			for (; i < n - 1; ++i)
732
				ent->overwrittenby[i] = ent->overwrittenby[i+1];
733
			ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
734
735
			break;
		}
736
	}
737
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
738

739
740
void *(get_entity_link)(const ir_entity *ent)
{
741
	return _get_entity_link(ent);
742
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
743

744
745
void (set_entity_link)(ir_entity *ent, void *l)
{
746
	_set_entity_link(ent, l);
747
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
748

749
750
ir_graph *(get_entity_irg)(const ir_entity *ent)
{
751
	return _get_entity_irg(ent);
752
}
Christian Schäfer's avatar
Christian Schäfer committed
753

754
755
void set_entity_irg(ir_entity *ent, ir_graph *irg)
{
756
	assert(is_method_entity(ent));
Matthias Braun's avatar
Matthias Braun committed
757
	assert(get_entity_peculiarity(ent) == peculiarity_existent);
758
	ent->attr.mtd_attr.irg = irg;
759
}
760

761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
int (is_parameter_entity)(const ir_entity *entity)
{
	return _is_parameter_entity(entity);
}

size_t (get_entity_parameter_number)(const ir_entity *entity)
{
	return _get_entity_parameter_number(entity);
}

void set_entity_parameter_number(ir_entity *entity, size_t n)
{
	assert(is_parameter_entity(entity));
	entity->attr.parameter.number = n;
}

777
778
unsigned get_entity_vtable_number(const ir_entity *ent)
{
779
780
	assert(is_method_entity((ir_entity *)ent));
	return ent->attr.mtd_attr.vtable_number;
781
}
782

783
784
void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number)
{
785
786
	assert(is_method_entity(ent));
	ent->attr.mtd_attr.vtable_number = vtable_number;
787
}
788

789
790
791
792
793
int is_unknown_entity(const ir_entity *entity)
{
	return entity->entity_kind == IR_ENTITY_UNKNOWN;
}

794
795
int (is_entity)(const void *thing)
{
796
	return _is_entity(thing);
797
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
798

Matthias Braun's avatar
Matthias Braun committed
799
int is_atomic_entity(const ir_entity *ent)
800
{
Matthias Braun's avatar
Matthias Braun committed
801
	ir_type     *t  = get_entity_type(ent);
802
	const tp_op *op = get_type_tpop(t);
803
804
	return op == type_primitive || op == type_pointer
	    || op == type_method;
805
}
806

Matthias Braun's avatar
Matthias Braun committed
807
int is_compound_entity(const ir_entity *ent)
808
{
809
810
811
	ir_type     *t  = get_entity_type(ent);
	const tp_op *op = get_type_tpop(t);
	return (op == type_class || op == type_struct ||
812
	        op == type_array || op == type_union);
813
}
814

Matthias Braun's avatar
Matthias Braun committed
815
int is_method_entity(const ir_entity *ent)
816
{
817
818
	ir_type *t = get_entity_type(ent);
	return is_Method_type(t);
819
}
820

Matthias Braun's avatar
Matthias Braun committed
821
ir_visited_t (get_entity_visited)(const ir_entity *ent)
822
{
823
	return _get_entity_visited(ent);
824
}
Michael Beck's avatar
Michael Beck committed
825

826
827
void (set_entity_visited)(ir_entity *ent, ir_visited_t num)
{
828
	_set_entity_visited(ent, num);
829
}
Michael Beck's avatar
Michael Beck committed
830

831
832
void (mark_entity_visited)(ir_entity *ent)
{
833
	_mark_entity_visited(ent);
834
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
835

Matthias Braun's avatar
Matthias Braun committed
836
int (entity_visited)(const ir_entity *ent)
837
{
838
	return _entity_visited(ent);
839
}
840

Matthias Braun's avatar
Matthias Braun committed
841
int (entity_not_visited)(const ir_entity *ent)
842
{
843
	return _entity_not_visited(ent);
844
}
845

846
mtp_additional_properties get_entity_additional_properties(const ir_entity *ent)
847
{
848
	assert(is_method_entity(ent));
Matthias Braun's avatar
Matthias Braun committed
849
	return ent->attr.mtd_attr.properties;
850
}
851