3. Preparation According to Parsing Options
3.1. Hooks for Langauges
Back to toplev_main, as we mentioned before, it’s appreciate that front-end can make new language introducation easy. In GCC, the front-end provides the famework for language, all language related treatments are designed as callback. It also bundles all these callbacks into a structure as following.
212 struct lang_hooks in langhooks.h
213 {
214
215 const char *name;
216
217
219 size_t identifier_size;
220
221
224 size_t (*tree_size) (enum tree_code);
225
226
229 unsigned int (*init_options) (unsigned int argc, const char **argv);
230
231
233 void (*initialize_diagnostics) (struct diagnostic_context *);
234
235
244 int (*handle_option) (size_t code, const char *arg, int value);
245
246
248 bool (*missing_argument) (const char *opt, size_t code);
249
250
258 bool (*post_options) (const char **);
259
260
263 bool (*init) (void);
264
265
266 void (*finish) (void);
267
268
270 void (*parse_file) (int);
271
272
273 void (*clear_binding_stack) (void);
274
275
277 HOST_WIDE_INT (*get_alias_set) (tree);
278
279
282 tree (*expand_constant) (tree);
283
284
286 rtx (*expand_expr) (tree, rtx, enum machine_mode, int, rtx *);
287
288
299 tree (*truthvalue_conversion) (tree);
300
301
308 int (*safe_from_p) (rtx, tree);
309
310
312 void (*finish_incomplete_decl) (tree);
313
314
317 int (*unsafe_for_reeval) (tree);
318
319
322 bool (*mark_addressable) (tree);
323
324
325 int (*staticp) (tree);
326
327
329 void (*dup_lang_specific_decl) (tree);
330
331
334 tree (*unsave_expr_now) (tree);
335
336
338 tree (*maybe_build_cleanup) (tree);
339
340
345 void (*set_decl_assembler_name) (tree);
346
347
349 bool (*can_use_bit_fields_p) (void);
350
351
352 bool honor_readonly;
353
354
357 bool no_body_blocks;
358
359
361 void (*print_statistics) (void);
362
363
365 lang_print_tree_hook print_xnode;
366
367
369 lang_print_tree_hook print_decl;
370 lang_print_tree_hook print_type;
371 lang_print_tree_hook print_identifier;
372
373
379 const char *(*decl_printable_name) (tree decl, int verbosity);
380
381
382 tree (*lang_get_callee_fndecl) (tree);
383
384
385 void (*print_error_function) (struct diagnostic_context *, const char *);
386
387
391 tree (*expr_size) (tree);
392
393
395 bool (*decl_uninit) (tree);
396
397
402 const struct attribute_spec *attribute_table;
403 const struct attribute_spec *common_attribute_table;
404 const struct attribute_spec *format_attribute_table;
405
406
407 struct lang_hooks_for_functions function;
408
409 struct lang_hooks_for_tree_inlining tree_inlining;
410
411 struct lang_hooks_for_callgraph callgraph;
412
413 struct lang_hooks_for_tree_dump tree_dump;
414
415 struct lang_hooks_for_decls decls;
416
417 struct lang_hooks_for_types types;
418
419 struct lang_hooks_for_rtl_expansion rtl_expand;
420
421
423 };
It is a big and complex but important data structure; it has a global instance lang_hooks. Every front-end needs set this structure. It is initialized by macro LANG_HOOKS_INITIALIZER. Front-end doesn’t need provide the whole definition, for part front-end doesn’t define, default operation is assumed (commonly is harmless empty operation or returns false value). Following, with C++ front-end, we will see the functionality one by one.
3.2. Initialization source reader
No doubt different language has different compilation options set, and their processing is front-end dependent. Routine decode_options processes compilation options for the language. Note that the function can be invoked by all front-ends.
479 void
480 decode_options (unsigned int argc, const char **argv) in opts.c
481 {
482 unsigned int i, lang_mask;
483
484
485 lang_mask = (*lang_hooks.init_options) (argc, argv);
486
487 lang_hooks.initialize_diagnostics (global_dc);
For C/C++, they share the same init_options operation. It’s c_common_init_options. Its major work is to create a reader for source files for scanning program.
188 unsigned int
189 c_common_init_options (unsigned int argc, const char **argv) in c-opts.c
190 {
191 static const unsigned int lang_flags[] = {CL_C, CL_ObjC, CL_CXX, CL_ObjCXX};
192 unsigned int i, result;
193
194
196 if (c_dialect_cxx ())
197 {
198
200 diagnostic_line_cutoff (global_dc) = 80;
201
203 diagnostic_prefixing_rule (global_dc) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
204 }
205
206 parse_in = cpp_create_reader (c_dialect_cxx () ? CLK_GNUCXX: CLK_GNUC89,
207 ident_hash);
c_dialect_cxx at line 195 is a very simple macro to check if it is C++ dialect.
254 #define c_dialect_cxx() (c_language & clk_cxx) in c-common.h
c_language is an instance of an enum type of c_language_kind. It is initialized by front-end, for example, in C++ front-end, it is set as clk_cxx.
241 typedef enum c_language_kind in c-common.h
242 {
243 clk_c = 0,
244 clk_objc = 1,
245 clk_cxx = 2,
246 clk_objcxx = 3
247 }
248 c_language_kind;
249
250
252 extern c_language_kind c_language; in c-common.h