Add shared copyright to EfficiOS Inc. and Linux Foundation
[babeltrace.git] / types / variant.c
index 8a781f67c0df03af2a114b7fdddbd2d5b6ae509f..bb4faa17093a87ec0266f7f08349922037105e1b 100644 (file)
@@ -3,7 +3,9 @@
  *
  * BabelTrace - Variant Type Converter
  *
- * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
+ *
+ * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -23,7 +25,8 @@
 static
 struct definition *_variant_definition_new(struct declaration *declaration,
                                struct definition_scope *parent_scope,
-                               GQuark field_name, int index);
+                               GQuark field_name, int index,
+                               const char *root_name);
 static
 void _variant_definition_free(struct definition *definition);
 
@@ -99,6 +102,7 @@ struct declaration_variant *
        variant_declaration = g_new(struct declaration_variant, 1);
        declaration = &variant_declaration->p;
        variant_declaration->untagged_variant = untagged_variant;
+       declaration_ref(&untagged_variant->p);
        variant_declaration->tag_name = g_array_new(FALSE, TRUE, sizeof(GQuark));
        append_scope_path(tag, variant_declaration->tag_name);
        declaration->id = CTF_TYPE_VARIANT;
@@ -161,25 +165,36 @@ static
 struct definition *
        _variant_definition_new(struct declaration *declaration,
                                struct definition_scope *parent_scope,
-                               GQuark field_name, int index)
+                               GQuark field_name, int index,
+                               const char *root_name)
 {
        struct declaration_variant *variant_declaration =
                container_of(declaration, struct declaration_variant, p);
        struct definition_variant *variant;
        unsigned long i;
+       int ret;
 
        variant = g_new(struct definition_variant, 1);
        declaration_ref(&variant_declaration->p);
        variant->p.declaration = declaration;
        variant->declaration = variant_declaration;
        variant->p.ref = 1;
-       variant->p.index = index;
+       /*
+        * Use INT_MAX order to ensure that all fields of the parent
+        * scope are seen as being prior to this scope.
+        */
+       variant->p.index = root_name ? INT_MAX : index;
        variant->p.name = field_name;
-       variant->p.path = new_definition_path(parent_scope, field_name);
-       variant->scope = new_definition_scope(parent_scope, field_name);
-       variant->enum_tag = lookup_definition(variant->scope->scope_path,
-                                             variant_declaration->tag_name,
-                                             parent_scope);
+       variant->p.path = new_definition_path(parent_scope, field_name, root_name);
+       variant->p.scope = new_definition_scope(parent_scope, field_name, root_name);
+
+       ret = register_field_definition(field_name, &variant->p,
+                                       parent_scope);
+       assert(!ret);
+
+       variant->enum_tag = lookup_path_definition(variant->p.scope->scope_path,
+                                                  variant_declaration->tag_name,
+                                                  parent_scope);
                                              
        if (!variant->enum_tag
            || check_enum_tag(variant, variant->enum_tag) < 0)
@@ -199,13 +214,15 @@ struct definition *
                 * various choices of the same field.
                 */
                *field = declaration_field->declaration->definition_new(declaration_field->declaration,
-                                                 variant->scope,
-                                                 declaration_field->name, 0);
+                                                 variant->p.scope,
+                                                 declaration_field->name, 0, NULL);
+               if (!*field)
+                       goto error;
        }
        variant->current_field = NULL;
        return &variant->p;
 error:
-       free_definition_scope(variant->scope);
+       free_definition_scope(variant->p.scope);
        declaration_unref(&variant_declaration->p);
        g_free(variant);
        return NULL;
@@ -224,7 +241,7 @@ void _variant_definition_free(struct definition *definition)
                definition_unref(field);
        }
        definition_unref(variant->enum_tag);
-       free_definition_scope(variant->scope);
+       free_definition_scope(variant->p.scope);
        declaration_unref(variant->p.declaration);
        g_free(variant);
 }
This page took 0.024442 seconds and 4 git commands to generate.