home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / gcc-2.3.3-src.lha / GNU / src / amiga / gcc-2.3.3 / cp-edsel.c < prev    next >
C/C++ Source or Header  |  1994-02-06  |  20KB  |  928 lines

  1. /* Interface to LUCID Cadillac system for GNU compiler.
  2.    Copyright (C) 1988, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "config.h"
  21.  
  22. #include "tree.h"
  23. #include "flags.h"
  24. #include <stdio.h>
  25. #include "cp-tree.h"
  26. #include "obstack.h"
  27.  
  28. #ifdef CADILLAC
  29. #include <compilerreq.h>
  30. #include <compilerconn.h>
  31. #include <sys/time.h>
  32. #include <sys/types.h>
  33. #include <errno.h>
  34. #include <sys/file.h>
  35.  
  36. #define obstack_chunk_alloc xmalloc
  37. #define obstack_chunk_free free
  38.  
  39. void init_cadillac ();
  40.  
  41. extern char *input_filename;
  42. extern int lineno;
  43.  
  44. /* Put random information we might want to get back from
  45.    Cadillac here.  */
  46. typedef struct
  47. {
  48.   /* The connection to the Cadillac kernel.  */
  49.   Connection *conn;
  50.  
  51.   /* Input and output file descriptors for Cadillac.  */
  52.   short fd_input, fd_output;
  53.  
  54.   /* #include nesting of current file.  */
  55.   short depth;
  56.  
  57.   /* State variables for the connection.  */
  58.   char messages;
  59.   char conversion;
  60.   char emission;
  61.   char process_until;
  62.  
  63.   /* #if level of current file.  */
  64.   int iflevel;
  65.  
  66.   /* Line number that starts current source file.  */
  67.   int lineno;
  68.  
  69.   /* Name of current file.  */
  70.   char *filename;
  71.  
  72.   /* Where to stop processing (if process_until is set).  */
  73.   char *end_filename;
  74.   int end_position;
  75.  
  76. } cadillac_struct;
  77. static cadillac_struct cadillacObj;
  78.  
  79. /* Nonzero if in the process of exiting.  */
  80. static int exiting;
  81.  
  82. void cadillac_note_source ();
  83. static void CWriteLanguageDecl ();
  84. static void CWriteLanguageType ();
  85. static void CWriteTopLevel ();
  86. static void cadillac_note_filepos ();
  87. static void cadillac_process_request (), cadillac_process_requests ();
  88. static void cadillac_switch_source ();
  89. static void exit_cadillac ();
  90.  
  91. /* Blocking test.  */
  92. static int
  93. readable_p (fd)
  94.      int fd;
  95. {
  96.   fd_set f;
  97.  
  98.   FD_ZERO (&f);
  99.   FD_SET (fd, &f);
  100.  
  101.   return select (32, &f, NULL, NULL, 0) == 1;
  102. }
  103.  
  104. static CObjectType *tree_to_cadillac_map;
  105. struct obstack cadillac_obstack;
  106.  
  107.  
  108. #include "stack.h"
  109.  
  110. struct context_level
  111. {
  112.   struct stack_level base;
  113.  
  114.   tree context;
  115. };
  116.  
  117. /* Stack for maintaining contexts (in case functions or types are nested).
  118.    When defining a struct type, the `context' field is the RECORD_TYPE.
  119.    When defining a function, the `context' field is the FUNCTION_DECL.  */
  120.  
  121. static struct context_level *context_stack;
  122.  
  123. static struct context_level *
  124. push_context_level (stack, obstack)
  125.      struct stack_level *stack;
  126.      struct obstack *obstack;
  127. {
  128.   struct context_level tem;
  129.  
  130.   tem.base.prev = stack;
  131.   return (struct context_level *)push_stack_level (obstack, &tem, sizeof (tem));
  132. }
  133.  
  134. /* Discard a level of search allocation.  */
  135.  
  136. static struct context_level *
  137. pop_context_level (stack)
  138.      struct context_level *stack;
  139. {
  140.   stack = (struct context_level *)pop_stack_level (stack);
  141.   return stack;
  142. }
  143.  
  144. void
  145. init_cadillac ()
  146. {
  147.   extern FILE *finput;
  148.   extern int errno;
  149.   CCompilerMessage* req;
  150.   cadillac_struct *cp = &cadillacObj;
  151.   int i;
  152.  
  153.   if (! flag_cadillac)
  154.     return;
  155.  
  156.   tree_to_cadillac_map = (CObjectType*) xmalloc (sizeof (CObjectType) * LAST_CPLUS_TREE_CODE);
  157.   for (i = 0; i < LAST_CPLUS_TREE_CODE; i++)
  158.     tree_to_cadillac_map[i] = MiscOType;
  159.   tree_to_cadillac_map[RECORD_TYPE] = StructOType;
  160.   tree_to_cadillac_map[UNION_TYPE] = UnionOType;
  161.   tree_to_cadillac_map[ENUMERAL_TYPE] = EnumTypeOType;
  162.   tree_to_cadillac_map[TYPE_DECL] = TypedefOType;
  163.   tree_to_cadillac_map[VAR_DECL] = VariableOType;
  164.   tree_to_cadillac_map[CONST_DECL] = EnumConstantOType;
  165.   tree_to_cadillac_map[FUNCTION_DECL] = FunctionOType;
  166.   tree_to_cadillac_map[FIELD_DECL] = FieldOType;
  167.  
  168. #ifdef sun
  169.   on_exit (&exit_cadillac, 0);
  170. #endif
  171.  
  172.   gcc_obstack_init (&cadillac_obstack);
  173.  
  174.   /* Yow!  This is the way Cadillac was designed to deal with
  175.      Oregon C++ compiler!  */
  176.   cp->fd_input = flag_cadillac;
  177.   cp->fd_output = flag_cadillac;
  178.  
  179.   /* Start in "turned-on" state.  */
  180.   cp->messages = 1;
  181.   cp->conversion = 1;
  182.   cp->emission = 1;
  183.  
  184.   /* Establish a connection with Cadillac here.  */
  185.   cp->conn = NewConnection (cp, cp->fd_input, cp->fd_output);
  186.  
  187.   CWriteHeader (cp->conn, WaitingMType, 0);
  188.   CWriteRequestBuffer (cp->conn);
  189.  
  190.   if (!readable_p (cp->fd_input))
  191.     ;
  192.  
  193.   req = CReadCompilerMessage (cp->conn);
  194.  
  195.   if (!req)
  196.     switch (errno)
  197.       {
  198.       case EWOULDBLOCK:
  199.     sleep (5);
  200.     return;
  201.       
  202.       case 0:
  203.     fatal ("init_cadillac: EOF on connection to kernel, exiting\n");
  204.     break;
  205.  
  206.       default:
  207.     perror ("Editor to kernel connection");
  208.     exit (0);
  209.       }
  210. }
  211.  
  212. static void
  213. cadillac_process_requests (conn)
  214.      Connection *conn;
  215. {
  216.   CCompilerMessage *req;
  217.   while (req = (CCompilerMessage*) CPeekNextRequest (conn))
  218.     {
  219.       req = CReadCompilerMessage (conn);
  220.       cadillac_process_request (&cadillacObj, req);
  221.     }
  222. }
  223.  
  224. static void
  225. cadillac_process_request (cp, req)
  226.      cadillac_struct *cp;
  227.      CCompilerMessage *req;
  228. {
  229.   if (! req)
  230.     return;
  231.  
  232.   switch (req->reqType)
  233.     {
  234.     case ProcessUntilMType:
  235.       if (cp->process_until)
  236.     my_friendly_abort (23);
  237.       cp->process_until = 1;
  238.       /* This is not really right.  */
  239.       cp->end_position = ((CCompilerCommand*)req)->processuntil.position;
  240. #if 0
  241.       cp->end_filename = req->processuntil.filename;
  242. #endif
  243.       break;
  244.  
  245.     case CommandMType:
  246.       switch (req->header.data)
  247.     {
  248.     case MessagesOnCType:
  249.       cp->messages = 1;
  250.       break;
  251.     case MessagesOffCType:
  252.       cp->messages = 0;
  253.       break;
  254.     case ConversionOnCType:
  255.       cp->conversion = 1;
  256.       break;
  257.     case ConversionOffCType:
  258.       cp->conversion = 0;
  259.       break;
  260.     case EmissionOnCType:
  261.       cp->emission = 1;
  262.       break;
  263.     case EmissionOffCType:
  264.       cp->emission = 0;
  265.       break;
  266.  
  267.     case FinishAnalysisCType:
  268.       return;
  269.  
  270.     case PuntAnalysisCType:
  271.     case ContinueAnalysisCType:
  272.     case GotoFileposCType:
  273.     case OpenSucceededCType:
  274.     case OpenFailedCType:
  275.       fprintf (stderr, "request type %d not implemented\n", req->reqType);
  276.       return;
  277.  
  278.     case DieCType:
  279.       if (! exiting)
  280.         my_friendly_abort (24);
  281.       return;
  282.  
  283.     }
  284.       break;
  285.  
  286.     default:
  287.       fatal ("unknown request type %d", req->reqType);
  288.     }
  289. }
  290.  
  291. void
  292. cadillac_start ()
  293. {
  294.   Connection *conn = cadillacObj.conn;
  295.   CCompilerMessage *req;
  296.  
  297.   /* Let Cadillac know that we start in C++ language scope.  */
  298.   CWriteHeader (conn, ForeignLinkageMType, LinkCPlus);
  299.   CWriteLength (conn);
  300.   CWriteRequestBuffer (conn);
  301.  
  302.   cadillac_process_requests (conn);
  303. }
  304.  
  305. static void
  306. cadillac_printf (msg, name)
  307. {
  308.   if (cadillacObj.messages)
  309.     printf ("[%s,%4d] %s `%s'\n", input_filename, lineno, msg, name);
  310. }
  311.  
  312. void
  313. cadillac_start_decl (decl)
  314.      tree decl;
  315. {
  316.   Connection *conn = cadillacObj.conn;
  317.   CObjectType object_type = tree_to_cadillac_map [TREE_CODE (decl)];
  318.  
  319.   if (context_stack)
  320.     switch (TREE_CODE (context_stack->context))
  321.       {
  322.       case FUNCTION_DECL:
  323.     /* Currently, cadillac only implements top-level forms.  */
  324.     return;
  325.       case RECORD_TYPE:
  326.       case UNION_TYPE:
  327.     cadillac_printf ("start class-level decl", IDENTIFIER_POINTER (DECL_NAME (decl)));
  328.     break;
  329.       default:
  330.     my_friendly_abort (25);
  331.       }
  332.   else
  333.     {
  334.       cadillac_printf ("start top-level decl", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
  335.       CWriteTopLevel (conn, StartMType);
  336.     }
  337.  
  338.   CWriteLanguageDecl (conn, decl, tree_to_cadillac_map[TREE_CODE (decl)]);
  339.   CWriteRequestBuffer (conn);
  340.   cadillac_process_requests (conn);
  341. }
  342.  
  343. void
  344. cadillac_finish_decl (decl)
  345.      tree decl;
  346. {
  347.   Connection *conn = cadillacObj.conn;
  348.  
  349.   if (context_stack)
  350.     switch (TREE_CODE (context_stack->context))
  351.       {
  352.       case FUNCTION_DECL:
  353.     return;
  354.       case RECORD_TYPE:
  355.       case UNION_TYPE:
  356.     cadillac_printf ("end class-level decl", IDEN