entity.c 21.6 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
#include <stdlib.h>
12

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

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

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

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

39
	ir_entity *res = XMALLOCZ(ir_entity);
Matthias Braun's avatar
Matthias Braun committed
40
41
42
43
44
45
46
47
48
	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;
49
#ifdef DEBUG_libfirm
Matthias Braun's avatar
Matthias Braun committed
50
	res->nr          = get_irp_new_node_nr();
51
52
53
#endif

	/* Remember entity in its owner. */
54
	add_compound_member(owner, res);
55
56
57
58
59

	res->visit = 0;
	return res;
}

60
ir_entity *new_entity(ir_type *owner, ident *name, ir_type *type)
61
{
Matthias Braun's avatar
Matthias Braun committed
62
	ir_entity *res;
63
	if (is_Method_type(type)) {
64
65
66
		res = intern_new_entity(owner, IR_ENTITY_METHOD, name, type);
		res->linkage                     = IR_LINKAGE_CONSTANT;
		res->attr.mtd_attr.properties    = get_method_additional_properties(type);
Matthias Braun's avatar
Matthias Braun committed
67
68
69
70
		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;
71
	} else if (is_compound_type(owner) && !(owner->flags & tf_segment)) {
72
		res = intern_new_entity(owner, IR_ENTITY_COMPOUND_MEMBER, name, type);
73
		res->attr.compound_member.offset = -1;
Matthias Braun's avatar
Matthias Braun committed
74
	} else {
75
		res = intern_new_entity(owner, IR_ENTITY_NORMAL, name, type);
76
77
	}

78
79
80
	hook_new_entity(res);
	return res;
}
81

82
ir_entity *new_parameter_entity(ir_type *owner, size_t pos, ir_type *type)
83
{
84
	ident     *name = new_id_fmt("parameter.%lu", (unsigned long)pos);
Matthias Braun's avatar
Matthias Braun committed
85
	ir_entity *res  = intern_new_entity(owner, IR_ENTITY_PARAMETER, name, type);
86
	res->attr.compound_member.offset = -1;
87
	res->attr.parameter.number = pos;
88
89
	hook_new_entity(res);
	return res;
90
}
91

92
ir_entity *new_label_entity(ir_label_t label)
Matthias Braun's avatar
Matthias Braun committed
93
94
95
{
	ident *name = id_unique("label_%u");
	ir_type *global_type = get_glob_type();
Matthias Braun's avatar
Matthias Braun committed
96
97
	ir_entity *res = intern_new_entity(global_type, IR_ENTITY_LABEL, name,
	                                   get_code_type());
Matthias Braun's avatar
Matthias Braun committed
98
99
100
101
102
	res->attr.code_attr.label = label;
	hook_new_entity(res);
	return res;
}

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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;
}

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

