entity.c 21.9 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;
}

35
36
37
/*-----------------------------------------------------------------*/
/* ENTITY                                                          */
/*-----------------------------------------------------------------*/
38

Matthias Braun's avatar
Matthias Braun committed
39
static ir_entity *intern_new_entity(ir_type *owner, ir_entity_kind kind,
40
                                    ident *name, ir_type *type)
Christian Schäfer's avatar
Christian Schäfer committed
41
{
Matthias Braun's avatar
Matthias Braun committed
42
	assert(owner != NULL);
43

44
	ir_entity *res = XMALLOCZ(ir_entity);
45
46
47
48
49
	res->kind    = k_entity;
	res->name    = name;
	res->type    = type;
	res->owner   = owner;

Matthias Braun's avatar
Matthias Braun committed
50
	res->entity_kind          = kind;
51
	res->volatility           = volatility_non_volatile;
Matthias Braun's avatar
Matthias Braun committed
52
	res->aligned              = align_is_aligned;
53
	res->usage                = ir_usage_unknown;
Matthias Braun's avatar
Matthias Braun committed
54
	res->visibility           = ir_visibility_external;
55
56
57
58
59
#ifdef DEBUG_libfirm
	res->nr = get_irp_new_node_nr();
#endif

	/* Remember entity in its owner. */
60
	if (is_compound_type(owner))
61
62
63
64
65
66
		add_compound_member(owner, res);

	res->visit = 0;
	return res;
}

67
ir_entity *new_entity(ir_type *owner, ident *name, ir_type *type)
68
{
Matthias Braun's avatar
Matthias Braun committed
69
	ir_entity *res;
70
	if (is_Method_type(type)) {
71
		ir_graph *irg = get_const_code_irg();
72
		symconst_symbol sym;
73
		res = intern_new_entity(owner, IR_ENTITY_METHOD, name, type);
74
		sym.entity_p            = res;
Matthias Braun's avatar
Matthias Braun committed
75
76
77
78
79
80
81
		set_atomic_ent_value(res, new_r_SymConst(irg, mode_P_code, sym, symconst_addr_ent));
		res->linkage                     = IR_LINKAGE_CONSTANT;
		res->attr.mtd_attr.properties    = get_method_additional_properties(type);
		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;
82
	} else if (is_compound_type(owner) && !(owner->flags & tf_segment)) {
83
		res = intern_new_entity(owner, IR_ENTITY_COMPOUND_MEMBER, name, type);
84
		res->attr.compound_member.offset = -1;
Matthias Braun's avatar
Matthias Braun committed
85
	} else {
86
		res = intern_new_entity(owner, IR_ENTITY_NORMAL, name, type);
87
88
	}

89
90
91
	hook_new_entity(res);
	return res;
}
92

93
94
95
96
97
98
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
99

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

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

121
122
123
124
125
/**
 * Free entity attributes.
 *
 * @param ent  the entity
 */
Matthias Braun's avatar
Matthias Braun committed
126
127
static void free_entity_attrs(ir_entity *ent)
{
128
129
130
	if (ent->overwrites != NULL) {
		DEL_ARR_F(ent->overwrites);
		ent->overwrites = NULL;
131
	}
132
133
134
135
136
	if (ent->overwrittenby != NULL) {
		DEL_ARR_F(ent->overwrittenby);
		ent->overwrittenby = NULL;
	}

Matthias Braun's avatar
Matthias Braun committed
137
138
139
	if (ent->initializer != NULL) {
		/* TODO: free initializers */
	}
140
	if (ent->entity_kind == IR_ENTITY_METHOD) {
141
142
143
144
145
146
147
148
149
		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;
		}
	}
150
}
Christian Schäfer's avatar
Christian Schäfer committed
151

152
153
154
155
156
157
158
/**
 * Creates a deep copy of an entity.
 */
static ir_entity *deep_entity_copy(ir_entity *old)
{
	ir_entity *newe = XMALLOC(ir_entity);

159
	*newe = *old;
Matthias Braun's avatar
Matthias Braun committed
160
161
	if (old->initializer != NULL) {
		/* FIXME: the initializers are NOT copied */
162
163
164
165
166
	} 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;
	}
167
168
	newe->overwrites    = NULL;
	newe->overwrittenby = NULL;
169
170
171
172

#ifdef DEBUG_libfirm
	newe->nr = get_irp_new_node_nr();
#endif
173
	hook_new_entity(newe);
