home *** CD-ROM | disk | FTP | other *** search
/ ftptest.leeds.ac.uk / 2015.02.ftptest.leeds.ac.uk.tar / ftptest.leeds.ac.uk / bionet / CAE-GROUP / SCL-WIN3x / FED_PLUS.EXE / VARIABLE.C < prev    next >
C/C++ Source or Header  |  1994-07-23  |  14KB  |  557 lines

  1. static char rcsid[] = "$Id: variable.c,v 1.6 1994/05/11 19:51:24 libes Exp $";
  2.  
  3. /************************************************************************
  4. ** Module:    Variable
  5. ** Description:    This module implements the Variable abstraction.  A
  6. **    Variable consists of a name, a type, a reference class, and
  7. **    some flags, e.g. 'optional', 'variable'.  It is used to represent
  8. **    variable attributes, variables, and formal parameters.
  9. ** Constants:
  10. **    VARIABLE_NULL    - the null variable
  11. **
  12. ************************************************************************/
  13.  
  14. /*
  15.  * This code was developed with the support of the United States Government,
  16.  * and is not subject to copyright.
  17.  *
  18.  * $Log: variable.c,v $
  19.  * Revision 1.6  1994/05/11  19:51:24  libes
  20.  * numerous fixes
  21.  *
  22.  * Revision 1.5  1993/10/15  18:48:48  libes
  23.  * CADDETC certified
  24.  *
  25.  * Revision 1.4  1992/08/18  17:13:43  libes
  26.  * rm'd extraneous error messages
  27.  *
  28.  * Revision 1.3  1992/06/08  18:06:57  libes
  29.  * prettied up interface to print_objects_when_running
  30.  *
  31.  * Revision 1.2  1992/05/31  08:35:51  libes
  32.  * multiple files
  33.  *
  34.  * Revision 1.1  1992/05/28  03:55:04  libes
  35.  * Initial revision
  36.  *
  37.  * Revision 1.5  1992/05/10  06:03:51  libes
  38.  * cleaned up OBJget_symbol
  39.  *
  40.  * Revision 1.4  1992/02/17  14:34:01  libes
  41.  * lazy ref/use evaluation now working
  42.  *
  43.  * Revision 1.3  1992/02/12  07:06:10  libes
  44.  * do sub/supertype
  45.  *
  46.  * Revision 1.2  1992/02/09  00:50:45  libes
  47.  * does ref/use correctly
  48.  *
  49.  * Revision 1.1  1992/02/05  08:34:24  libes
  50.  * Initial revision
  51.  *
  52.  * Revision 1.0.1.1  1992/01/22  02:47:57  libes
  53.  * copied from ~pdes
  54.  *
  55.  * Revision 4.6  1991/09/16  23:13:12  libes
  56.  * added print functionsalgorithm.c
  57.  *
  58.  * Revision 4.5  1991/07/21  05:22:17  libes
  59.  * added VARget and put_inverse, initialized private ->inverse
  60.  *
  61.  * Revision 4.4  1991/06/14  20:42:02  libes
  62.  * Remove reference_class from variable, and added support for true name
  63.  * of variable in another schema to which variable corresponds.
  64.  *
  65.  * Revision 4.3  1991/01/24  22:20:36  silver
  66.  * merged changes from NIST and SCRA
  67.  * SCRA changes are due to DEC ANSI C compiler tests.
  68.  *
  69.  * Revision 4.2  91/01/08  18:56:07  pdesadmn
  70.  * Initial - Beta checkin at SCRA
  71.  * 
  72.  * Revision 4.1  90/09/06  11:40:50  clark
  73.  * Initial checkin at SCRA
  74.  * 
  75.  * Revision 4.1  90/09/06  11:40:50  clark
  76.  * initial checkin at SCRA
  77.  * 
  78.  * Revision 4.1  90/09/06  11:40:50  clark
  79.  * BPR 2.1 alpha
  80.  * 
  81.  */
  82.  
  83. #define VARIABLE_C
  84. #include "variable.h"
  85. #include "object.h"
  86. #include <stdlib.h>
  87. /*
  88. ** Procedure:    VAR_create/free/copy/equal
  89. ** Description:    These are the low-level defining functions for Class_Variable
  90. */
  91.  
  92. #if 0
  93. void
  94. VAR_create(Generic dummy)
  95. {
  96.     struct Variable*    var = (struct Variable*)dummy;
  97.  
  98.     var->type = TYPE_NULL;
  99.     var->initializer = EXPRESSION_NULL;
  100.     var->reference = EXPRESSION_NULL;
  101. /*OLD    var->ref_class = REF_DYNAMIC;*/
  102.     var->flags = 0;
  103.     var->inverse = EXPRESSION_NULL;
  104. }
  105.  
  106. void
  107. VAR_free(Generic dummy)
  108. {
  109.     struct Variable*    var = (struct Variable*)dummy;
  110.     Error        errc;
  111.  
  112.     OBJfree(var->type, &errc);
  113.     OBJfree(var->initializer, &errc);
  114. }
  115.  
  116. void
  117. VAR_copy(Generic dummy1, Generic dummy2)
  118. {
  119.     struct Variable*    dest = (struct Variable*)dummy1;
  120.     struct Variable*    source = (struct Variable*)dummy2;
  121.  
  122.     dest->type = OBJreference(source->type);
  123.     dest->initializer = OBJreference(source->initializer);
  124.     dest->reference = source->reference;
  125. /*    dest->ref_class = source->ref_class;*/
  126.     dest->offset = source->offset;
  127.     dest->flags = source->flags;
  128. }
  129.  
  130. Boolean
  131. VAR_equal(Generic dummy1, Generic dummy2)
  132. {
  133.     struct Variable*    var1 = (struct Variable*)dummy1;
  134.     struct Variable*    var2 = (struct Variable*)dummy2;
  135.     Error        errc;
  136.  
  137.     return (OBJequal(var1->type, var2->type, &errc) /* &&
  138. Deep compare or not even necessary? - DEL
  139.         (var1->ref_class == var2->ref_class)*/);
  140. }
  141.  
  142. void
  143. VAR_print(Generic dummy)
  144. {
  145.     struct Variable*    var = (struct Variable*)dummy;
  146.  
  147.     if (print_none(var_print)) return;
  148.  
  149.     if (print_some(var_print,type)) {
  150.         iprint("type:\n");
  151.         OBJprint(var->type);
  152.     }
  153.     if (print_some(var_print,initializer)) {
  154.         iprint("initializer:\n");
  155.         OBJprint(var->initializer);
  156.     }
  157.     if (print_some(var_print,reference)) {
  158.         iprint("reference:\n");
  159.         OBJprint(var->reference);
  160.     }
  161.     if (print_some(var_print,offset)) {
  162.         iprint("offset: %d\n",var->offset);
  163.     }
  164.     if (print_some(var_print,flags)) {
  165.         iprint("optional: %s  pass-by-reference %s\n",
  166.             BOOLprint((var->flags & VAR_OPT_MASK)!=0),
  167.             BOOLprint((var->flags & VAR_VAR_MASK)!=0));
  168.     }
  169.     if (print_some(var_print,inverse)) {
  170.         iprint("inverse:\n");
  171.         OBJprint(var->inverse);
  172.     }
  173. }
  174.  
  175. #endif /*0*/
  176.  
  177. Symbol *
  178. VAR_get_symbol(Generic v)
  179. {
  180.     return(&((Variable )v)->name->symbol);
  181. }
  182.  
  183. /*
  184. ** Procedure:    VARinitialize
  185. ** Parameters:    -- none --
  186. ** Returns:    void
  187. ** Description:    Initialize the Variable module.
  188. */
  189.  
  190. void
  191. VARinitialize()
  192. {
  193.     MEMinitialize(&VAR_fl,sizeof(struct Variable),100,50);
  194. /*    OBJcreate(OBJ_VARIABLE,VAR_get_symbol,"variable",OBJ_UNUSED_BITS);*/
  195.     OBJcreate(OBJ_VARIABLE,VAR_get_symbol,"variable",OBJ_VARIABLE_BITS);
  196. }
  197.  
  198. /* returns simple name of variable */
  199. /* for example, if var is named SELF\xxx.yyy, return yyy */
  200. extern char *
  201. VARget_simple_name(Variable v)
  202. {
  203.     char tmp;
  204.  
  205.     Expression e = VARget_name(v);
  206.  
  207.     while (TYPEis_expression(EXPget_type(e))) {
  208.         switch (e->e.op_code) {
  209.         case OP_DOT:
  210.         case OP_GROUP:
  211.             e = e->e.op2;
  212.             break;
  213.         default:
  214.             fprintf(stderr,"unexpected op_code (%d) encountered in variable name expression\n",e->e.op_code);
  215.             exit(0);
  216.         }
  217.     }
  218.     return EXPget_name(e);
  219. }
  220.  
  221. /*
  222. ** Procedure:    VARcreate
  223. ** Parameters:    String name    - name of variable to create
  224. **        Type   type    - type of new variable
  225. **        Error* errc    - buffer for error code
  226. ** Returns:    Variable    - the Variable created
  227. ** Description:    Create and return a new variable.
  228. **
  229. ** Notes:    The reference class of the variable is, by default,
  230. **        dynamic.  Special flags associated with the variable
  231. **        (e.g., optional) are initially false.
  232. */
  233.  
  234. Variable 
  235. VARcreate(Expression name, Type type)
  236. {
  237.     Variable v = VAR_new();
  238.     v->name = name;
  239.     v->type = type;
  240.     return v;
  241. }
  242.  
  243. #if 0
  244.  
  245. /*
  246. ** Procedure:    VARput_type
  247. ** Parameters:    Variable var    - variable to examine
  248. **        Type     type    - type for variable
  249. ** Returns:    void
  250. ** Description:    Set the type of a variable.
  251. */
  252.  
  253. void
  254. VARput_type(Variable var, Type type)
  255. {
  256.     struct Variable*    data;
  257.     Error        errc;
  258.  
  259.     data = (struct Variable*)OBJget_data(var, Class_Variable, &errc);
  260.     OBJfree(data->type, &errc);
  261.     data->type = OBJreference(type);
  262. }
  263.  
  264. /*
  265. ** Procedure:    VARput_initializer
  266. ** Parameters:    Variable   var    - variable to modify
  267. **        Expression init    - initializer
  268. ** Returns:    void
  269. ** Description:    Set the initializing expression for a variable.
  270. **
  271. ** Notes:    When a variable used to represent an attribute has an
  272. **    initializer, the represented attribute is a derived attribute.
  273. */
  274.  
  275. void
  276. VARput_initializer(Variable var, Expression init)
  277. {
  278.     struct Variable*    data;
  279.     Error        errc;
  280.  
  281.     data = (struct Variable*)OBJget_data(var, Class_Variable, &errc);
  282.     OBJfree(data->initializer, &errc);
  283.     data->initializer = OBJreference(init);
  284. }
  285.  
  286. /*
  287. ** Procedure:    VARput_derived
  288. ** Parameters:    Variable var    - variable to modify
  289. **        Boolean  val    - new value for derived flag
  290. ** Returns:    void
  291. ** Description:    Set the value of the 'derived' flag for a variable.
  292. **
  293. ** Notes:    This is currently redundant information, as a derived
  294. **    attribute can be identified by the fact that it has an
  295. **    initializing expression.  This may not always be true, however.
  296. */
  297.  
  298. void
  299. VARput_derived(Variable var, Boolean val)
  300. {
  301.     return;
  302. }
  303.  
  304. /*
  305. ** Procedure:    VARput_optional
  306. ** Parameters:    Variable var    - variable to modify
  307. **        Boolean  val    - new value for optional flag
  308. ** Returns:    void
  309. ** Description:    Set the value of the 'optional' flag for a variable.
  310. */
  311.  
  312. void
  313. VARput_optional(Variable var, Boolean val)
  314. {
  315.     struct Variable*    data;
  316.     Error        errc;
  317.  
  318.     data = (struct Variable*)OBJget_data(var, Class_Variable, &errc);
  319.     if (val)
  320.     data->flags |= VAR_OPT_MASK;
  321.     else
  322.     data->flags &= ~VAR_OPT_MASK;
  323. }
  324.  
  325. /*
  326. ** Procedure:    VARput_variable
  327. ** Parameters:    Variable var    - variable to modify
  328. **        Boolean  val    - new value for variable flag
  329. ** Returns:    void
  330. ** Description:    Set the value of the 'variable' flag for a variable.
  331. **
  332. ** Notes:    This flag is intended for use in the cases when a variable
  333. **    is used to represent a formal parameter, which may be passed by
  334. **    reference (a variable parameter, in Pascal terminology).
  335. */
  336.  
  337. void
  338. VARput_variable(Variable var, Boolean val)
  339. {
  340.     struct Variable*    data;
  341.     Error        errc;
  342.  
  343.     data = (struct Variable*)OBJget_data(var, Class_Variable, &errc);
  344.     if (val)
  345.     data->flags |= VAR_VAR_MASK;
  346.     else
  347.     data->flags &= ~VAR_VAR_MASK;
  348. }
  349.  
  350. /*
  351. ** Procedure:    VARput_reference
  352. ** Parameters:    Variable        var    - variable to modify
  353. **        Expression ref    - the variable's reference
  354. ** Returns:    void
  355. ** Description:    Set the reference class of a variable.
  356. */
  357.  
  358. void
  359. VARput_reference(Variable var, Expression ref)
  360. {
  361.     struct Variable*    data;
  362.     Error        errc;
  363.  
  364.     data = (struct Variable*)OBJget_data(var, Class_Variable, &errc);
  365.     data->reference = ref;
  366. }
  367.  
  368. /*
  369. ** Procedure:    VARput_inverse
  370. ** Parameters:    Variable        var    - variable to modify
  371. **        Expression ref    - attr of entity related to be inverse attr
  372. ** Returns:    void
  373. ** Description:    Marks this as an inverse attribute and also says to which
  374.         attribute in the remove entity this relates to.
  375. */
  376.  
  377. void
  378. VARput_inverse(Variable var, Symbol attr)
  379. {
  380.     struct Variable*    data;
  381.     Error        errc;
  382.  
  383.     data = (struct Variable*)OBJget_data(var, Class_Variable, &errc);
  384.     data->inverse = attr;
  385. }
  386.  
  387. /*
  388. ** Procedure:    VARget_inverse
  389. ** Parameters:    Variable        var    - variable to modify
  390. ** Returns:    Symbol        attr    - returns what was put
  391. ** Description:    See VARput_inverse
  392. */
  393.  
  394. Symbol
  395. VARget_inverse(Variable var)
  396. {
  397.     struct Variable*    data;
  398.     Error        errc;
  399.  
  400.     data = (struct Variable*)OBJget_data(var, Class_Variable, &errc);
  401.     return data->inverse;
  402. }
  403.  
  404. /*
  405. ** Procedure:    VARput_reference_class
  406. ** Parameters:    Variable        var    - variable to modify
  407. **        Reference_Class ref    - the variable's reference class
  408. ** Returns:    void
  409. ** Description:    Set the reference class of a variable.
  410. */
  411.  
  412. void
  413. VARput_reference_class(Variable var, Reference_Class ref)
  414. {
  415.     struct Variable*    data;
  416.     Error        errc;
  417.  
  418.     data = (struct Variable*)OBJget_data(var, Class_Variable, &errc);
  419.     data->ref_class = ref;
  420. }
  421.  
  422. /*
  423. ** Procedure:    VARput_offset
  424. ** Parameters:    Variable var    - variable to modify
  425. **        int      offset    - offset to variable in local frame
  426. ** Returns:    void
  427. ** Description:    Set a variable's offset in its local frame.
  428. */
  429.  
  430. void
  431. VARput_offset(Variable var, int offset)
  432. {
  433.     struct Variable*    data;
  434.     Error        errc;
  435.  
  436.     data = (struct Variable*)OBJget_data(var, Class_Variable, &errc);
  437.     data->offset = offset;
  438. }
  439.  
  440. /*
  441. ** Procedure:    VARget_type
  442. ** Parameters:    Variable var    - variable to examine
  443. ** Returns:    Type        - the type of the variable
  444. ** Description:    Retrieve the type of a variable.
  445. */
  446.  
  447. Type
  448. VARget_type(Variable var)
  449. {
  450.     struct Variable*    data;
  451.     Error        errc;
  452.  
  453.     data = (struct Variable*)OBJget_data(var, Class_Variable, &errc);
  454.     return OBJreference(data->type);
  455. }
  456.  
  457. /*
  458. ** Procedure:    VARget_derived
  459. ** Parameters:    Variable var    - variable to examine
  460. ** Returns:    Boolean        - value of variable's derived flag
  461. ** Description:    Retrieve the value of a variable's 'derived' flag.
  462. */
  463.  
  464. /* this function is inlined in variable.h */
  465.  
  466. /*
  467. ** Procedure:    VARget_optional
  468. ** Parameters:    Variable var    - variable to examine
  469. ** Returns:    Boolean        - value of variable's optional flag
  470. ** Description:    Retrieve the value of a variable's 'optional' flag.
  471. */
  472.  
  473. Boolean
  474. VARget_optional(Variable var)
  475. {
  476.     struct Variable*    data;
  477.     Error        errc;
  478.  
  479.     data = (struct Variable*)OBJget_data(var, Class_Variable, &errc);
  480.     return (data->flags & VAR_OPT_MASK) != 0;
  481. }
  482.  
  483. /*
  484. ** Procedure:    VARget_variable
  485. ** Parameters:    Variable var    - variable to examine
  486. ** Returns:    Boolean        - value of variable's variable flag
  487. ** Description:    Retrieve the value of a variable's 'variable' flag.
  488. */
  489.  
  490. Boolean
  491. VARget_variable(Variable var)
  492. {
  493.     struct Variable*    data;
  494.     Error        errc;
  495.  
  496.     data = (struct Variable*)OBJget_data(var, Class_Variable, &errc);
  497.     return (data->flags & VAR_VAR_MASK) != 0;
  498. }
  499.  
  500. /*
  501. ** Procedure:    VARget_reference
  502. ** Parameters:    Variable var    - variable to examine
  503. ** Returns:    Expression    - the variable's reference
  504. ** Description:    Retrieve a variable's reference
  505. */
  506.  
  507. Expression
  508. VARget_reference_class(Variable var)
  509. {
  510.     struct Variable*    data;
  511.     Error        errc;
  512.  
  513.     data = (struct Variable*)OBJget_data(var, Class_Variable, &errc);
  514.     return data->reference;
  515. }
  516.  
  517. /*
  518. ** Procedure:    VARget_reference_class
  519. ** Parameters:    Variable var    - variable to examine
  520. ** Returns:    Reference_Class    - the variable's reference class
  521. ** Description:    Retrieve a variable's reference class.
  522. */
  523.  
  524. Reference_Class
  525. VARget_reference_class(Variable var)
  526. {
  527.     struct Variable*    data;
  528.     Error        errc;
  529.  
  530.     data = (struct Variable*)OBJget_data(var, Class_Variable, &errc);
  531.     return data->ref_class;
  532. }
  533.  
  534. /*
  535. ** Procedure:    VARget_offset
  536. ** Parameters:    Variable var    - variable to examine
  537. ** Returns:    int        - offset to variable in local frame
  538. ** Description:    Retrieve the offset to a variable in it's local frame.
  539. */
  540.  
  541. /* now a macro in variable.h */
  542.  
  543. void
  544. VARprint(Variable var)
  545. {
  546.     Error errc;
  547.     struct Var *data;
  548.  
  549.     print_force(var_print);
  550.  
  551.     data = OBJget_data(var,Class_Variable,&errc);
  552.     VAR_print(data);
  553.  
  554.     print_unforce(var_print);
  555. }
  556. #endif
  557.