+ if (!has_body) {
+ assert(name);
+ enum_declaration =
+ lookup_enum_declaration(g_quark_from_string(name),
+ declaration_scope);
+ return enum_declaration;
+ } else {
+ /* For unnamed enum, create type */
+ /* For named enum (with body), create type and add to declaration scope */
+ if (name) {
+ if (lookup_enum_declaration(g_quark_from_string(name),
+ declaration_scope)) {
+
+ fprintf(stderr, "[error] %s: enum %s already declared in scope\n", __func__, name);
+ return NULL;
+ }
+ }
+
+ /* TODO CHECK Enumerations need to have their size/type specifier (< >). */
+ integer_declaration = integer_declaration_new(); /* TODO ... */
+ .....
+ enum_declaration = enum_declaration_new(name, integer_declaration);
+ declaration_unref(&integer_declaration->p); /* leave ref to enum */
+ cds_list_for_each_entry(iter, enumerator_list, siblings) {
+ ret = ctf_enumerator_list_visit(fd, depth + 1, iter, enum_declaration);
+ if (ret)
+ goto error;
+ }
+ if (name) {
+ ret = register_enum_declaration(g_quark_from_string(name),
+ enum_declaration,
+ declaration_scope);
+ assert(!ret);
+ }
+ return enum_declaration;
+ }
+error:
+ enum_declaration->p.declaration_free(&enum_declaration->p);
+ return NULL;
+}
+
+static
+struct declaration *ctf_declaration_type_specifier_visit(int fd, int depth,
+ struct cds_list_head *declaration_specifier,
+ struct declaration_scope *declaration_scope)
+{
+ GString *str;
+ struct declaration *declaration;
+ const char *str_c;
+
+ str = g_string_new();
+ ret = visit_declaration_specifier(declaration_specifier, str);
+ if (ret)
+ return NULL;
+ str_c = g_string_free(str, FALSE);
+ id_q = g_quark_from_string(str_c);
+ g_free(str_c);
+ declaration = lookup_declaration(id_q, declaration_scope);
+ return declaration;
+}
+
+/*
+ * Returns 0/1 boolean, or < 0 on error.
+ */
+static
+int get_boolean(int fd, int depth, struct node *unary_expression)
+{
+ if (unary_expression->type != NODE_UNARY_EXPRESSION) {
+ fprintf(stderr, "[error] %s: expecting unary expression\n",
+ __func__);
+ return -EINVAL;
+ }
+ switch (unary_expression->u.unary_expression.type) {
+ case UNARY_UNSIGNED_CONSTANT:
+ if (unary_expression->u.unary_expression.u.unsigned_constant == 0)
+ return 0;
+ else
+ return 1;
+ case UNARY_SIGNED_CONSTANT:
+ if (unary_expression->u.unary_expression.u.signed_constant == 0)
+ return 0;
+ else
+ return 1;
+ case UNARY_STRING:
+ if (!strcmp(unary_expression->u.unary_expression.u.string, "true"))
+ return 1;
+ else if (!strcmp(unary_expression->u.unary_expression.u.string, "TRUE"))
+ return 1;
+ else if (!strcmp(unary_expression->u.unary_expression.u.string, "false"))
+ return 0;
+ else if (!strcmp(unary_expression->u.unary_expression.u.string, "FALSE"))
+ return 0;
+ else {
+ fprintf(stderr, "[error] %s: unexpected string \"%s\"\n",
+ __func__, unary_expression->u.unary_expression.u.string);
+ return -EINVAL;
+ }
+ break;
+ default:
+ fprintf(stderr, "[error] %s: unexpected unary expression type\n",
+ __func__);
+ return -EINVAL;
+ }
+
+}
+
+static
+int get_byte_order(int fd, int depth, struct node *unary_expression)
+{
+ int byte_order;
+
+ if (unary_expression->u.unary_expression.type != UNARY_STRING) {
+ fprintf(stderr, "[error] %s: byte_order: expecting string\n",
+ __func__);
+ return -EINVAL;
+ }
+ if (!strcmp(unary_expression->u.unary_expression.u.string, "native"))
+ byte_order = trace->byte_order;
+ else if (!strcmp(unary_expression->u.unary_expression.u.string, "network"))
+ byte_order = BIG_ENDIAN;
+ else if (!strcmp(unary_expression->u.unary_expression.u.string, "be"))
+ byte_order = BIG_ENDIAN;
+ else if (!strcmp(unary_expression->u.unary_expression.u.string, "le"))
+ byte_order = LITTLE_ENDIAN;
+ else {
+ fprintf(stderr, "[error] %s: unexpected string \"%s\". Should be \"native\", \"network\", \"be\" or \"le\".\n",
+ __func__, right->u.unary_expression.u.string);
+ return -EINVAL;
+ }
+ return byte_order;
+}
+
+static
+struct declaration *ctf_declaration_integer_visit(int fd, int depth,
+ struct cds_list_head *expressions,
+ struct ctf_trace *trace)
+{
+ struct node *expression;
+ uint64_t alignment, size;
+ int byte_order = trace->byte_order;
+ int signedness = 0;
+ int has_alignment = 0, has_size = 0;
+ struct declaration_integer *integer_declaration;
+
+ cds_list_for_each_entry(expression, expressions, siblings) {
+ struct node *left, *right;
+
+ left = expression->u.ctf_expression.left;
+ right = expression->u.ctf_expression.right;
+ assert(left->u.unary_expression.type == UNARY_STRING);
+ if (!strcmp(left->u.unary_expression.u.string, "signed")) {
+ signedness = get_boolean(fd, depth, right);
+ if (signedness < 0)
+ return NULL;
+ } else if (!strcmp(left->u.unary_expression.u.string, "byte_order")) {
+ byte_order = get_byte_order(fd, depth, right);
+ if (byte_order < 0)
+ return NULL;
+ } else if (!strcmp(left->u.unary_expression.u.string, "size")) {
+ if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
+ fprintf(stderr, "[error] %s: size: expecting unsigned constant\n",
+ __func__);
+ return NULL;
+ }
+ size = right->u.unary_expression.u.unsigned_constant;
+ has_size = 1;
+ } else if (!strcmp(left->u.unary_expression.u.string, "align")) {
+ if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
+ fprintf(stderr, "[error] %s: align: expecting unsigned constant\n",
+ __func__);
+ return NULL;
+ }
+ alignment = right->u.unary_expression.u.unsigned_constant;
+ has_alignment = 1;
+ } else {
+ fprintf(stderr, "[error] %s: unknown attribute name %s\n",
+ __func__, left->u.unary_expression.u.string);
+ return NULL;
+ }
+ }
+ if (!has_size) {
+ fprintf(stderr, "[error] %s: missing size attribute\n", __func__);
+ return NULL;
+ }
+ if (!has_alignment) {
+ if (size % CHAR_BIT) {
+ /* bit-packed alignment */
+ alignment = 1;
+ } else {
+ /* byte-packed alignment */
+ alignment = CHAR_BIT;
+ }
+ }
+ integer_declaration = integer_declaration_new(size,
+ byte_order, signedness, alignment);
+ return &integer_declaration->p;
+}
+
+static
+struct declaration *ctf_declaration_floating_point_visit(int fd, int depth,
+ struct cds_list_head *expressions,
+ struct ctf_trace *trace)
+{
+ struct node *expression;
+ uint64_t alignment, exp_dig, mant_dig, byte_order = trace->byte_order;
+ int has_alignment = 0, has_exp_dig = 0, has_mant_dig = 0;
+ struct declaration_float *float_declaration;
+
+ cds_list_for_each_entry(expression, expressions, siblings) {
+ struct node *left, *right;
+
+ left = expression->u.ctf_expression.left;
+ right = expression->u.ctf_expression.right;
+ assert(left->u.unary_expression.type == UNARY_STRING);
+ if (!strcmp(left->u.unary_expression.u.string, "byte_order")) {
+ byte_order = get_byte_order(fd, depth, right);
+ if (byte_order < 0)
+ return NULL;
+ } else if (!strcmp(left->u.unary_expression.u.string, "exp_dig")) {
+ if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
+ fprintf(stderr, "[error] %s: exp_dig: expecting unsigned constant\n",
+ __func__);
+ return NULL;
+ }
+ exp_dig = right->u.unary_expression.u.unsigned_constant;
+ has_exp_dig = 1;
+ } else if (!strcmp(left->u.unary_expression.u.string, "mant_dig")) {
+ if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
+ fprintf(stderr, "[error] %s: mant_dig: expecting unsigned constant\n",
+ __func__);
+ return NULL;
+ }
+ mant_dig = right->u.unary_expression.u.unsigned_constant;
+ has_mant_dig = 1;
+ } else if (!strcmp(left->u.unary_expression.u.string, "align")) {
+ if (right->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) {
+ fprintf(stderr, "[error] %s: align: expecting unsigned constant\n",
+ __func__);
+ return NULL;
+ }
+ alignment = right->u.unary_expression.u.unsigned_constant;
+ has_alignment = 1;
+ } else {
+ fprintf(stderr, "[error] %s: unknown attribute name %s\n",
+ __func__, left->u.unary_expression.u.string);
+ return NULL;
+ }
+ }
+ if (!has_mant_dig) {
+ fprintf(stderr, "[error] %s: missing mant_dig attribute\n", __func__);
+ return NULL;
+ }
+ if (!has_exp_dig) {
+ fprintf(stderr, "[error] %s: missing exp_dig attribute\n", __func__);
+ return NULL;
+ }
+ if (!has_alignment) {
+ if ((mant_dig + exp_dig) % CHAR_BIT) {
+ /* bit-packed alignment */
+ alignment = 1;
+ } else {
+ /* byte-packed alignment */
+ alignment = CHAR_BIT;
+ }
+ }
+ float_declaration = float_declaration_new(mant_dig, exp_dig,
+ byte_order, alignment);
+ return &float_declaration->p;
+}
+
+static
+struct declaration *ctf_declaration_string_visit(int fd, int depth,
+ struct cds_list_head *expressions,
+ struct ctf_trace *trace)
+{
+ struct node *expression;
+ const char *encoding_c = NULL;
+ enum ctf_string_encoding encoding = CTF_STRING_UTF8;
+ struct declaration_string *string_declaration;
+
+ cds_list_for_each_entry(expression, expressions, siblings) {
+ struct node *left, *right;
+
+ left = expression->u.ctf_expression.left;
+ right = expression->u.ctf_expression.right;
+ assert(left->u.unary_expression.type == UNARY_STRING);
+ if (!strcmp(left->u.unary_expression.u.string, "encoding")) {
+ if (right->u.unary_expression.type != UNARY_UNSIGNED_STRING) {
+ fprintf(stderr, "[error] %s: encoding: expecting string\n",
+ __func__);
+ return NULL;
+ }
+ encoding_c = right->u.unary_expression.u.string;
+ } else {
+ fprintf(stderr, "[error] %s: unknown attribute name %s\n",
+ __func__, left->u.unary_expression.u.string);
+ return NULL;
+ }
+ }
+ if (encoding_c && !strcmp(encoding_c, "ASCII"))
+ encoding = CTF_STRING_ASCII;
+ string_declaration = string_declaration_new(encoding);
+ return &string_declaration->p;
+}
+
+
+/*
+ * Also add named variant, struct or enum to the current declaration scope.
+ */
+static
+struct declaration *ctf_declaration_specifier_visit(FILE *fd,
+ int depth, struct cds_list_head *head,
+ struct declaration_scope *declaration_scope,
+ struct ctf_trace *trace)
+{
+ struct declaration *declaration;
+ struct node *first;