174
175
	return newe;
}
176

177
ir_entity *copy_entity_own(ir_entity *old, ir_type *new_owner)
178
{
179
180
	assert(is_entity(old));
	assert(is_compound_type(new_owner));
181
	assert(get_type_state(new_owner) != layout_fixed);
182
183
184
185
	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
186
	ir_entity *newe = deep_entity_copy(old);
187
	newe->owner = new_owner;
188
	add_compound_member(new_owner, newe);
189

190
	return newe;
191
}
192

193
ir_entity *copy_entity_name(ir_entity *old, ident *new_name)
194
{
Matthias Braun's avatar
Matthias Braun committed
195
	assert(old->kind == k_entity);
196
197
198
	if (old->name == new_name)
		return old;

Matthias Braun's avatar
Matthias Braun committed
199
200
	ir_entity *newe = deep_entity_copy(old);
	newe->name    = new_name;
201
	newe->ld_name = NULL;
202
	add_compound_member(old->owner, newe);
203

204
	return newe;
205
}
206

207
void free_entity(ir_entity *ent)
208
{
209
	if (is_compound_type(ent->owner))
210
		remove_compound_member(ent->owner, ent);
211

Matthias Braun's avatar
Matthias Braun committed
212
	assert(ent->kind == k_entity);
213
	free_entity_attrs(ent);
214
#ifdef DEBUG_libfirm
215
	ent->kind = k_BAD;
216
#endif
217
	free(ent);
218
}
219

220
long get_entity_nr(const ir_entity *ent)
221
{
Matthias Braun's avatar
Matthias Braun committed
222
	assert(ent->kind == k_entity);
223
#ifdef DEBUG_libfirm
224
	return ent->nr;
225
#else
226
	return (long)PTR_TO_INT(ent);
227
#endif
228
}
229

230
231
const char *(get_entity_name)(const ir_entity *ent)
{
232
	return _get_entity_name(ent);
Matthias Braun's avatar
Matthias Braun committed
233
}
Christian Schäfer's avatar
Christian Schäfer committed
234

235
236
ident *(get_entity_ident)(const ir_entity *ent)
{
237
	return _get_entity_ident(ent);
Matthias Braun's avatar
Matthias Braun committed
238
}
Michael Beck's avatar
Michael Beck committed
239

240
241
void (set_entity_ident)(ir_entity *ent, ident *id)
{
242
	_set_entity_ident(ent, id);
Matthias Braun's avatar
Matthias Braun committed
243
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
244

245
246
ir_type *(get_entity_owner)(const ir_entity *ent)
{
247
	return _get_entity_owner(ent);
Matthias Braun's avatar
Matthias Braun committed
248
}
Christian Schäfer's avatar
Christian Schäfer committed
249

250
void set_entity_owner(ir_entity *ent, ir_type *owner)
251
{
252
253
	assert(is_entity(ent));
	assert(is_compound_type(owner));
254
255
256

	remove_compound_member(ent->owner, ent);
	add_compound_member(owner, ent);
257
	ent->owner = owner;
Matthias Braun's avatar
Matthias Braun committed
258
}
Christian Schäfer's avatar
Christian Schäfer committed
259

Matthias Braun's avatar
Matthias Braun committed
260
261
ident *(get_entity_ld_ident)(const ir_entity *ent)
{
262
	return _get_entity_ld_ident(ent);
Matthias Braun's avatar
Matthias Braun committed
263
}
264

265
266
void (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident)
{
267
	_set_entity_ld_ident(ent, ld_ident);
Matthias Braun's avatar
Matthias Braun committed
268
}
Christian Schäfer's avatar
Christian Schäfer committed
269

Matthias Braun's avatar
Matthias Braun committed
270
271
const char *(get_entity_ld_name)(const ir_entity *ent)
{
272
	return _get_entity_ld_name(ent);
Matthias Braun's avatar
Matthias Braun committed
273
}
274

275
276
277
278
279
int entity_has_ld_ident(const ir_entity *entity)
{
	return entity->ld_name != NULL;
}

280
281
ir_type *(get_entity_type)(const ir_entity *ent)
{
282
	return _get_entity_type(ent);
Matthias Braun's avatar
Matthias Braun committed
283
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
284

Matthias Braun's avatar
Matthias Braun committed
285
286
287
288
289
290
291
292
293
294
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:
295
		assert(type == get_code_type());
Matthias Braun's avatar
Matthias Braun committed
296
297
298
299
300
		break;
	case IR_ENTITY_COMPOUND_MEMBER:
		break;
	}
	ent->type = type;
Matthias Braun's avatar
Matthias Braun committed
301
}
Götz Lindenmaier's avatar
a    
Götz Lindenmaier committed
302