138
139
	/* 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;
160
161
	/* FIXME: the initializers are NOT copied */
	if (is_method_entity(old)) {
162
163
164
165
		/* 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;
	}
166
167
	newe->overwrites    = NULL;
	newe->overwrittenby = NULL;
168
169
170
171

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

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

189
	return newe;
190
}
191

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

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

203
	return newe;
204
}
205

206
void free_entity(ir_entity *ent)
207
{
208
	remove_compound_member(ent->owner, ent);
209

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

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

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

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

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

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

248
void set_entity_owner(ir_entity *ent, ir_type *owner)
249
{
250
	assert(ent->kind == k_entity);
251
	assert(is_compound_type(owner));
252
253
254

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

418
ir_node *get_atomic_ent_value(const ir_entity *entity)
Matthias Braun's avatar
Matthias Braun committed
419
{
Matthias Braun's avatar
Matthias Braun committed
420
	assert(is_atomic_entity(entity));
Matthias Braun's avatar
Matthias Braun committed
421
422

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

Matthias Braun's avatar
Matthias Braun committed
428
429
430
431
	switch (get_initializer_kind(initializer)) {
	case IR_INITIALIZER_NULL: {
		ir_type *type = get_entity_type(entity);
		ir_mode *mode = get_type_mode(type);
432
		return new_r_Const_null(get_const_code_irg(), mode);
Matthias Braun's avatar
Matthias Braun committed
433
434
	}
	case IR_INITIALIZER_TARVAL: {
Matthias Braun's avatar
Matthias Braun committed
435
		ir_tarval *tv = get_initializer_tarval_value(initializer);
Matthias Braun's avatar
Matthias Braun committed
436
437
438
439
440
441
442
443
		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);
	}

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

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

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

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

	return initializer;
}

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

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

	return initializer;
}

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

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

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

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

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

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

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

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

ir_initializer_t *get_initializer_compound_value(
547
		const ir_initializer_t *initializer, size_t index)
548
549
550
551
552
553
554
555
556
557
558
559
560
561
{
	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
562
#ifndef NDEBUG
563
	ir_initializer_t *initializer = get_entity_initializer(entity);
564
565
	if (initializer == NULL)
		return;
566
	ir_type          *entity_tp   = get_entity_type(entity);
Matthias Braun's avatar
Matthias Braun committed
567
568
	switch (initializer->kind) {
	case IR_INITIALIZER_COMPOUND:
569
		assert(is_aggregate_type(entity_tp));
Matthias Braun's avatar
Matthias Braun committed
570
		break;
571

Matthias Braun's avatar
Matthias Braun committed
572
573
	case IR_INITIALIZER_CONST:
	case IR_INITIALIZER_TARVAL:
574
		assert(is_atomic_type(entity_tp));
Matthias Braun's avatar
Matthias Braun committed
575
		break;
576

Matthias Braun's avatar
Matthias Braun committed
577
578
579
	case IR_INITIALIZER_NULL:
		break;
	}
yb9976's avatar
yb9976 committed
580
581
#else
	(void)entity;
Matthias Braun's avatar
Matthias Braun committed
582
#endif
583
584
585
586
}

void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
{
587
	assert(get_entity_kind(entity) == IR_ENTITY_NORMAL);
588
	entity->attr.normal.initializer = initializer;
589
590
591
	check_entity_initializer(entity);
}

592
ir_initializer_t *(get_entity_initializer)(const ir_entity *entity)
593
{
594
	return _get_entity_initializer(entity);
595
596
}

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

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

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

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

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

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

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

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

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

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

679

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

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

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

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

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

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

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

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

736
737
738
739
740
ir_graph *(get_entity_linktime_irg)(const ir_entity *ent)
{
	return _get_entity_linktime_irg(ent);
}

741
742
void set_entity_irg(ir_entity *ent, ir_graph *irg)
{
743
744
	assert(is_method_entity(ent));
	ent->attr.mtd_attr.irg = irg;
745
}
746

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

763
764
unsigned get_entity_vtable_number(const ir_entity *ent)
{
yb9976's avatar
yb9976 committed
765
	assert(is_method_entity(ent));
766
	return ent->attr.mtd_attr.vtable_number;
767
}
768

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

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

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

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

Matthias Braun's avatar
Matthias Braun committed
796
int is_method_entity(const ir_entity *ent)
797
{
798
799
800
801
802
803
	return ent->entity_kind == IR_ENTITY_METHOD;
}

int is_alias_entity(const ir_entity *entity)
{
	return entity->entity_kind == IR_ENTITY_ALIAS;
804
}
805

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

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

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

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

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

831
832
833
834
835
836
int entity_has_additional_properties(const ir_entity *entity)
{
	return entity->entity_kind == IR_ENTITY_METHOD
	    || entity->entity_kind == IR_ENTITY_ALIAS;
}

837
mtp_additional_properties get_entity_additional_properties(const ir_entity *ent)
838
{
839
840
	assert(entity_has_additional_properties(ent));
	return ent->attr.properties;
841
}
842

Matthias Braun's avatar
Matthias Braun committed
<