entity.c 23.1 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
		res                = intern_new_entity(owner, IR_ENTITY_METHOD, name, type);
68
		ir_node *const val = new_r_Address(irg, res);
69
		set_atomic_ent_value(res, val);
Matthias Braun's avatar
Matthias Braun committed
70
71
		res->linkage                   = IR_LINKAGE_CONSTANT;
		res->attr.mtd_attr.properties  = get_method_additional_properties(type);
Matthias Braun's avatar
Matthias Braun committed
72
73
74
75
		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;
76
	} else if (is_compound_type(owner) && !(owner->flags & tf_segment)) {
77
		res = intern_new_entity(owner, IR_ENTITY_COMPOUND_MEMBER, name, type);
78
		res->attr.compound_member.offset = -1;
Matthias Braun's avatar
Matthias Braun committed
79
	} else {
80
		res = intern_new_entity(owner, IR_ENTITY_NORMAL, name, type);
81
82
	}

83
84
85
	hook_new_entity(res);
	return res;
}
86

87
88
89
90
91
92
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
93

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

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

Matthias Braun's avatar
Matthias Braun committed
115
116
117
118
119
120
121
122
123
124
125
126
ir_entity *new_got_entry_entity(ir_entity *referenced)
{
	ir_type *reftype = get_entity_type(referenced);
	ir_type *pointer = new_type_pointer(reftype);
	ir_type *global_type = get_glob_type();
	ir_entity *res = intern_new_entity(global_type, IR_ENTITY_GOTENTRY, NULL,
	                                   pointer);
	res->attr.got.referenced = referenced;
	hook_new_entity(res);
	return res;
}

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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;
}

148
149
150
/**
 * Free entity attributes.
 */
Matthias Braun's avatar
Matthias Braun committed
151
152
static void free_entity_attrs(ir_entity *ent)
{
153
154
155
	if (ent->overwrites != NULL) {
		DEL_ARR_F(ent->overwrites);
		ent->overwrites = NULL;
156
	}
157
158
159
160
161
	if (ent->overwrittenby != NULL) {
		DEL_ARR_F(ent->overwrittenby);
		ent->overwrittenby = NULL;
	}

Matthias Braun's avatar
Matthias Braun committed
162
163
164
	if (ent->initializer != NULL) {
		/* TODO: free initializers */
	}
165
	if (ent->entity_kind == IR_ENTITY_METHOD) {
166
167
168
169
170
171
172
173
174
		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;
		}
	}
175
}
Christian Schäfer's avatar
Christian Schäfer committed
176

177
178
179
180
181
182
183
/**
 * Creates a deep copy of an entity.
 */
static ir_entity *deep_entity_copy(ir_entity *old)
{
	ir_entity *newe = XMALLOC(ir_entity);

184
	*newe = *old;
Matthias Braun's avatar
Matthias Braun committed
185
186
	if (old->initializer != NULL) {
		/* FIXME: the initializers are NOT copied */
187
188
189
190
191
	} 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;
	}
192
193
	newe->overwrites    = NULL;
	newe->overwrittenby = NULL;
194
195
196
197

#ifdef DEBUG_libfirm
	newe->nr = get_irp_new_node_nr();
#endif
198
	hook_new_entity(newe);
199
200
	return newe;
}
201

202
ir_entity *copy_entity_own(ir_entity *old, ir_type *new_owner)
203
{
204
205
	assert(is_entity(old));
	assert(is_compound_type(new_owner));
206
	assert(get_type_state(new_owner) != layout_fixed);
207
208
209
210
	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
211
	ir_entity *newe = deep_entity_copy(old);
212
	newe->owner = new_owner;
213
	add_compound_member(new_owner, newe);
214

215
	return newe;
216
}
217

218
ir_entity *copy_entity_name(ir_entity *old, ident *new_name)
219
{
Matthias Braun's avatar
Matthias Braun committed
220
	assert(old->kind == k_entity);
221
222
223
	if (old->name == new_name)
		return old;

Matthias Braun's avatar
Matthias Braun committed
224
225
	ir_entity *newe = deep_entity_copy(old);
	newe->name    = new_name;
226
	newe->ld_name = NULL;
227
	add_compound_member(old->owner, newe);
228

229
	return newe;
230
}
231

