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