303
304
ir_volatility (get_entity_volatility)(const ir_entity *ent)
{
305
	return _get_entity_volatility(ent);
Matthias Braun's avatar
Matthias Braun committed
306
}
Götz Lindenmaier's avatar
a    
Götz Lindenmaier committed
307

308
309
void (set_entity_volatility)(ir_entity *ent, ir_volatility vol)
{
310
	_set_entity_volatility(ent, vol);
Matthias Braun's avatar
Matthias Braun committed
311
}
Götz Lindenmaier's avatar
a    
Götz Lindenmaier committed
312

313
const char *get_volatility_name(ir_volatility var)
314
{
315
#define X(a)    case a: return #a
316
317
318
319
	switch (var) {
	X(volatility_non_volatile);
	X(volatility_is_volatile);
	}
320
#undef X
Matthias Braun's avatar
Matthias Braun committed
321
	return "BAD VALUE";
322
}
323

324
325
ir_align (get_entity_aligned)(const ir_entity *ent)
{
Matthias Braun's avatar
Matthias Braun committed
326
327
	return _get_entity_aligned(ent);
}
328

329
330
void (set_entity_aligned)(ir_entity *ent, ir_align a)
{
Matthias Braun's avatar
Matthias Braun committed
331
332
	_set_entity_aligned(ent, a);
}
333

334
335
unsigned (get_entity_alignment)(const ir_entity *ent)
{
336
337
338
	return _get_entity_alignment(ent);
}

339
340
void (set_entity_alignment)(ir_entity *ent, unsigned alignment)
{
341
342
343
	_set_entity_alignment(ent, alignment);
}

Michael Beck's avatar
Michael Beck committed
344
345
346
347
348
349
350
351
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
352
	return "BAD VALUE";
353
}
Michael Beck's avatar
Michael Beck committed
354

355
void set_entity_label(ir_entity *ent, ir_label_t label)
356
{
Matthias Braun's avatar
Matthias Braun committed
357
	assert(ent->entity_kind == IR_ENTITY_LABEL);
358
359
360
361
362
	ent->attr.code_attr.label = label;
}

ir_label_t get_entity_label(const ir_entity *ent)
{
Matthias Braun's avatar
Matthias Braun committed
363
	assert(ent->entity_kind == IR_ENTITY_LABEL);
364
365
366
	return ent->attr.code_attr.label;
}

367
368
369
370
371
372
373
void set_entity_visibility(ir_entity *entity, ir_visibility visibility)
{
	entity->visibility = visibility;
}

ir_visibility get_entity_visibility(const ir_entity *entity)
{
374
	return (ir_visibility)entity->visibility;
375
376
}

Matthias Braun's avatar
Matthias Braun committed
377
378
379
380
381
382
383
384
385
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);
}
386

Matthias Braun's avatar
Matthias Braun committed
387
388
389
390
void add_entity_linkage(ir_entity *entity, ir_linkage linkage)
{
	entity->linkage |= linkage;
}
391

Matthias Braun's avatar
Matthias Braun committed
392
393
394
395
void remove_entity_linkage(ir_entity *entity, ir_linkage linkage)
{
	entity->linkage &= ~linkage;
}
396

397
398
int (is_entity_compiler_generated)(const ir_entity *ent)
{
Michael Beck's avatar
Michael Beck committed
399
	return _is_entity_compiler_generated(ent);
400
}
401

402
403
void (set_entity_compiler_generated)(ir_entity *ent, int flag)
{
Michael Beck's avatar
Michael Beck committed
404
	_set_entity_compiler_generated(ent, flag);
405
}
406

407
408
ir_entity_usage (get_entity_usage)(const ir_entity *ent)
{
409
410
	return _get_entity_usage(ent);
}
Michael Beck's avatar
Michael Beck committed
411

412
413
void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags)
{
414
415
	_set_entity_usage(ent, flags);
}
416