232
void free_entity(ir_entity *ent)
233
{
234
	if (is_compound_type(ent->owner))
235
		remove_compound_member(ent->owner, ent);
236

Matthias Braun's avatar
Matthias Braun committed
237
	assert(ent->kind == k_entity);
238
	free_entity_attrs(ent);
239
#ifdef DEBUG_libfirm
240
	ent->kind = k_BAD;
241
#endif
242
	free(ent);
243
}
244

245
long get_entity_nr(const ir_entity *ent)
246
{
Matthias Braun's avatar
Matthias Braun committed
247
	assert(ent->kind == k_entity);
248
#ifdef DEBUG_libfirm
249
	return ent->nr;
250
#else
251
	return (long)PTR_TO_INT(ent);
252
#endif
253
}
254

255
256
const char *(get_entity_name)(const ir_entity *ent)
{
257
	return _get_entity_name(ent);
Matthias Braun's avatar
Matthias Braun committed
258
}
Christian Schäfer's avatar
Christian Schäfer committed
259

260
261
ident *(get_entity_ident)(const ir_entity *ent)
{
262
	return _get_entity_ident(ent);
Matthias Braun's avatar
Matthias Braun committed
263
}
Michael Beck's avatar
Michael Beck committed
264

265
266
void (set_entity_ident)(ir_entity *ent, ident *id)
{
267
	_set_entity_ident(ent, id);
Matthias Braun's avatar
Matthias Braun committed
268
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
269

270
271
ir_type *(get_entity_owner)(const ir_entity *ent)
{
272
	return _get_entity_owner(ent);
Matthias Braun's avatar
Matthias Braun committed
273
}
Christian Schäfer's avatar
Christian Schäfer committed
274

275
void set_entity_owner(ir_entity *ent, ir_type *owner)
276
{
277
278
	assert(is_entity(ent));
	assert(is_compound_type(owner));
279
280
281

	remove_compound_member(ent->owner, ent);
	add_compound_member(owner, ent);
282
	ent->owner = owner;
Matthias Braun's avatar
Matthias Braun committed
283
}
Christian Schäfer's avatar
Christian Schäfer committed
284

Matthias Braun's avatar
Matthias Braun committed
285
286
ident *(get_entity_ld_ident)(const ir_entity *ent)
{
287
	return _get_entity_ld_ident(ent);
Matthias Braun's avatar
Matthias Braun committed
288
}
289

290
291
void (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident)
{
292
	_set_entity_ld_ident(ent, ld_ident);
Matthias Braun's avatar
Matthias Braun committed
293
}
Christian Schäfer's avatar
Christian Schäfer committed
294

Matthias Braun's avatar
Matthias Braun committed
295
296
const char *(get_entity_ld_name)(const ir_entity *ent)
{
297
	return _get_entity_ld_name(ent);
Matthias Braun's avatar
Matthias Braun committed
298
}
299

300
301
302
303
304
int entity_has_ld_ident(const ir_entity *entity)
{
	return entity->ld_name != NULL;
}

305
306
ir_type *(get_entity_type)(const ir_entity *ent)
{
307
	return _get_entity_type(ent);
Matthias Braun's avatar
Matthias Braun committed
308
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
309

Matthias Braun's avatar
Matthias Braun committed
310
311
312
313
314
315
316
317
318
319
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:
320
		assert(type == get_code_type());
Matthias Braun's avatar
Matthias Braun committed
321
322
323
324
325
		break;
	case IR_ENTITY_COMPOUND_MEMBER:
		break;
	}
	ent->type = type;
Matthias Braun's avatar
Matthias Braun committed
326
}
Götz Lindenmaier's avatar
a    
Götz Lindenmaier committed
327

328
329
ir_volatility (get_entity_volatility)(const ir_entity *ent)
{
330
	return _get_entity_volatility(ent);
Matthias Braun's avatar
Matthias Braun committed
331
}
Götz Lindenmaier's avatar
a    
Götz Lindenmaier committed
332

333
334
void (set_entity_volatility)(ir_entity *ent, ir_volatility vol)
{
335
	_set_entity_volatility(ent, vol);
Matthias Braun's avatar
Matthias Braun committed
336
}
Götz Lindenmaier's avatar
a    
Götz Lindenmaier committed
337

