X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=wrappergen%2Fwrite_jna.c;h=aa9ed365c816e7e3c2d8e472d3514d11d104a2f7;hb=779610287a11b207e958e31a29f0dd9ea5459e39;hp=1739481ba1f8b6e1cd2df8e9ca53951a2b468529;hpb=25e87415eecbd8d7cd3811d745568e70cec17bf1;p=cparser diff --git a/wrappergen/write_jna.c b/wrappergen/write_jna.c index 1739481..aa9ed36 100644 --- a/wrappergen/write_jna.c +++ b/wrappergen/write_jna.c @@ -1,27 +1,12 @@ /* * This file is part of cparser. - * Copyright (C) 2007-2009 Matthias Braun - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA - * 02111-1307, USA. + * Copyright (C) 2012 Matthias Braun */ #include -#include #include +#include "adt/strutil.h" #include "write_jna.h" #include "symbol_t.h" #include "ast_t.h" @@ -31,7 +16,9 @@ #include "printer.h" #include "adt/error.h" #include "adt/xmalloc.h" -#include +#include "adt/pset_new.h" +#include "separator_t.h" +#include "symbol_table.h" typedef struct output_limit { const char *filename; @@ -44,31 +31,17 @@ static pset_new_t avoid_symbols; static output_limit *output_limits; static const char *libname; -static bool is_system_header(const char *fname) -{ - if (strncmp(fname, "/usr/include", 12) == 0) - return true; - if (fname == builtin_source_position.input_name) - return true; - return false; -} - static const char *fix_builtin_names(const char *name) { - if (strcmp(name, "class") == 0) { - return "_class"; - } else if(strcmp(name, "this") == 0) { - return "_this"; - } else if(strcmp(name, "public") == 0) { - return "_public"; - } else if(strcmp(name, "protected") == 0) { - return "_protected"; - } else if(strcmp(name, "private") == 0) { - return "_private"; - } else if(strcmp(name, "final") == 0) { - return "_final"; - } +#define FIX(x) if (streq(name, x)) return "_" x + FIX("class"); + FIX("final"); + FIX("private"); + FIX("protected"); + FIX("public"); + FIX("this"); /* TODO put all reserved names here */ +#undef FIX return name; } @@ -225,7 +198,7 @@ static void write_type(type_t *type) case TYPE_ERROR: case TYPE_TYPEOF: case TYPE_TYPEDEF: - panic("invalid type found"); + panic("invalid type"); case TYPE_ARRAY: case TYPE_REFERENCE: case TYPE_FUNCTION: @@ -274,7 +247,7 @@ static void write_unary_expression(const unary_expression_t *expression) write_expression(expression->value); return; default: - panic("unimeplemented unary expression found"); + panic("unimplemented unary expression"); } write_expression(expression->value); } @@ -302,28 +275,32 @@ static void write_binary_expression(const binary_expression_t *expression) fputs(")", out); } +static void write_integer(const literal_expression_t *literal) +{ + for (const char *c = literal->value.begin; c != literal->suffix; ++c) { + fputc(*c, out); + } +} + static void write_expression(const expression_t *expression) { /* TODO */ switch(expression->kind) { case EXPR_LITERAL_INTEGER: - case EXPR_LITERAL_INTEGER_OCTAL: - fprintf(out, "%s", expression->literal.value.begin); - break; - case EXPR_LITERAL_INTEGER_HEXADECIMAL: - fprintf(out, "0x%s", expression->literal.value.begin); + write_integer(&expression->literal); break; - case EXPR_REFERENCE_ENUM_VALUE: { + + case EXPR_ENUM_CONSTANT: { /* UHOH... hacking */ entity_t *entity = expression->reference.entity; write_enum_name(& entity->enum_value.enum_type->enumt); fprintf(out, ".%s.val", entity->base.symbol->string); break; } - EXPR_UNARY_CASES + case EXPR_UNARY_CASES: write_unary_expression(&expression->unary); break; - EXPR_BINARY_CASES + case EXPR_BINARY_CASES: write_binary_expression(&expression->binary); break; default: @@ -399,7 +376,7 @@ static void write_variable(const entity_t *entity) static void write_function(const entity_t *entity) { - if (entity->function.statement != NULL) { + if (entity->function.body != NULL) { fprintf(stderr, "Warning: can't convert function bodies (at %s)\n", entity->base.symbol->string); return; @@ -415,16 +392,12 @@ static void write_function(const entity_t *entity) write_type(return_type); fprintf(out, " %s(", entity->base.symbol->string); - entity_t *parameter = entity->function.parameters.entities; - int first = 1; - int n = 0; + entity_t *parameter = entity->function.parameters.entities; + separator_t sep = { "", ", " }; + int n = 0; for ( ; parameter != NULL; parameter = parameter->base.next) { assert(parameter->kind == ENTITY_PARAMETER); - if(!first) { - fprintf(out, ", "); - } else { - first = 0; - } + fputs(sep_next(&sep), out); write_type(parameter->declaration.type); if(parameter->base.symbol != NULL) { fprintf(out, " %s", fix_builtin_names(parameter->base.symbol->string)); @@ -433,11 +406,7 @@ static void write_function(const entity_t *entity) } } if(function_type->variadic) { - if(!first) { - fprintf(out, ", "); - } else { - first = 0; - } + fputs(sep_next(&sep), out); fputs("Object ... args", out); } fprintf(out, ");\n"); @@ -484,7 +453,7 @@ void write_jna_decls(FILE *output, const translation_unit_t *unit) /* read the avoid list */ FILE *avoid = fopen("avoid.config", "r"); if (avoid != NULL) { - while (!feof(avoid)) { + for (;;) { char buf[1024]; char *res = fgets(buf, sizeof(buf), avoid); if (res == NULL) @@ -519,8 +488,7 @@ void write_jna_decls(FILE *output, const translation_unit_t *unit) } #if 0 - if(type->kind == TYPE_COMPOUND_STRUCT - || type->kind == TYPE_COMPOUND_UNION) { + if (is_type_compound(type)) { write_compound(entity->base.symbol, &type->compound); } #endif @@ -531,16 +499,16 @@ void write_jna_decls(FILE *output, const translation_unit_t *unit) for ( ; entity != NULL; entity = entity->base.next) { if (entity->kind != ENTITY_FUNCTION) continue; - const char *input_name = entity->base.source_position.input_name; - if (is_system_header(input_name)) + if (entity->base.pos.is_system_header) continue; if (entity->function.elf_visibility != ELF_VISIBILITY_DEFAULT) continue; if (output_limits != NULL) { - bool in_limits = false; + bool in_limits = false; + char const *const input_name = entity->base.pos.input_name; for (output_limit *limit = output_limits; limit != NULL; limit = limit->next) { - if (strcmp(limit->filename, input_name) == 0) { + if (streq(limit->filename, input_name)) { in_limits = true; break; }