Updating compiler to commit dabfa73 from upstream dated Oct 1 2016. These changes...
[inform.git] / files.c
diff --git a/files.c b/files.c
index fc0ad8ce23422b64b0b9f64f4c7f93a11143041e..4e7377e4cb9c661db45c0abbb59c484333063aab 100644 (file)
--- a/files.c
+++ b/files.c
 
 #include "header.h"
 
-int input_file;                         /* Number of source files so far     */
+int total_files;                        /* Number of files so far, including 
+                                           #include and #origsource files    */
+int total_input_files;                  /* Number of source files so far
+                                           (excludes #origsource)            */
+int current_input_file;                 /* Most recently-opened source file  */
+static int current_origsource_file;     /* Most recently-used #origsource    */
 
 int32 total_chars_read;                 /* Characters read in (from all
                                            source files put together)        */
@@ -114,12 +119,12 @@ extern void load_sourcefile(char *filename_given, int same_directory_flag)
     int x = 0;
     FILE *handle;
 
-    if (input_file == MAX_SOURCE_FILES)
+    if (total_files == MAX_SOURCE_FILES)
         memoryerror("MAX_SOURCE_FILES", MAX_SOURCE_FILES);
 
     do
     {   x = translate_in_filename(x, name, filename_given, same_directory_flag,
-                (input_file==0)?1:0);
+                (total_files==0)?1:0);
         handle = fopen(name,"r");
     } while ((handle == NULL) && (x != 0));
 
@@ -128,12 +133,12 @@ extern void load_sourcefile(char *filename_given, int same_directory_flag)
 
     filename_storage_left -= strlen(name)+1;
     strcpy(filename_storage_p, name);
-    InputFiles[input_file].filename = filename_storage_p;
+    InputFiles[total_files].filename = filename_storage_p;
 
     filename_storage_p += strlen(name)+1;
 
     if (debugfile_switch)
-    {   debug_file_printf("<source index=\"%d\">", input_file);
+    {   debug_file_printf("<source index=\"%d\">", total_files);
         debug_file_printf("<given-path>");
         debug_file_print_with_entities(filename_given);
         debug_file_printf("</given-path>");
@@ -148,13 +153,17 @@ extern void load_sourcefile(char *filename_given, int same_directory_flag)
         debug_file_printf("</source>");
     }
 
-    InputFiles[input_file].handle = handle;
-    if (InputFiles[input_file].handle==NULL)
+    InputFiles[total_files].handle = handle;
+    if (InputFiles[total_files].handle==NULL)
         fatalerror_named("Couldn't open source file", name);
 
+    InputFiles[total_files].is_input = TRUE;
+
     if (line_trace_level > 0) printf("\nOpening file \"%s\"\n",name);
 
-    input_file++;
+    total_files++;
+    total_input_files++;
+    current_input_file = total_files;
 }
 
 static void close_sourcefile(int file_number)
@@ -176,7 +185,67 @@ static void close_sourcefile(int file_number)
 
 extern void close_all_source(void)
 {   int i;
-    for (i=0; i<input_file; i++) close_sourcefile(i+1);
+    for (i=0; i<total_files; i++) close_sourcefile(i+1);
+}
+
+/* ------------------------------------------------------------------------- */
+/*   Register an #origsource filename. This goes in the InputFiles table,    */
+/*   but we do not open the file or advance current_input_file.              */
+/* ------------------------------------------------------------------------- */
+
+extern int register_orig_sourcefile(char *filename)
+{
+    int ix;
+
+    /* If the filename has already been used as an origsource filename,
+       return that entry. We check the most-recently-used file first, and
+       then search the list. */
+    if (current_origsource_file > 0 && current_origsource_file <= total_files) {
+        if (!strcmp(filename, InputFiles[current_origsource_file-1].filename))
+            return current_origsource_file;
+    }
+
+    for (ix=0; ix<total_files; ix++) {
+        if (InputFiles[ix].is_input)
+            continue;
+        if (!strcmp(filename, InputFiles[ix].filename)) {
+            current_origsource_file = ix+1;
+            return current_origsource_file;
+        }
+    }
+
+    /* This filename has never been used before. Allocate a new InputFiles
+       entry. */
+
+    char *name = filename; /* no translation */
+
+    if (total_files == MAX_SOURCE_FILES)
+        memoryerror("MAX_SOURCE_FILES", MAX_SOURCE_FILES);
+
+    if (filename_storage_left <= (int)strlen(name))
+        memoryerror("MAX_SOURCE_FILES", MAX_SOURCE_FILES);
+
+    filename_storage_left -= strlen(name)+1;
+    strcpy(filename_storage_p, name);
+    InputFiles[total_files].filename = filename_storage_p;
+
+    filename_storage_p += strlen(name)+1;
+
+    if (debugfile_switch)
+    {   debug_file_printf("<source index=\"%d\">", total_files);
+        debug_file_printf("<given-path>");
+        debug_file_print_with_entities(filename);
+        debug_file_printf("</given-path>");
+        debug_file_printf("<language>Inform 7</language>");
+        debug_file_printf("</source>");
+    }
+
+    InputFiles[total_files].handle = NULL;
+    InputFiles[total_files].is_input = FALSE;
+
+    total_files++;
+    current_origsource_file = total_files;
+    return current_origsource_file;
 }
 
 /* ------------------------------------------------------------------------- */
@@ -188,7 +257,7 @@ extern int file_load_chars(int file_number, char *buffer, int length)
 {
     int read_in; FILE *handle;
 
-    if (file_number-1 > input_file)
+    if (file_number-1 > total_files)
     {   buffer[0] = 0; return 1; }
 
     handle = InputFiles[file_number-1].handle;
@@ -1369,12 +1438,28 @@ static void write_debug_location_internals(debug_location location)
     }
 }
 
+static void write_debug_location_origsource_internals(debug_location location)
+{   debug_file_printf
+        ("<file-index>%d</file-index>", location.orig_file_index - 1);
+    if (location.orig_beg_line_number)
+        debug_file_printf
+            ("<line>%d</line>", location.orig_beg_line_number);
+    if (location.orig_beg_char_number)
+        debug_file_printf
+            ("<character>%d</character>", location.orig_beg_char_number);
+}
+
 extern void write_debug_location(debug_location location)
 {   if (location.file_index && location.file_index != 255)
     {   debug_file_printf("<source-code-location>");
         write_debug_location_internals(location);
         debug_file_printf("</source-code-location>");
     }
+    if (location.orig_file_index)
+    {   debug_file_printf("<source-code-location>");
+        write_debug_location_origsource_internals(location);
+        debug_file_printf("</source-code-location>");
+    }
 }
 
 extern void write_debug_locations(debug_locations locations)
@@ -1386,6 +1471,11 @@ extern void write_debug_locations(debug_locations locations)
             write_debug_location_internals(current->location);
             debug_file_printf("</source-code-location>");
         }
+        if (locations.location.orig_file_index)
+        {   debug_file_printf("<source-code-location>");
+            write_debug_location_origsource_internals(locations.location);
+            debug_file_printf("</source-code-location>");
+        }
     }
     else
     {   write_debug_location(locations.location);
@@ -1725,7 +1815,11 @@ extern void init_files_vars(void)
 }
 
 extern void files_begin_prepass(void)
-{   input_file = 0;
+{   
+    total_files = 0;
+    total_input_files = 0;
+    current_input_file = 0;
+    current_origsource_file = 0;
 }
 
 extern void files_begin_pass(void)