417
ir_node *get_atomic_ent_value(const ir_entity *entity)
Matthias Braun's avatar
Matthias Braun committed
418
419
{
	ir_initializer_t *initializer = get_entity_initializer(entity);
420

Matthias Braun's avatar
Matthias Braun committed
421
	assert(is_atomic_entity(entity));
Matthias Braun's avatar
Matthias Braun committed
422
423
424
425
	if (initializer == NULL) {
		ir_type *type = get_entity_type(entity);
		return new_r_Unknown(get_const_code_irg(), get_type_mode(type));
	}
426

Matthias Braun's avatar
Matthias Braun committed
427
428
429
430
431
432
433
	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
434
		ir_tarval *tv = get_initializer_tarval_value(initializer);
Matthias Braun's avatar
Matthias Braun committed
435
436
437
438
439
440
441
442
		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);
	}

443
	panic("invalid initializer kind (%+F)", entity);
Matthias Braun's avatar
Matthias Braun committed
444
445
446
}

void set_atomic_ent_value(ir_entity *entity, ir_node *val)
447
{
Matthias Braun's avatar
Matthias Braun committed
448
449
	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
450
	ir_initializer_t *initializer = create_initializer_const(val);
Matthias Braun's avatar
Matthias Braun committed
451
452
	entity->initializer = initializer;
}
453

454
455
456
457
458
459
460
461
462
463
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
464
	return "BAD VALUE";
465
466
}

467
468
469
470
471
472
473
474
475
476
477
478
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
479
		= (ir_initializer_t*)OALLOC(obst, ir_initializer_const_t);
480
481
482
483
484
485
	initializer->kind         = IR_INITIALIZER_CONST;
	initializer->consti.value = value;

	return initializer;
}

Matthias Braun's avatar
Matthias Braun committed
486
ir_initializer_t *create_initializer_tarval(ir_tarval *tv)
487
488
489
490
{
	struct obstack *obst = get_irg_obstack(get_const_code_irg());

	ir_initializer_t *initializer
491
		= (ir_initializer_t*)OALLOC(obst, ir_initializer_tarval_t);
492
493
494
495
496
497
	initializer->kind         = IR_INITIALIZER_TARVAL;
	initializer->tarval.value = tv;

	return initializer;
}

498
ir_initializer_t *create_initializer_compound(size_t n_entries)
499
500
501
{
	struct obstack *obst = get_irg_obstack(get_const_code_irg());

Matthias Braun's avatar
Matthias Braun committed
502
503
504
	size_t size = sizeof(ir_initializer_compound_t)
	            + n_entries * sizeof(ir_initializer_t*)
	            - sizeof(ir_initializer_t*);
505

506
507
	ir_initializer_t *initializer
		= (ir_initializer_t*)obstack_alloc(obst, size);
508
509
510
	initializer->kind                    = IR_INITIALIZER_COMPOUND;
	initializer->compound.n_initializers = n_entries;

Matthias Braun's avatar
Matthias Braun committed
511
	for (size_t i = 0; i < n_entries; ++i) {
512
513
514
515
516
517
518
519
520
		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);
521
	return skip_Id(initializer->consti.value);
522
523
}

Matthias Braun's avatar
Matthias Braun committed
524
ir_tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
525
526
527
528
529
{
	assert(initializer->kind == IR_INITIALIZER_TARVAL);
	return initializer->tarval.value;
}

530
size_t get_initializer_compound_n_entries(const ir_initializer_t *initializer)
531
532
533
534
535
536
{
	assert(initializer->kind == IR_INITIALIZER_COMPOUND);
	return initializer->compound.n_initializers;
}

void set_initializer_compound_value(ir_initializer_t *initializer,
537
                                    size_t index, ir_initializer_t *value)
