]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/java/resource.c
This patch rewrites the old VEC macro-based interface into a new one
[thirdparty/gcc.git] / gcc / java / resource.c
CommitLineData
caa8fa37 1/* Functions related to building resource files.
e4b52719 2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
967958e4 3 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
caa8fa37 4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
e4b52719 9the Free Software Foundation; either version 3, or (at your option)
caa8fa37 10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
e4b52719 18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>.
caa8fa37 20
21Java and all Java-based marks are trademarks or registered trademarks
22of Sun Microsystems, Inc. in the United States and other countries.
23The Free Software Foundation is independent of Sun Microsystems, Inc. */
24
25#include "config.h"
26#include "system.h"
27#include "coretypes.h"
caa8fa37 28#include "tree.h"
caa8fa37 29#include "java-tree.h"
30#include "jcf.h"
0b205f4c 31#include "diagnostic-core.h"
caa8fa37 32#include "toplev.h"
caa8fa37 33#include "parse.h"
34#include "function.h"
35#include "ggc.h"
b5530559 36#include "tree-iterator.h"
c1dcd13c 37#include "cgraph.h"
caa8fa37 38
caa8fa37 39/* A list of all the resources files. */
f1f41a6c 40static GTY(()) vec<tree, va_gc> *resources;
caa8fa37 41
caa8fa37 42void
35171f45 43compile_resource_data (const char *name, const char *buffer, int length)
caa8fa37 44{
45 tree rtype, field = NULL_TREE, data_type, rinit, data, decl;
f1f41a6c 46 vec<constructor_elt, va_gc> *v = NULL;
caa8fa37 47
48 data_type = build_prim_array_type (unsigned_byte_type_node,
49 strlen (name) + length);
50 rtype = make_node (RECORD_TYPE);
e60a6f7b 51 PUSH_FIELD (input_location,
52 rtype, field, "name_length", unsigned_int_type_node);
53 PUSH_FIELD (input_location,
54 rtype, field, "resource_length", unsigned_int_type_node);
55 PUSH_FIELD (input_location, rtype, field, "data", data_type);
caa8fa37 56 FINISH_RECORD (rtype);
3eafef1a 57 START_RECORD_CONSTRUCTOR (v, rtype);
58 PUSH_FIELD_VALUE (v, "name_length",
7016c612 59 build_int_cst (NULL_TREE, strlen (name)));
3eafef1a 60 PUSH_FIELD_VALUE (v, "resource_length",
7016c612 61 build_int_cst (NULL_TREE, length));
caa8fa37 62 data = build_string (strlen(name) + length, buffer);
63 TREE_TYPE (data) = data_type;
3eafef1a 64 PUSH_FIELD_VALUE (v, "data", data);
65 FINISH_RECORD_CONSTRUCTOR (rinit, v, rtype);
caa8fa37 66 TREE_CONSTANT (rinit) = 1;
67
e60a6f7b 68 decl = build_decl (input_location,
69 VAR_DECL, java_mangle_resource_name (name), rtype);
caa8fa37 70 TREE_STATIC (decl) = 1;
0ac26f75 71 TREE_PUBLIC (decl) = 1;
72 java_hide_decl (decl);
caa8fa37 73 DECL_ARTIFICIAL (decl) = 1;
74 DECL_IGNORED_P (decl) = 1;
75 TREE_READONLY (decl) = 1;
76 TREE_THIS_VOLATILE (decl) = 0;
77 DECL_INITIAL (decl) = rinit;
78 layout_decl (decl, 0);
79 pushdecl (decl);
b2c4af5e 80 rest_of_decl_compilation (decl, global_bindings_p (), 0);
1d416bd7 81 varpool_finalize_decl (decl);
caa8fa37 82
f1f41a6c 83 vec_safe_push (resources, decl);
caa8fa37 84}
85
86void
b5530559 87write_resource_constructor (tree *list_p)
caa8fa37 88{
43d03f3a 89 tree decl, t, register_resource_fn;
90 unsigned ix;
caa8fa37 91
b5530559 92 if (resources == NULL)
caa8fa37 93 return;
94
b5530559 95 t = build_function_type_list (void_type_node, ptr_type_node, NULL);
e60a6f7b 96 t = build_decl (input_location,
97 FUNCTION_DECL, get_identifier ("_Jv_RegisterResource"), t);
b5530559 98 TREE_PUBLIC (t) = 1;
99 DECL_EXTERNAL (t) = 1;
100 register_resource_fn = t;
caa8fa37 101
102 /* Write out entries in the same order in which they were defined. */
f1f41a6c 103 FOR_EACH_VEC_ELT (*resources, ix, decl)
caa8fa37 104 {
43d03f3a 105 t = build_fold_addr_expr (decl);
c2f47e15 106 t = build_call_expr (register_resource_fn, 1, t);
b5530559 107 append_to_statement_list (t, list_p);
caa8fa37 108 }
caa8fa37 109}
110
111/* Generate a byte array representing the contents of FILENAME. The
112 array is assigned a unique local symbol. The array represents a
113 compiled Java resource, which is accessed by the runtime using
114 NAME. */
115void
35171f45 116compile_resource_file (const char *name, const char *filename)
caa8fa37 117{
118 struct stat stat_buf;
119 int fd;
120 char *buffer;
121
122 fd = open (filename, O_RDONLY | O_BINARY);
123 if (fd < 0)
124 {
125 perror ("Failed to read resource file");
126 return;
127 }
128 if (fstat (fd, &stat_buf) != 0
129 || ! S_ISREG (stat_buf.st_mode))
130 {
131 perror ("Could not figure length of resource file");
132 return;
133 }
4c36ffe6 134 buffer = XNEWVEC (char, strlen (name) + stat_buf.st_size);
caa8fa37 135 strcpy (buffer, name);
136 read (fd, buffer + strlen (name), stat_buf.st_size);
137 close (fd);
138
139 compile_resource_data (name, buffer, stat_buf.st_size);
caa8fa37 140}
141
142#include "gt-java-resource.h"