338
const char *get_volatility_name(ir_volatility var)
339
{
340
#define X(a)    case a: return #a
341
342
343
344
	switch (var) {
	X(volatility_non_volatile);
	X(volatility_is_volatile);
	}
345
#undef X
Matthias Braun's avatar
Matthias Braun committed
346
	return "BAD VALUE";
347
}
348

349
350
ir_align (get_entity_aligned)(const ir_entity *ent)
{
Matthias Braun's avatar
Matthias Braun committed
351
352
	return _get_entity_aligned(ent);
}
353

354
355
void (set_entity_aligned)(ir_entity *ent, ir_align a)
{
Matthias Braun's avatar
Matthias Braun committed
356
357
	_set_entity_aligned(ent, a);
}
358

359
360
unsigned (get_entity_alignment)(const ir_entity *ent)
{
361
362
363
	return _get_entity_alignment(ent);
}

364
365
void (set_entity_alignment)(ir_entity *ent, unsigned alignment)
{
366
367
368
	_set_entity_alignment(ent, alignment);
}

Michael Beck's avatar
Michael Beck committed
369
370
371
372
373
374
375
376
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
377
	return "BAD VALUE";
378
}
Michael Beck's avatar
Michael Beck committed
379

380
void set_entity_label(ir_entity *ent, ir_label_t label)
381
{
Matthias Braun's avatar
Matthias Braun committed
382
	assert(ent->entity_kind == IR_ENTITY_LABEL);
383
384
385
386
387
	ent->attr.code_attr.label = label;
}

ir_label_t get_entity_label(const ir_entity *ent)
{
Matthias Braun's avatar
Matthias Braun committed
388
	assert(ent->entity_kind == IR_ENTITY_LABEL);
389
390
391
	return ent->attr.code_attr.label;
}

392
393
394
395
396
397
398
void set_entity_visibility(ir_entity *entity, ir_visibility visibility)
{
	entity->visibility = visibility;
}

ir_visibility get_entity_visibility(const ir_entity *entity)
{
399
	return (ir_visibility)entity->visibility;
400
401
}

Matthias Braun's avatar
Matthias Braun committed
402
403
404
405
406
407
408
409
410
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);
}
411

Matthias Braun's avatar
Matthias Braun committed
412
413
414
415
void add_entity_linkage(ir_entity *entity, ir_linkage linkage)
{
	entity->linkage |= linkage;
}
416

Matthias Braun's avatar
Matthias Braun committed
417
418
419
420
void remove_entity_linkage(ir_entity *entity, ir_linkage linkage)
{
	entity->linkage &= ~linkage;
}
421

422
423
int (is_entity_compiler_generated)(const ir_entity *ent)
{
Michael Beck's avatar
Michael Beck committed
424
	return _is_entity_compiler_generated(ent);
425
}
426

427
428
void (set_entity_compiler_generated)(ir_entity *ent, int flag)
{
Michael Beck's avatar
Michael Beck committed
429
	_set_entity_compiler_generated(ent, flag);
430
}
431

432
433
ir_entity_usage (get_entity_usage)(const ir_entity *ent)
{
434
435
	return _get_entity_usage(ent);
}
Michael Beck's avatar
Michael Beck committed
436

437
438
void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags)
{
439
440
	_set_entity_usage(ent, flags);
}
441

442
ir_node *get_atomic_ent_value(const ir_entity *entity)
Matthias Braun's avatar
Matthias Braun committed
443
{
Matthias Braun's avatar
Matthias Braun committed
444
	assert(is_atomic_entity(entity));
Matthias Braun's avatar
Matthias Braun committed
445
446

	ir_initializer_t *initializer = get_entity_initializer(entity);
Matthias Braun's avatar
Matthias Braun committed
447
448
449
450
	if (initializer == NULL) {
		ir_type *type = get_entity_type(entity);
		return new_r_Unknown(get_const_code_irg(), get_type_mode(type));
	}
451

Matthias Braun's avatar
Matthias Braun committed
452
453
454
455
456
457
458
	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
459
		ir_tarval *tv = get_initializer_tarval_value(initializer);
Matthias Braun's avatar
Matthias Braun committed
460
461
462
463
464
465
466
467
		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);
	}

468
	panic("invalid initializer kind (%+F)", entity);
Matthias Braun's avatar
Matthias Braun committed
469
470
471
}

void set_atomic_ent_value(ir_entity *entity, ir_node *val)
472
{
Matthias Braun's avatar
Matthias Braun committed
473
474
	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
475
	ir_initializer_t *initializer = create_initializer_const(val);
Matthias Braun's avatar
Matthias Braun committed
476
477
	entity->initializer = initializer;
}
478

479
480
481
482
483
484
485
486
487
488
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
489
	return "BAD VALUE";
490
491
}

492
493
494
495
496
497
498
499
500
501
502
503
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
504
		= (ir_initializer_t*)OALLOC(obst, ir_initializer_const_t);
505
506
507
508
509
510
	initializer->kind         = IR_INITIALIZER_CONST;
	initializer->consti.value = value;

	return initializer;
}

Matthias Braun's avatar
Matthias Braun committed
511
ir_initializer_t *create_initializer_tarval(ir_tarval *tv)
512
513
514
515
{
	struct obstack *obst = get_irg_obstack(get_const_code_irg());

	ir_initializer_t *initializer
516
		= (ir_initializer_t*)OALLOC(obst, ir_initializer_tarval_t);
517
518
519
520
521
522
	initializer->kind         = IR_INITIALIZER_TARVAL;
	initializer->tarval.value = tv;

	return initializer;
}

523
ir_initializer_t *create_initializer_compound(size_t n_entries)
524
525
526
{
	struct obstack *obst = get_irg_obstack(get_const_code_irg());

Matthias Braun's avatar
Matthias Braun committed
527
528
529
	size_t size = sizeof(ir_initializer_compound_t)
	            + n_entries * sizeof(ir_initializer_t*)
	            - sizeof(ir_initializer_t*);
530

531
532
	ir_initializer_t *initializer
		= (ir_initializer_t*)obstack_alloc(obst, size);
533
534
535
	initializer->kind                    = IR_INITIALIZER_COMPOUND;
	initializer->compound.n_initializers = n_entries;

Matthias Braun's avatar
Matthias Braun committed
536
	for (size_t i = 0; i < n_entries; ++i) {
537
538
539
540
541
542
543
544
545
		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);
546
	return skip_Id(initializer->consti.value);
547
548
}

Matthias Braun's avatar
Matthias Braun committed
549
ir_tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
550
551
552
553
554
{
	assert(initializer->kind == IR_INITIALIZER_TARVAL);
	return initializer->tarval.value;
}

555
size_t get_initializer_compound_n_entries(const ir_initializer_t *initializer)
556
557
558
559
560
561
{
	assert(initializer->kind == IR_INITIALIZER_COMPOUND);
	return initializer->compound.n_initializers;
}

void set_initializer_compound_value(ir_initializer_t *initializer,
562
                                    size_t index, ir_initializer_t *value)
