Commit 2f2d13da by Doug Evans

c-typeck.c (lookup_field): New function (for chill).

* c-typeck.c (lookup_field): New function (for chill).
(build_component_ref): Call it.

From-SVN: r5330
parent adb44af8
......@@ -1033,48 +1033,15 @@ default_conversion (exp)
return exp;
}
/* Make an expression to refer to the COMPONENT field of
structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
/* Look up component name in the structure type definition. */
tree
build_component_ref (datum, component)
tree datum, component;
static tree
lookup_field (type, component)
tree type, component;
{
register tree type = TREE_TYPE (datum);
register enum tree_code code = TREE_CODE (type);
register tree field = NULL;
register tree ref;
/* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
unless we are not to support things not strictly ANSI. */
switch (TREE_CODE (datum))
{
case COMPOUND_EXPR:
{
tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
return build (COMPOUND_EXPR, TREE_TYPE (value),
TREE_OPERAND (datum, 0), value);
}
case COND_EXPR:
return build_conditional_expr
(TREE_OPERAND (datum, 0),
build_component_ref (TREE_OPERAND (datum, 1), component),
build_component_ref (TREE_OPERAND (datum, 2), component));
}
/* See if there is a field or component with name COMPONENT. */
if (code == RECORD_TYPE || code == UNION_TYPE)
{
if (TYPE_SIZE (type) == 0)
{
incomplete_type_error (NULL_TREE, type);
return error_mark_node;
}
/* Look up component name in the structure type definition.
tree field;
If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
/* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
to the field elements. Use a binary search on this array to quickly
find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
will always be set for structures which have many elements. */
......@@ -1093,6 +1060,27 @@ build_component_ref (datum, component)
half = (top - bot + 1) >> 1;
field = field_array[bot+half];
if (DECL_NAME (field) == NULL_TREE)
{
/* Step through all anon unions in linear fashion. */
while (DECL_NAME (field_array[bot]) == NULL_TREE)
{
tree anon;
field = field_array[bot++];
anon = lookup_field (TREE_TYPE (field), component);
if (anon != NULL_TREE)
return anon;
}
/* Entire record is only anon unions. */
if (bot > top)
return NULL_TREE;
/* Restart the binary search, with new lower bound. */
continue;
}
cmp = (long)DECL_NAME (field) - (long)component;
if (cmp == 0)
break;
......@@ -1111,11 +1099,62 @@ build_component_ref (datum, component)
{
for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
{
if (DECL_NAME (field) == NULL_TREE)
{
tree anon = lookup_field (TREE_TYPE (field), component);
if (anon != NULL_TREE)
return anon;
}
if (DECL_NAME (field) == component)
break;
}
}
return field;
}
/* Make an expression to refer to the COMPONENT field of
structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
tree
build_component_ref (datum, component)
tree datum, component;
{
register tree type = TREE_TYPE (datum);
register enum tree_code code = TREE_CODE (type);
register tree field = NULL;
register tree ref;
/* If DATUM is a COMPOUND_EXPR or COND_EXPR, move our reference inside it
unless we are not to support things not strictly ANSI. */
switch (TREE_CODE (datum))
{
case COMPOUND_EXPR:
{
tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
return build (COMPOUND_EXPR, TREE_TYPE (value),
TREE_OPERAND (datum, 0), value);
}
case COND_EXPR:
return build_conditional_expr
(TREE_OPERAND (datum, 0),
build_component_ref (TREE_OPERAND (datum, 1), component),
build_component_ref (TREE_OPERAND (datum, 2), component));
}
/* See if there is a field or component with name COMPONENT. */
if (code == RECORD_TYPE || code == UNION_TYPE)
{
if (TYPE_SIZE (type) == 0)
{
incomplete_type_error (NULL_TREE, type);
return error_mark_node;
}
field = lookup_field (type, component);
if (!field)
{
error (code == RECORD_TYPE
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment