entity.c 21.4 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
ir_entity_usage (get_entity_usage)(const ir_entity *ent)
{
400
401
	return _get_entity_usage(ent);
}
Michael Beck's avatar
Michael Beck committed
402

403
404
void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags)
{
405
406
	_set_entity_usage(ent, flags);
}
407

408
ir_node *get_atomic_ent_value(const ir_entity *entity)
Matthias Braun's avatar
Matthias Braun committed
409
{
Matthias Braun's avatar
Matthias Braun committed
410
	assert(is_atomic_entity(entity));
Matthias Braun's avatar
Matthias Braun committed
411
412

	ir_initializer_t *initializer = get_entity_initializer(entity);
Matthias Braun's avatar
Matthias Braun committed
413
414
415
416
	if (initializer == NULL) {
		ir_type *type = get_entity_type(entity);
		return new_r_Unknown(get_const_code_irg(), get_type_mode(type));
	}
417

Matthias Braun's avatar
Matthias Braun committed
418
419
420
421
	switch (get_initializer_kind(initializer)) {
	case IR_INITIALIZER_NULL: {
		ir_type *type = get_entity_type(entity);
		ir_mode *mode = get_type_mode(type);
422
		return new_r_Const_null(get_const_code_irg(), mode);
Matthias Braun's avatar
Matthias Braun committed
423
424
	}
	case IR_INITIALIZER_TARVAL: {
Matthias Braun's avatar
Matthias Braun committed
425
		ir_tarval *tv = get_initializer_tarval_value(initializer);
Matthias Braun's avatar
Matthias Braun committed
426
427
428
429
430
431
432
433
		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);
	}

434
	panic("invalid initializer kind (%+F)", entity);
Matthias Braun's avatar
Matthias Braun committed
435
436
437
}

void set_atomic_ent_value(ir_entity *entity, ir_node *val)
438
{
Matthias Braun's avatar
Matthias Braun committed
439
440
	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
441
	ir_initializer_t *initializer = create_initializer_const(val);
442
	set_entity_initializer(entity, initializer);
Matthias Braun's avatar
Matthias Braun committed
443
}
444

445
446
447
448
449
450
451
452
453
454
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
455
	return "BAD VALUE";
456
457
}

458
459
460
461
462
463
464
465
466
467
468
469
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
470
		= (ir_initializer_t*)OALLOC(obst, ir_initializer_const_t);
471
472
473
474
475
476
	initializer->kind         = IR_INITIALIZER_CONST;
	initializer->consti.value = value;

	return initializer;
}

Matthias Braun's avatar
Matthias Braun committed
477
ir_initializer_t *create_initializer_tarval(ir_tarval *tv)
478
479
480
481
{
	struct obstack *obst = get_irg_obstack(get_const_code_irg());

	ir_initializer_t *initializer
482
		= (ir_initializer_t*)OALLOC(obst, ir_initializer_tarval_t);
483
484
485
486
487
488
	initializer->kind         = IR_INITIALIZER_TARVAL;
	initializer->tarval.value = tv;

	return initializer;
}

489
ir_initializer_t *create_initializer_compound(size_t n_entries)
490
491
492
{
	struct obstack *obst = get_irg_obstack(get_const_code_irg());

Matthias Braun's avatar
Matthias Braun committed
493
494
495
	size_t size = sizeof(ir_initializer_compound_t)
	            + n_entries * sizeof(ir_initializer_t*)
	            - sizeof(ir_initializer_t*);
496

497
498
	ir_initializer_t *initializer
		= (ir_initializer_t*)obstack_alloc(obst, size);
499
500
501
	initializer->kind                    = IR_INITIALIZER_COMPOUND;
	initializer->compound.n_initializers = n_entries;

Matthias Braun's avatar
Matthias Braun committed
502
	for (size_t i = 0; i < n_entries; ++i) {
503
504
505
506
507
508
509
510
511
		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);
512
	return skip_Id(initializer->consti.value);
513
514
}

Matthias Braun's avatar
Matthias Braun committed
515
ir_tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
516
517
518
519
520
{
	assert(initializer->kind == IR_INITIALIZER_TARVAL);
	return initializer->tarval.value;
}

521
size_t get_initializer_compound_n_entries(const ir_initializer_t *initializer)
522
523
524
525
526
527
{
	assert(initializer->kind == IR_INITIALIZER_COMPOUND);
	return initializer->compound.n_initializers;
}

void set_initializer_compound_value(ir_initializer_t *initializer,
528
                                    size_t index, ir_initializer_t *value)