563
564
565
566
567
568
569
570
{
	assert(initializer->kind == IR_INITIALIZER_COMPOUND);
	assert(index < initializer->compound.n_initializers);

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

ir_initializer_t *get_initializer_compound_value(
571
		const ir_initializer_t *initializer, size_t index)
572
573
574
575
576
577
578
579
580
581
582
583
584
585
{
	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
586
587
#ifndef NDEBUG
	ir_initializer_t *initializer = entity->initializer;
588
	ir_type          *entity_tp   = get_entity_type(entity);
Matthias Braun's avatar
Matthias Braun committed
589
590
	switch (initializer->kind) {
	case IR_INITIALIZER_COMPOUND:
591
		assert(is_aggregate_type(entity_tp));
Matthias Braun's avatar
Matthias Braun committed
592
593
		break;
	case IR_INITIALIZER_CONST:
594
		/* methods are initialized by an Address */
595
596
		assert(is_atomic_type(entity_tp) || is_Method_type(entity_tp));
		break;
Matthias Braun's avatar
Matthias Braun committed
597
	case IR_INITIALIZER_TARVAL:
598
		assert(is_atomic_type(entity_tp));
Matthias Braun's avatar
Matthias Braun committed
599
600
601
602
		break;
	case IR_INITIALIZER_NULL:
		break;
	}
yb9976's avatar
yb9976 committed
603
604
#else
	(void)entity;
Matthias Braun's avatar
Matthias Braun committed
605
#endif
606
607
608
609
}

void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
{
Matthias Braun's avatar
Matthias Braun committed
610
	entity->initializer = initializer;
611
612
613
	check_entity_initializer(entity);
}

614
615
int has_entity_initializer(const ir_entity *entity)
{
Matthias Braun's avatar
Matthias Braun committed
616
	return entity->initializer != NULL;
617
618
}

619
620
ir_initializer_t *get_entity_initializer(const ir_entity *entity)
{
Matthias Braun's avatar
Matthias Braun committed
621
	return entity->initializer;
622
623
}

624
625
int (get_entity_offset)(const ir_entity *ent)
{
626
	return _get_entity_offset(ent);
627
}
Michael Beck's avatar
Michael Beck committed
628

629
630
void (set_entity_offset)(ir_entity *ent, int offset)
{
631
	_set_entity_offset(ent, offset);
632
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
633

634
unsigned (get_entity_bitfield_offset)(const ir_entity *ent)
635
{
636
	return _get_entity_bitfield_offset(ent);
637
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
638

639
void (set_entity_bitfield_offset)(ir_entity *ent, unsigned offset)
640
{
641
642
643
644
645
646
647
648
649
650
651
	_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);
652
}
Michael Beck's avatar
Michael Beck committed
653

654
655
void add_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
{
656
657
658
	if (ent->overwrites == NULL) {
		ent->overwrites = NEW_ARR_F(ir_entity*, 0);
	}
659
	ARR_APP1(ir_entity *, ent->overwrites, overwritten);
660
661
662
	if (overwritten->overwrittenby == NULL) {
		overwritten->overwrittenby = NEW_ARR_F(ir_entity*, 0);
	}
663
	ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
664
}
665

666
size_t get_entity_n_overwrites(const ir_entity *ent)
667
{
668
669
670
	if (ent->overwrites == NULL)
		return 0;
	return ARR_LEN(ent->overwrites);
671
}
672

673
size_t get_entity_overwrites_index(const ir_entity *ent, ir_entity *overwritten)
674
{
Matthias Braun's avatar
Matthias Braun committed
675
	for (size_t i = 0, n = get_entity_n_overwrites(ent); i < n; ++i) {
676
677
		if (get_entity_overwrites(ent, i) == overwritten)
			return i;
678
	}
679
	return (size_t)-1;
680
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
681

682
ir_entity *get_entity_overwrites(const ir_entity *ent, size_t pos)
683
{
684
685
	assert(pos < get_entity_n_overwrites(ent));
	return ent->overwrites[pos];
686
}
687

688
void set_entity_overwrites(ir_entity *ent, size_t pos, ir_entity *overwritten)
689
{
690
691
	assert(pos < get_entity_n_overwrites(ent));
	ent->overwrites[pos] = overwritten;
692
}
693

694
695
void remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
{
Matthias Braun's avatar
Matthias Braun committed
696
	for (size_t i = 0, n = get_entity_n_overwrites(ent); i < n; ++i) {
697
		if (ent->overwrites[i] == overwritten) {
698
			for (; i < n - 1; i++)
699
				ent->overwrites[i] = ent->overwrites[i+1];
700
			ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
701
702
			break;
		}
703
	}
704
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
705

706

707
size_t get_entity_n_overwrittenby(const ir_entity *ent)
708
{
709
710
	if (ent->overwrittenby == NULL)
		return 0;
711
	return ARR_LEN(ent->overwrittenby);
712
}
713

714
715
size_t get_entity_overwrittenby_index(const ir_entity *ent,
                                      ir_entity *overwrites)
716
{
Matthias Braun's avatar
Matthias Braun committed
717
	for (size_t i = 0, n = get_entity_n_overwrittenby(ent); i < n; ++i) {
718
719
		if (get_entity_overwrittenby(ent, i) == overwrites)
			return i;
720
	}
721
	return (size_t)-1;
722
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
723

724
ir_entity *get_entity_overwrittenby(const ir_entity *ent, size_t pos)
725
{
726
727
	assert(pos < get_entity_n_overwrittenby(ent));
	return ent->overwrittenby[pos];
728
}
729

730
void set_entity_overwrittenby(ir_entity *ent, size_t pos, ir_entity *overwrites)
731
{
732
733
	assert(pos < get_entity_n_overwrittenby(ent));
	ent->overwrittenby[pos] = overwrites;
734
}
735

736
737
void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites)
{
Matthias Braun's avatar
Matthias Braun committed
738
	for (size_t i = 0, n = get_entity_n_overwrittenby(ent); i < n; ++i) {
739
		if (ent->overwrittenby[i] == overwrites) {
740
			for (; i < n - 1; ++i)
741
				ent->overwrittenby[i] = ent->overwrittenby[i+1];
742
			ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
743
744
			break;
		}
745
	}
746
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
747

748
749
void *(get_entity_link)(const ir_entity *ent)
{
750
	return _get_entity_link(ent);
751
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
752

753
754
void (set_entity_link)(ir_entity *ent, void *l)
{
755
	_set_entity_link(ent, l);
756
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
757

758
759
ir_graph *(get_entity_irg)(const ir_entity *ent)
{
760
	return _get_entity_irg(ent);
761
}
Christian Schäfer's avatar
Christian Schäfer committed
762

763
764
765
766
767
ir_graph *(get_entity_linktime_irg)(const ir_entity *ent)
{
	return _get_entity_linktime_irg(ent);
}

768
769
void set_entity_irg(ir_entity *ent, ir_graph *irg)
{
770
	assert(is_method_entity(ent));
Matthias Braun's avatar
Matthias Braun committed
771
	assert(get_entity_peculiarity(ent) == peculiarity_existent);
772
	ent->attr.mtd_attr.irg = irg;
773
}
774

775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
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;
}

791
792
unsigned get_entity_vtable_number(const ir_entity *ent)
{
yb9976's avatar
yb9976 committed
793
	assert(is_method_entity(ent));
794
	return ent->attr.mtd_attr.vtable_number;
795
}
796

797
798
void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number)
{
799
800
	assert(is_method_entity(ent));
	ent->attr.mtd_attr.vtable_number = vtable_number;
801
}
802

803
804
805
806
807
int is_unknown_entity(const ir_entity *entity)
{
	return entity->entity_kind == IR_ENTITY_UNKNOWN;
}

808
809
int (is_entity)(const void *thing)
{
810
	return _is_entity(thing);
811
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
812

Matthias Braun's avatar
Matthias Braun committed
813
int is_atomic_entity(const ir_entity *ent)
814
{
Matthias Braun's avatar
Matthias Braun committed
815
	ir_type     *t  = get_entity_type(ent);
816
	const tp_op *op = get_type_tpop(t);
817
818
	return op == type_primitive || op == type_pointer
	    || op == type_method;
819
}
820

Matthias Braun's avatar
Matthias Braun committed
821
int is_compound_entity(const ir_entity *ent)
822
{
823
824
825
	ir_type     *t  = get_entity_type(ent);
	const tp_op *op = get_type_tpop(t);
	return (op == type_class || op == type_struct ||
826
	        op == type_array || op == type_union);
827
}
828

Matthias Braun's avatar
Matthias Braun committed
829
int is_method_entity(const ir_entity *ent)
830
{
831
832
833
834
835
836
	return ent->entity_kind == IR_ENTITY_METHOD;
}

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

Matthias Braun's avatar
Matthias Braun committed
839
ir_visited_t (get_entity_visited)(const ir_entity *ent)
840
{
841
	return _get_entity_visited(ent);
842
}
Michael Beck's avatar
Michael Beck committed
843

844
845
void (set_entity_visited)(ir_entity *ent, ir_visited_t num)
{
846
	_set_entity_visited(ent, num);
847
}
Michael Beck's avatar
Michael Beck committed
848

849
850
void (mark_entity_visited)(ir_entity *ent)
{
851
	_mark_entity_visited(ent);
852
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
853

Matthias Braun's avatar
Matthias Braun committed
854
int (entity_visited)(const ir_entity *ent)
855
{
856
	return _entity_visited(ent);
857
}
858

Matthias Braun's avatar
Matthias Braun committed
859
int (entity_not_visited)(const ir_entity *ent)
860
{
861
	return _entity_not_visited(ent);
Matthias Braun's avatar