void MOF_Object_Reference::validate()
{
    MOF_Class_Decl* class_decl;
    MOF_Key_Value_Pair* p;

    /*
     * Does class exist?
     */

    if ((class_decl = MOF_Class_Decl::find(class_name)) == 0)
    {
        MOF_error_printf("class undefined in reference initializer: \"%s\"",
            class_name);
    }

    /*
     * Validate each key-value pair:
     */

    for (p = pairs; p; p = (MOF_Key_Value_Pair*)p->next)
        p->validate(class_decl);

    /*
     * Check to see that there are as many key-value pairs as keys in the
     * the class.
     */

    if (class_decl->count_keys() != pairs->list_size())
    {
        MOF_error("class contains keys which are missing "
            "from the reference initializer");
    }
}
void _make_obj_ref(
    MOF_Instance_Decl* inst_decl,
    MOF_Object_Reference*& obj_ref)
{
    MOF_Feature_Info* p;
    bool found_key = false;

    /*
     * Allocate the MOF_Object_Reference object:
     */

    if ((obj_ref = new MOF_Object_Reference()) == 0)
    {
        MOF_error("out of memory");
        return;
    }

    /*
     * Set class name:
     */

    if ((obj_ref->class_name = strdup(inst_decl->class_name)) == 0)
    {
        MOF_error("out of memory");
        return;
    }

    /*
     * Iterate the features looking for keys:
     */

    for (p = inst_decl->all_features; p; p = (MOF_Feature_Info*)p->next)
    {
        MOF_Feature* feature = p->feature;

        if (feature->qual_mask & MOF_QT_KEY)
        {
            found_key = true;

            /*
             * The feature cannot be a method because the the key qualifier
             * (which we just checked for) only applies to references and
             * properties.
             */

            MOF_ASSERT(feature->type != MOF_FEATURE_METHOD);

            /* 
             * Create new MOF_Key_Value_Pair.
             */

            MOF_Key_Value_Pair* pair = new MOF_Key_Value_Pair();

            if (pair == 0)
            {
                MOF_error("out of memory");
                return;
            }

            /* 
             * Initialize MOF_Key_Value_Pair object.
             */

            if ((pair->key = strdup(feature->name)) == 0)
            {
                MOF_error("out of memory");
                return;
            }

            /*
             * If property; else reference.
             */

            if (feature->type == MOF_FEATURE_PROP)
            {
                MOF_Property_Decl* prop_decl = (MOF_Property_Decl*)feature;

                /*
                 * Grab is_array flag.
                 */

                pair->is_array 
                    = prop_decl->array_index == 0 ? false : true;

                /*
                 * Clone the initializer.
                 */

                if (prop_decl->initializer)
                {
                    if ((pair->value = (MOF_Literal*)
                        prop_decl->initializer->clone_list()) == 0)
                    {
                        MOF_error("out of memory");
                        return;
                    }
                }
                else
                    pair->value = 0;
            }
            else if (feature->type == MOF_FEATURE_REF)
            {
                MOF_Reference_Decl* ref_decl = (MOF_Reference_Decl*)feature;

                /*
                 * It can't be an array if its a reference.
                 */

                pair->is_array = false;

                /*
                 * If the initializer is non-null.
                 */

                if (ref_decl->obj_ref)
                {
                    MOF_Literal* value = new MOF_Literal();
                    value->value_type = TOK_STRING_VALUE;
                    value->string_value = ref_decl->obj_ref->to_string();
                    pair->value = value;
                }
                else
                    pair->value = 0;
            }
            else
            {
                /* Logically unreachable. */
                MOF_ASSERT(0);
            }

            /*
             * Append to list
             */
            
            if (obj_ref->pairs)
                obj_ref->pairs->append(pair);
            else
                obj_ref->pairs = pair;
        }
    }

    if (!found_key)
        MOF_error("instance has no key fields");

    obj_ref->validate();
    obj_ref->normalize();
}
static void _build_all_features_list(
    const MOF_Class_Decl* class_decl, 
    MOF_Instance_Decl* inst_decl)
{
    MOF_Feature_Info* p;
    MOF_Feature_Info* all_features = 0;
    MOF_Property* q;

    /*
     * Iterate the properties in the class and build the all-properties list.
     */

    for (p = class_decl->all_features; p; p = (MOF_Feature_Info*)p->next)
    {
        MOF_Feature* feature = p->feature;
        MOF_Feature_Info* new_feature_info;
        MOF_Property* inst_prop = 0;

        /*
         * First, search for this feature in the instance declaration.
         */

        for (q = inst_decl->properties; q; q = (MOF_Property*)q->next)
        {
            if (MOF_stricmp(feature->name, q->name) == 0)
            {
                inst_prop = q;
                break;
            }
        }

        /*
         * Create a new MOF_Feature_Info object.
         */

        if ((new_feature_info = new MOF_Feature_Info()) == 0)
        {
            MOF_error("out of memory");
            return;
        } 

        /*
         * If instance contains a property with this name:
         */

        if (inst_prop)
        {
            if (feature->type == MOF_FEATURE_PROP)
            {
                MOF_Property_Decl* prop_decl;

                /*
                 * Clone the feature (and initialize).
                 */

                prop_decl = (MOF_Property_Decl*)
                    ((MOF_Property_Decl*)feature)->clone();

                if (prop_decl == 0)
                {
                    MOF_error("out of memory");
                    return;
                }

                prop_decl->qualifiers = inst_prop->qualifiers;
                prop_decl->all_qualifiers = 0;
                prop_decl->qual_mask = 0;
                prop_decl->initializer = inst_prop->initializer;

                /*
                 * Initialize the new_feature_info structure:
                 */

                new_feature_info->feature = (MOF_Feature*)prop_decl;
                new_feature_info->class_origin = p->class_origin;
                new_feature_info->propagated = p->propagated;

                /*
                 * Build the all qualifiers list
                 */

                prop_decl->all_qualifiers = 
                    MOF_Qualifier_Info::make_all_qualifiers(
                        class_decl->name, 
                        0, 
                        inst_prop->name, 
                        0,
                        prop_decl->qualifiers, 
                        feature->all_qualifiers,
                        &prop_decl->qual_mask,
                        true); /* prop */

                /*
                 * Validate the initializer (if any).
                 */

                if (inst_prop->initializer)
                {
                    inst_prop->initializer->validate("property", 
                        inst_prop->name, prop_decl->data_type, 
                        prop_decl->array_index);
                }
            }
            else if (feature->type == MOF_FEATURE_REF)
            {
                MOF_Reference_Decl* ref_decl;

                /*
                 * Clone the feature (and initialize).
                 */

                ref_decl = (MOF_Reference_Decl*)
                    ((MOF_Reference_Decl*)feature)->clone();

                if (ref_decl == 0)
                {
                    MOF_error("out of memory");
                    return;
                }

                ref_decl->qualifiers = inst_prop->qualifiers;
                ref_decl->all_qualifiers = 0;
                ref_decl->qual_mask = 0;
                ref_decl->alias = 0;

                /*
                 * Initialize the new_feature_info structure:
                 */

                new_feature_info->feature = (MOF_Feature*)ref_decl;
                new_feature_info->class_origin = p->class_origin;
                new_feature_info->propagated = p->propagated;

                /*
                 * Build the all qualifiers list
                 */

                ref_decl->all_qualifiers = 
                    MOF_Qualifier_Info::make_all_qualifiers(
                    class_decl->name, 
                    0, 
                    inst_prop->name, 
                    0,
                    ref_decl->qualifiers, 
                    feature->all_qualifiers,
                    &ref_decl->qual_mask,
                    false); /* prop */

                /*
                 * Validate the alias (ATTN) OR object reference.
                 */

                if (inst_prop->initializer)
                {
                    MOF_Object_Reference* obj_ref = 0;

                    /*
                     * Since it's a reference, we expect a string initializer.
                     */

                    if (inst_prop->initializer->value_type != TOK_STRING_VALUE)
                    {
                        MOF_error_printf(
                            "bad ref initializer for property: \"%s\"",
                            inst_prop->name);
                        return;
                    }

                    /*
                     * Parse the object reference string:
                     */

                    if (REF_parse(
                        inst_prop->initializer->string_value, &obj_ref) != 0)
                    {
                        MOF_error_printf(
                            "bad ref initializer for property: \"%s\"",
                            inst_prop->name);
                        return;
                    }

                    /*
                     * Validate and normalize new object reference.
                     */

                    obj_ref->validate();
                    obj_ref->normalize();

                    /*
                     * Be sure the class of the initializer is a sub-class 
                     * of the class of the class's ref decl.
                     */

                    ref_decl->validate_obj_ref(obj_ref);
                    ref_decl->obj_ref = obj_ref;
                }
                else if (inst_prop->alias)
                {
                    MOF_Object_Reference* obj_ref = 
                        MOF_Instance_Decl::alias_to_obj_ref(inst_prop->alias);

                    /*
                     * Be sure the class of the initializer is a sub-class 
                     * of the class of the class's ref decl.
                     */

                    ref_decl->validate_obj_ref(obj_ref);
                    ref_decl->obj_ref = obj_ref;
                }
            }
        }
        else
        {
            /*
             * Propagate feature from class.
             */

            new_feature_info->feature = p->feature;
            new_feature_info->class_origin = p->class_origin;
            new_feature_info->propagated = true;
        }

        /*
         * Append to list:
         */

        if (all_features)
            all_features->append(new_feature_info);
        else
            all_features = new_feature_info;
    }

    inst_decl->all_features = all_features;
}
void MOF_Object_Reference::normalize()
{
    size_t count;

    /*
     * Sort the key-value pairs:
     */

    if ((count = pairs->list_size()))
    {
        MOF_Key_Value_Pair* p;
        MOF_Key_Value_Pair** tmp_pairs;
        size_t i;

        /*
         * Allocate an array of pointers to the key-value pairs:
         */

        if ((tmp_pairs = (MOF_Key_Value_Pair**)calloc(
            1, sizeof(MOF_Key_Value_Pair*) * count)) == 0)
        {
            MOF_error("out of memory");
            return;
        }

        /*
         * Initialize the array of pointers and shift key names to lower
         * case as you go.
         */

        for (p = pairs, i = 0; p; p = (MOF_Key_Value_Pair*)p->next, i++)
        {
            tmp_pairs[i] = p;
            MOF_strtolower(p->key);
        }

        /*
         * Sort the array (in ascending order by key):
         */

        qsort(tmp_pairs, count, sizeof(MOF_Key_Value_Pair*), _compare);

        /*
         * Reset the linked list pointers so the linked list is in sorted order.
         */

        for (i = 0; i < count; i++)
            tmp_pairs[i]->next = 0;

        for (i = 1; i < count; i++)
            tmp_pairs[i-1]->append(tmp_pairs[i]);

        MOF_ASSERT(tmp_pairs[0]->list_size() == count);

        /*
         * Set new pairs head:
         */

        pairs = tmp_pairs[0];

        /*
         * Release the temporary array.
         */

        free(tmp_pairs);
    }

    /*
     * Shift the class name to lower case:
     */

    MOF_strtolower(class_name);
}