529
530
531
532
533
534
535
536
{
	assert(initializer->kind == IR_INITIALIZER_COMPOUND);
	assert(index < initializer->compound.n_initializers);

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

ir_initializer_t *get_initializer_compound_value(
537
		const ir_initializer_t *initializer, size_t index)
538
539
540
541
542
543
544
545
546
547
548
549
550
551
{
	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
552
#ifndef NDEBUG
553
	ir_initializer_t *initializer = get_entity_initializer(entity);
554
555
	if (initializer == NULL)
		return;
556
	ir_type          *entity_tp   = get_entity_type(entity);
Matthias Braun's avatar
Matthias Braun committed
557
558
	switch (initializer->kind) {
	case IR_INITIALIZER_COMPOUND:
559
		assert(is_aggregate_type(entity_tp));
Matthias Braun's avatar
Matthias Braun committed
560
		break;
561

Matthias Braun's avatar
Matthias Braun committed
562
563
	case IR_INITIALIZER_CONST:
	case IR_INITIALIZER_TARVAL:
564
		assert(is_atomic_type(entity_tp));
Matthias Braun's avatar
Matthias Braun committed
565
		break;
566

Matthias Braun's avatar
Matthias Braun committed
567
568
569
	case IR_INITIALIZER_NULL:
		break;
	}
yb9976's avatar
yb9976 committed
570
571
#else
	(void)entity;
Matthias Braun's avatar
Matthias Braun committed
572
#endif
573
574
575
576
}

void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
{
577
	assert(get_entity_kind(entity) == IR_ENTITY_NORMAL);
578
	entity->attr.normal.initializer = initializer;
579
580
581
	check_entity_initializer(entity);
}

582
ir_initializer_t *(get_entity_initializer)(const ir_entity *entity)
583
{
584
	return _get_entity_initializer(entity);
585
586
}

587
588
int (get_entity_offset)(const ir_entity *ent)
{
589
	return _get_entity_offset(ent);
590
}
Michael Beck's avatar
Michael Beck committed
591

592
593
void (set_entity_offset)(ir_entity *ent, int offset)
{
594
	_set_entity_offset(ent, offset);
595
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
596

597
unsigned (get_entity_bitfield_offset)(const ir_entity *ent)
598
{
599
	return _get_entity_bitfield_offset(ent);
600
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
601

602
void (set_entity_bitfield_offset)(ir_entity *ent, unsigned offset)
603
{
604
605
606
607
608
609
610
611
612
613
614
	_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);
615
}
Michael Beck's avatar
Michael Beck committed
616

617
618
void add_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
{
619
620
621
	if (ent->overwrites == NULL) {
		ent->overwrites = NEW_ARR_F(ir_entity*, 0);
	}
622
	ARR_APP1(ir_entity *, ent->overwrites, overwritten);
623
624
625
	if (overwritten->overwrittenby == NULL) {
		overwritten->overwrittenby = NEW_ARR_F(ir_entity*, 0);
	}
626
	ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
627
}
628

629
size_t get_entity_n_overwrites(const ir_entity *ent)
630
{
631
632
633
	if (ent->overwrites == NULL)
		return 0;
	return ARR_LEN(ent->overwrites);
634
}
635

636
size_t get_entity_overwrites_index(const ir_entity *ent, ir_entity *overwritten)
637
{
Matthias Braun's avatar
Matthias Braun committed
638
	for (size_t i = 0, n = get_entity_n_overwrites(ent); i < n; ++i) {
639
640
		if (get_entity_overwrites(ent, i) == overwritten)
			return i;
641
	}
642
	return (size_t)-1;
643
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
644

645
ir_entity *get_entity_overwrites(const ir_entity *ent, size_t pos)
646
{
647
648
	assert(pos < get_entity_n_overwrites(ent));
	return ent->overwrites[pos];
649
}
650

651
void set_entity_overwrites(ir_entity *ent, size_t pos, ir_entity *overwritten)
652
{
653
654
	assert(pos < get_entity_n_overwrites(ent));
	ent->overwrites[pos] = overwritten;
655
}
656

657
658
void remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
{
Matthias Braun's avatar
Matthias Braun committed
659
	for (size_t i = 0, n = get_entity_n_overwrites(ent); i < n; ++i) {
660
		if (ent->overwrites[i] == overwritten) {
661
			for (; i < n - 1; i++)
662
				ent->overwrites[i] = ent->overwrites[i+1];
663
			ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
664
665
			break;
		}
666
	}
667
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
668

669

670
size_t get_entity_n_overwrittenby(const ir_entity *ent)
671
{
672
673
	if (ent->overwrittenby == NULL)
		return 0;
674
	return ARR_LEN(ent->overwrittenby);
675
}
676

677
678
size_t get_entity_overwrittenby_index(const ir_entity *ent,
                                      ir_entity *overwrites)
679
{
Matthias Braun's avatar
Matthias Braun committed
680
	for (size_t i = 0, n = get_entity_n_overwrittenby(ent); i < n; ++i) {
681
682
		if (get_entity_overwrittenby(ent, i) == overwrites)
			return i;
683
	}
684
	return (size_t)-1;
685
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
686

687
ir_entity *get_entity_overwrittenby(const ir_entity *ent, size_t pos)
688
{
689
690
	assert(pos < get_entity_n_overwrittenby(ent));
	return ent->overwrittenby[pos];
691
}
692

693
void set_entity_overwrittenby(ir_entity *ent, size_t pos, ir_entity *overwrites)
694
{
695
696
	assert(pos < get_entity_n_overwrittenby(ent));
	ent->overwrittenby[pos] = overwrites;
697
}
698

699
700
void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites)
{
Matthias Braun's avatar
Matthias Braun committed
701
	for (size_t i = 0, n = get_entity_n_overwrittenby(ent); i < n; ++i) {
702
		if (ent->overwrittenby[i] == overwrites) {
703
			for (; i < n - 1; ++i)
704
				ent->overwrittenby[i] = ent->overwrittenby[i+1];
705
			ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
706
707
			break;
		}
708
	}
709
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
710

711
712
void *(get_entity_link)(const ir_entity *ent)
{
713
	return _get_entity_link(ent);
714
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
715

716
717
void (set_entity_link)(ir_entity *ent, void *l)
{
718
	_set_entity_link(ent, l);
719
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
720

721
722
ir_graph *(get_entity_irg)(const ir_entity *ent)
{
723
	return _get_entity_irg(ent);
724
}
Christian Schäfer's avatar
Christian Schäfer committed
725

726
727
728
729
730
ir_graph *(get_entity_linktime_irg)(const ir_entity *ent)
{
	return _get_entity_linktime_irg(ent);
}

731
732
void set_entity_irg(ir_entity *ent, ir_graph *irg)
{
733
734
	assert(is_method_entity(ent));
	ent->attr.mtd_attr.irg = irg;
735
}
736

737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
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;
}

753
754
unsigned get_entity_vtable_number(const ir_entity *ent)
{
yb9976's avatar
yb9976 committed
755
	assert(is_method_entity(ent));
756
	return ent->attr.mtd_attr.vtable_number;
757
}
758

759
760
void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number)
{
761
762
	assert(is_method_entity(ent));
	ent->attr.mtd_attr.vtable_number = vtable_number;
763
}
764

765
766
767
768
769
int is_unknown_entity(const ir_entity *entity)
{
	return entity->entity_kind == IR_ENTITY_UNKNOWN;
}

Matthias Braun's avatar
Matthias Braun committed
770
int is_atomic_entity(const ir_entity *ent)
771
{
Matthias Braun's avatar
Matthias Braun committed
772
	ir_type     *t  = get_entity_type(ent);
773
	const tp_op *op = get_type_tpop(t);
774
775
	return op == type_primitive || op == type_pointer
	    || op == type_method;
776
}
777

Matthias Braun's avatar
Matthias Braun committed
778
int is_compound_entity(const ir_entity *ent)
779
{
780
781
782
	ir_type     *t  = get_entity_type(ent);
	const tp_op *op = get_type_tpop(t);
	return (op == type_class || op == type_struct ||
783
	        op == type_array || op == type_union);
784
}
785

Matthias Braun's avatar
Matthias Braun committed
786
int is_method_entity(const ir_entity *ent)
787
{
788
789
790
791
792
793
	return ent->entity_kind == IR_ENTITY_METHOD;
}

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

Matthias Braun's avatar
Matthias Braun committed
796
ir_visited_t (get_entity_visited)(const ir_entity *ent)
797
{
798
	return _get_entity_visited(ent);
799
}
Michael Beck's avatar
Michael Beck committed
800

801
802
void (set_entity_visited)(ir_entity *ent, ir_visited_t num)
{
803
	_set_entity_visited(ent, num);
804
}
Michael Beck's avatar
Michael Beck committed
805

806
807
void (mark_entity_visited)(ir_entity *ent)
{
808
	_mark_entity_visited(ent);
809
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
810

Matthias Braun's avatar
Matthias Braun committed
811
int (entity_visited)(const ir_entity *ent)
812
{
813
	return _entity_visited(ent);
814
}
815

Matthias Braun's avatar
Matthias Braun committed
816
int (entity_not_visited)(const ir_entity *ent)
817
{
818
	return _entity_not_visited(ent);
819
}
820

821
822
823
824
825
826
int entity_has_additional_properties(const ir_entity *entity)
{
	return entity->entity_kind == IR_ENTITY_METHOD
	    || entity->entity_kind == IR_ENTITY_ALIAS;
}

827
mtp_additional_properties get_entity_additional_properties(const ir_entity *ent)
828
{
829
830
	assert(entity_has_additional_properties(ent));
	return ent->attr.properties;
831
}
832

Matthias Braun's avatar
Matthias Braun committed
833
834
void set_entity_additional_properties(ir_entity *ent,
                                      mtp_additional_properties property_mask)
835
{
836
	assert(entity_has_additional_properties(ent));
837
	/* you mustn't set fewer properties than the entities type */
Matthias Braun's avatar
Matthias Braun committed
838
	assert((get_method_additional_properties(get_entity_type(ent)) & ~property_mask) == 0);
839

Matthias Braun's avatar
Matthias Braun committed
840
841
	/* do not allow to set the mtp_property_inherited flag or
	 * the automatic inheritance of flags will not work */
Matthias Braun's avatar