538
539
540
541
542
543
544
545
{
	assert(initializer->kind == IR_INITIALIZER_COMPOUND);
	assert(index < initializer->compound.n_initializers);

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

ir_initializer_t *get_initializer_compound_value(
546
		const ir_initializer_t *initializer, size_t index)
547
548
549
550
551
552
553
554
555
556
557
558
559
560
{
	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
561
562
#ifndef NDEBUG
	ir_initializer_t *initializer = entity->initializer;
563
	ir_type          *entity_tp   = get_entity_type(entity);
Matthias Braun's avatar
Matthias Braun committed
564
565
	switch (initializer->kind) {
	case IR_INITIALIZER_COMPOUND:
566
		assert(is_aggregate_type(entity_tp));
Matthias Braun's avatar
Matthias Braun committed
567
568
		break;
	case IR_INITIALIZER_CONST:
569
570
571
		/* 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
572
	case IR_INITIALIZER_TARVAL:
573
		assert(is_atomic_type(entity_tp));
Matthias Braun's avatar
Matthias Braun committed
574
575
576
577
		break;
	case IR_INITIALIZER_NULL:
		break;
	}
yb9976's avatar
yb9976 committed
578
579
#else
	(void)entity;
Matthias Braun's avatar
Matthias Braun committed
580
#endif
581
582
583
584
}

void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
{
Matthias Braun's avatar
Matthias Braun committed
585
	entity->initializer = initializer;
586
587
588
	check_entity_initializer(entity);
}

589
590
int has_entity_initializer(const ir_entity *entity)
{
Matthias Braun's avatar
Matthias Braun committed
591
	return entity->initializer != NULL;
592
593
}

594
595
ir_initializer_t *get_entity_initializer(const ir_entity *entity)
{
Matthias Braun's avatar
Matthias Braun committed
596
	return entity->initializer;
597
598
}

599
600
int (get_entity_offset)(const ir_entity *ent)
{
601
	return _get_entity_offset(ent);
602
}
Michael Beck's avatar
Michael Beck committed
603

604
605
void (set_entity_offset)(ir_entity *ent, int offset)
{
606
	_set_entity_offset(ent, offset);
607
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
608

609
unsigned (get_entity_bitfield_offset)(const ir_entity *ent)
610
{
611
	return _get_entity_bitfield_offset(ent);
612
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
613

614
void (set_entity_bitfield_offset)(ir_entity *ent, unsigned offset)
615
{
616
617
618
619
620
621
622
623
624
625
626
	_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);
627
}
Michael Beck's avatar
Michael Beck committed
628

629
630
void add_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
{
631
632
633
	if (ent->overwrites == NULL) {
		ent->overwrites = NEW_ARR_F(ir_entity*, 0);
	}
634
	ARR_APP1(ir_entity *, ent->overwrites, overwritten);
635
636
637
	if (overwritten->overwrittenby == NULL) {
		overwritten->overwrittenby = NEW_ARR_F(ir_entity*, 0);
	}
638
	ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
639
}
640

641
size_t get_entity_n_overwrites(const ir_entity *ent)
642
{
643
644
645
	if (ent->overwrites == NULL)
		return 0;
	return ARR_LEN(ent->overwrites);
646
}
647

648
size_t get_entity_overwrites_index(const ir_entity *ent, ir_entity *overwritten)
649
{
Matthias Braun's avatar
Matthias Braun committed
650
	for (size_t i = 0, n = get_entity_n_overwrites(ent); i < n; ++i) {
651
652
		if (get_entity_overwrites(ent, i) == overwritten)
			return i;
653
	}
654
	return (size_t)-1;
655
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
656

657
ir_entity *get_entity_overwrites(const ir_entity *ent, size_t pos)
658
{
659
660
	assert(pos < get_entity_n_overwrites(ent));
	return ent->overwrites[pos];
661
}
662

663
void set_entity_overwrites(ir_entity *ent, size_t pos, ir_entity *overwritten)
664
{
665
666
	assert(pos < get_entity_n_overwrites(ent));
	ent->overwrites[pos] = overwritten;
667
}
668

669
670
void remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
{
Matthias Braun's avatar
Matthias Braun committed
671
	for (size_t i = 0, n = get_entity_n_overwrites(ent); i < n; ++i) {
672
		if (ent->overwrites[i] == overwritten) {
673
			for (; i < n - 1; i++)
674
				ent->overwrites[i] = ent->overwrites[i+1];
675
			ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
676
677
			break;
		}
678
	}
679
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
680

681

682
size_t get_entity_n_overwrittenby(const ir_entity *ent)
683
{
684
685
	if (ent->overwrittenby == NULL)
		return 0;
686
	return ARR_LEN(ent->overwrittenby);
687
}
688

689
690
size_t get_entity_overwrittenby_index(const ir_entity *ent,
                                      ir_entity *overwrites)
691
{
Matthias Braun's avatar
Matthias Braun committed
692
	for (size_t i = 0, n = get_entity_n_overwrittenby(ent); i < n; ++i) {
693
694
		if (get_entity_overwrittenby(ent, i) == overwrites)
			return i;
695
	}
696
	return (size_t)-1;
697
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
698

699
ir_entity *get_entity_overwrittenby(const ir_entity *ent, size_t pos)
700
{
701
702
	assert(pos < get_entity_n_overwrittenby(ent));
	return ent->overwrittenby[pos];
703
}
704

705
void set_entity_overwrittenby(ir_entity *ent, size_t pos, ir_entity *overwrites)
706
{
707
708
	assert(pos < get_entity_n_overwrittenby(ent));
	ent->overwrittenby[pos] = overwrites;
709
}
710

711
712
void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites)
{
Matthias Braun's avatar
Matthias Braun committed
713
	for (size_t i = 0, n = get_entity_n_overwrittenby(ent); i < n; ++i) {
714
		if (ent->overwrittenby[i] == overwrites) {
715
			for (; i < n - 1; ++i)
716
				ent->overwrittenby[i] = ent->overwrittenby[i+1];
717
			ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
718
719
			break;
		}
720
	}
721
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
722

723
724
void *(get_entity_link)(const ir_entity *ent)
{
725
	return _get_entity_link(ent);
726
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
727

728
729
void (set_entity_link)(ir_entity *ent, void *l)
{
730
	_set_entity_link(ent, l);
731
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
732

733
734
ir_graph *(get_entity_irg)(const ir_entity *ent)
{
735
	return _get_entity_irg(ent);
736
}
Christian Schäfer's avatar
Christian Schäfer committed
737

738
739
void set_entity_irg(ir_entity *ent, ir_graph *irg)
{
740
	assert(is_method_entity(ent));
Matthias Braun's avatar
Matthias Braun committed
741
	assert(get_entity_peculiarity(ent) == peculiarity_existent);
742
	ent->attr.mtd_attr.irg = irg;
743
}
744

745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
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;
}

761
762
unsigned get_entity_vtable_number(const ir_entity *ent)
{
763
764
	assert(is_method_entity((ir_entity *)ent));
	return ent->attr.mtd_attr.vtable_number;
765
}
766

767
768
void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number)
{
769
770
	assert(is_method_entity(ent));
	ent->attr.mtd_attr.vtable_number = vtable_number;
771
}
772

773
774
775
776
777
int is_unknown_entity(const ir_entity *entity)
{
	return entity->entity_kind == IR_ENTITY_UNKNOWN;
}

778
779
int (is_entity)(const void *thing)
{
780
	return _is_entity(thing);
781
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
782

Matthias Braun's avatar
Matthias Braun committed
783
int is_atomic_entity(const ir_entity *ent)
784
{
Matthias Braun's avatar
Matthias Braun committed
785
	ir_type     *t  = get_entity_type(ent);
786
	const tp_op *op = get_type_tpop(t);
787
788
	return op == type_primitive || op == type_pointer
	    || op == type_method;
789
}
790

Matthias Braun's avatar
Matthias Braun committed
791
int is_compound_entity(const ir_entity *ent)
792
{
793
794
795
	ir_type     *t  = get_entity_type(ent);
	const tp_op *op = get_type_tpop(t);
	return (op == type_class || op == type_struct ||
796
	        op == type_array || op == type_union);
797
}
798

Matthias Braun's avatar
Matthias Braun committed
799
int is_method_entity(const ir_entity *ent)
800
{
801
802
	ir_type *t = get_entity_type(ent);
	return is_Method_type(t);
803
}
804

Matthias Braun's avatar
Matthias Braun committed
805
ir_visited_t (get_entity_visited)(const ir_entity *ent)
806
{
807
	return _get_entity_visited(ent);
808
}
Michael Beck's avatar
Michael Beck committed
809

810
811
void (set_entity_visited)(ir_entity *ent, ir_visited_t num)
{
812
	_set_entity_visited(ent, num);
813
}
Michael Beck's avatar
Michael Beck committed
814

815
816
void (mark_entity_visited)(ir_entity *ent)
{
817
	_mark_entity_visited(ent);
818
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
819

Matthias Braun's avatar
Matthias Braun committed
820
int (entity_visited)(const ir_entity *ent)
821
{
822
	return _entity_visited(ent);
823
}
824

Matthias Braun's avatar
Matthias Braun committed
825
int (entity_not_visited)(const ir_entity *ent)
826
{
827
	return _entity_not_visited(ent);
828
}
829

830
mtp_additional_properties get_entity_additional_properties(const ir_entity *ent)
831
{
832
	assert(is_method_entity(ent));
Matthias Braun's avatar
Matthias Braun committed
833
	return ent->attr.mtd_attr.properties;
834
}