/* If unit.entry is a global, build an OP_VAR_VALUE expression node 
   and return true.  Else return false. */
static int
m3_find_global ( char *unit, char * entry )

{
    struct symbol *ir;
    struct type *exports;
    int i;

    /* try the module first */
    if ((ir = m3_unit_name_globals_symbol ('M', unit, NULL ))) {
      if (m3_find_rec_field (SYMBOL_TYPE (ir), entry, 0, 0, 0)) {
	write_exp_var (ir, block_found);
	write_exp_text (STRUCTOP_M3_MODULE, entry, strlen (entry));
	return 1;
      }
  
      /* Could it be a global name in one of the interfaces
	 explicitly exported by the current unit ? */
      if ((exports = find_m3_exported_interfaces (unit))) {
	for ( i = 0; i < TYPE_NFIELDS ( exports ); i++ ) {
	  if ( ir = m3_unit_name_globals_symbol 
                      ('I', TYPE_FIELD_NAME (exports, i), NULL )
             ) 
          { if (m3_find_rec_field (SYMBOL_TYPE (ir), entry, 0, 0, 0)) {
	      write_exp_var (ir, block_found);
	      write_exp_text (STRUCTOP_M3_INTERFACE, entry, strlen (entry));
	      return 1;
	    }
	  }
	}
      }
    }

    /* try the interface */
    if ((ir = m3_unit_name_globals_symbol ('I', unit, NULL ))) {
      if (m3_find_rec_field (SYMBOL_TYPE (ir), entry, 0, 0, 0)) {
	write_exp_var (ir, block_found);
	write_exp_text (STRUCTOP_M3_INTERFACE, entry, strlen (entry));
	return 1;
      }
    }

    /* last chance: procedures that use direct calls ("-all_direct")
       don't have entries in the interface record, but they do
       have entries in the minimal symbol table of the form
       "unit__entry" */
    {
      struct symbol *sym;
      char tmp[500];

      strcpy (tmp, unit);
      strcat (tmp, "__");
      strcat (tmp, entry);
      if ((sym = lookup_symbol (tmp, expression_context_block,
			        VAR_DOMAIN, 0, NULL)) != 0
 	  && sym->aclass != LOC_STATIC) {
        write_exp_var (sym, block_found);
        return 1;
      }
    }
    
    /* nope, give up... */
    return 0;
} /* m3_find_global */ 




      /* Is it a local, nonlocal, static, or  symbol ? 
         (Local to any block or procedure, including those surrounding
          the current one, but excluding  */
        { if ( ( sym = lookup_symbol 
                   ( cur_tok.string, 
                     expression_context_block,
                     VAR_DOMAIN, 0, NULL
                   )
                ) != 0
                && sym->aclass != LOC_STATIC
             ) 
            { write_exp_var ( sym, block_found );
              goto ident_ok;
            }
        }

      /* Could it be an unqualified global name in the current unit ?
	 these are accessible only through the interface record,
	 which happens to be the only symbol in the topmost block. */
      b = expression_context_block;
      while (b) 
        { if ( BLOCK_SUPERBLOCK (b) ) 
            { /* Not the outermost block, look in it. */
#if 0 
              if /* FIXME: This non-outermost search looks quadratically 
                    redundant to the search done by lookup_symbol above,
                    starting at expression_context_block.  Also, I think
                    there is a way (GLOBAL_BLOCK?) to avoid having to loop
                    through the blocks to get the global block.  Also,
                    we really should write a m3_lookup_symbol_nonlocal, 
                    make it a callback for la_lookup_symbol_nonlocal, 
                    and do this there. */ 
                 ( ( sym = lookup_symbol 
                       ( cur_tok.string, b, VAR_DOMAIN, 0, NULL ) 
                    ) != 0
                    && sym->aclass != LOC_STATIC
                 ) 
                { write_exp_var (sym, block_found );
                  goto ident_ok;
                }
#endif /* 0 */ 
              b = BLOCK_SUPERBLOCK (b);
            }
          else  
            { /* Outermost block. It contains the symbol for the current
                 unit as its only symbol.  Take the opportunity to use it
                 to construct a qualified name with cur_token and see if
                 that is what is wanted. */  
              sym = dict_iterator_first ( b->dict, &iter); 
              unit_name = SYMBOL_LINKAGE_NAME (sym);
              /* Try CM3 compiler naming. */ 
              /* FIXME: This doesn't make any sense.  This test is for a CM3
                 name of a compilation unit body _function_.  (And we have
                 cm3_compute_unit_body_name_len to check for it, if we needed
                 to.)  But we are looking fore the module record or
                 interface record here, and those are spelled the same in
                 all compilers, as in the PM3 case below. */ 
              i = strlen ( unit_name ) - 3;
              if ( ( i >= 0 )
                   && ( unit_name [ i ] == '_' )
                   && ( ( unit_name [ i + 1 ] == 'M' ) 
                          || ( unit_name [ i + 1 ] == 'I' ) 
                      )
                   && ( unit_name [ i + 2 ] == '3' )
                 ) 
                { unit_name = alloca ( i + 1 );
                  strncpy ( unit_name, SYMBOL_LINKAGE_NAME ( sym ), i);
                  unit_name[ i ] = 0;
                  if ( m3_find_global ( unit_name, cur_tok.string ) ) 
                    { goto ident_ok; } 
                }
              /* Try SRC, PM3, or EZM3 Modula-3 compiler naming. */ 
              if ( ( unit_name [ 0 ] == 'M' )
                   && ( ( unit_name [ 1 ] == 'I' ) 
                        || ( unit_name [ 1 ] == 'M' ) 
                      )
                   && ( unit_name [ 2 ] == '_' )
                   && m3_find_global ( unit_name + 3, cur_tok.string )
                 ) 
                { goto ident_ok; } 
              break; 
            } 
        } 
      /* Is this the beginning of a qualified global name?  In m3gdb expressions,
         there is no valid use of an interface or module name alone, so if it
         names one, require a dot and a global name within.  In this case, 
         don't put the dot construct in the expression, just a direct reference
         to the declared entity will suffice. */
      /* FIXME:  This too should go inside a m3_lookup_symbol_nonlocal callback. */ 
      unit_name = cur_tok.string;
      if ( m3_unit_name_globals_symbol ('I', unit_name, NULL ) 
           || m3_unit_name_globals_symbol ('M', unit_name, NULL )
         ) {
	get_token ();
	if (cur_tok.kind != TK_DOT) {
	  error ("Unit name %s requires a dot and identifier", unit_name);
	  return 1;
        }
	get_token ();
	if (cur_tok.kind != TK_IDENT) {
	  error ("Unit name %s requires a selector after the dot", unit_name);
	  return 1;
        }
	if (m3_find_global (unit_name, cur_tok.string)) {
	  goto ident_ok;
	}
      } else {
	unit_name = 0;
      }
