OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [host/] [libcdl/] [interface.cxx] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//{{{  Banner                           
2
 
3
//============================================================================
4
//
5
//     interface.cxx
6
//
7
//     Implementation of the CdlInterface class
8
//
9
//============================================================================
10
// ####ECOSHOSTGPLCOPYRIGHTBEGIN####                                        
11
// -------------------------------------------                              
12
// This file is part of the eCos host tools.                                
13
// Copyright (C) 1999, 2000, 2002 Free Software Foundation, Inc.            
14
//
15
// This program is free software; you can redistribute it and/or modify     
16
// it under the terms of the GNU General Public License as published by     
17
// the Free Software Foundation; either version 2 or (at your option) any   
18
// later version.                                                           
19
//
20
// This program is distributed in the hope that it will be useful, but      
21
// WITHOUT ANY WARRANTY; without even the implied warranty of               
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        
23
// General Public License for more details.                                 
24
//
25
// You should have received a copy of the GNU General Public License        
26
// along with this program; if not, write to the                            
27
// Free Software Foundation, Inc., 51 Franklin Street,                      
28
// Fifth Floor, Boston, MA  02110-1301, USA.                                
29
// -------------------------------------------                              
30
// ####ECOSHOSTGPLCOPYRIGHTEND####                                          
31
//============================================================================
32
//#####DESCRIPTIONBEGIN####
33
//
34
// Author(s):   bartv
35
// Contact(s):  bartv
36
// Date:        1999/03/01
37
// Version:     0.02
38
//
39
//####DESCRIPTIONEND####
40
//============================================================================
41
 
42
//}}}
43
//{{{  #include's                       
44
 
45
// ----------------------------------------------------------------------------
46
#include "cdlconfig.h"
47
 
48
// Get the infrastructure types, assertions, tracing and similar
49
// facilities.
50
#include <cyg/infra/cyg_ass.h>
51
#include <cyg/infra/cyg_trac.h>
52
 
53
// <cdlcore.hxx> defines everything implemented in this module.
54
// It implicitly supplies <string>, <vector> and <map> because
55
// the class definitions rely on these headers.
56
#include <cdlcore.hxx>
57
 
58
//}}}
59
 
60
//{{{  Statics                          
61
 
62
// ----------------------------------------------------------------------------
63
CYGDBG_DEFINE_MEMLEAK_COUNTER(CdlInterfaceBody);
64
 
65
//}}}
66
//{{{  Constructor                      
67
 
68
// ----------------------------------------------------------------------------
69
CdlInterfaceBody::CdlInterfaceBody(std::string name_arg, bool generated_arg)
70
    : CdlNodeBody(name_arg),
71
      CdlUserVisibleBody(),
72
      CdlValuableBody(CdlValueFlavor_Data),
73
      CdlParentableBody(),
74
      CdlBuildableBody(),
75
      CdlDefinableBody()
76
{
77
    CYG_REPORT_FUNCNAME("CdlInterfaceBody:: constructor");
78
    CYG_REPORT_FUNCARG1XV(this);
79
 
80
    generated = generated_arg;
81
    cdlinterfacebody_cookie = CdlInterfaceBody_Magic;
82
    CYGDBG_MEMLEAK_CONSTRUCTOR();
83
 
84
    CYG_POSTCONDITION_THISC();
85
    CYG_REPORT_RETURN();
86
}
87
 
88
//}}}
89
//{{{  Destructor                       
90
 
91
// ----------------------------------------------------------------------------
92
CdlInterfaceBody::~CdlInterfaceBody()
93
{
94
    CYG_REPORT_FUNCNAME("CdlInterfaceBody:: destructor");
95
    CYG_REPORT_FUNCARG1XV(this);
96
    CYG_PRECONDITION_THISC();
97
 
98
    cdlinterfacebody_cookie = CdlInterfaceBody_Invalid;
99
    CYGDBG_MEMLEAK_DESTRUCTOR();
100
 
101
    CYG_REPORT_RETURN();
102
}
103
 
104
//}}}
105
//{{{  parse_interface()                
106
 
107
// ----------------------------------------------------------------------------
108
// Parsing an interface definition. This is basically the same as parsing
109
// an option, component, or package.
110
 
111
int
112
CdlInterfaceBody::parse_interface(CdlInterpreter interp, int argc, const char* argv[])
113
{
114
    CYG_REPORT_FUNCNAMETYPE("CdlInterface::parse_interface", "result %d");
115
    CYG_REPORT_FUNCARG1("argc %d", argc);
116
    CYG_PRECONDITION_CLASSC(interp);
117
 
118
    std::string  diag_argv0      = CdlParse::get_tcl_cmd_name(argv[0]);
119
 
120
    CdlLoadable  loadable       = interp->get_loadable();
121
    CdlContainer parent         = interp->get_container();
122
    CdlToplevel  toplevel       = interp->get_toplevel();
123
    CYG_ASSERT_CLASSC(loadable);        // There should always be a loadable during parsing
124
    CYG_ASSERT_CLASSC(parent);
125
    CYG_ASSERT_CLASSC(toplevel);
126
 
127
    // The new interface should be created and added to the loadable.
128
    // early on. If there is a parsing error it will get cleaned up
129
    // automatically as a consequence of the loadable destructor.
130
    // However it is necessary to validate the name first. Errors
131
    // should be reported via CdlParse::report_error(), which
132
    // may result in an exception.
133
    CdlInterface new_interface  = 0;
134
    bool         ok             = true;
135
    int          result         = TCL_OK;
136
    try {
137
 
138
        // Currently there are no command-line options. This may change in future.
139
        if (3 != argc) {
140
            CdlParse::report_error(interp, "", std::string("Incorrect number of arguments to `") + diag_argv0 +
141
                                   "'\nExpecting name and properties list.");
142
            ok = false;
143
        } else if (!Tcl_CommandComplete(CDL_TCL_CONST_CAST(char*, argv[2]))) {
144
            CdlParse::report_error(interp, "", std::string("Invalid property list for cdl_interface `") + argv[1] + "'.");
145
            ok = false;
146
        } else if (0 != toplevel->lookup(argv[1])) {
147
            // FIXME: interfaces can be generated implicitly because of an
148
            // unresolved implements property. This code should look for
149
            // an existing auto-generated interface object and replace it
150
            // if necessary.
151
            CdlParse::report_error(interp, "", std::string("Interface `") + argv[1] +
152
                                   "' cannot be loaded.\nThe name is already in use.");
153
            ok = false;
154
        } else {
155
            new_interface = new CdlInterfaceBody(argv[1], false);
156
            toplevel->add_node(loadable, parent, new_interface);
157
        }
158
 
159
        if (!ok) {
160
            // Just because this component cannot be created, that is no
161
            // reason to abort the whole parsing process.
162
            CYG_REPORT_RETVAL(TCL_OK);
163
            return TCL_OK;
164
        }
165
    } catch(std::bad_alloc e) {
166
        interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Out of memory"));
167
        result = TCL_ERROR;
168
    } catch(CdlParseException e) {
169
        interp->set_result(e.get_message());
170
        result = TCL_ERROR;
171
    } catch(...) {
172
        interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Unexpected C++ exception"));
173
        result = TCL_ERROR;
174
    }
175
    if (TCL_OK != result) {
176
        CYG_REPORT_RETVAL(result);
177
        return result;
178
    }
179
 
180
    // At this stage new_interface has been created and added to the
181
    // hierarchy. The main work now is to add the properties.
182
 
183
    // Push the option as the current node early on. This aids
184
    // diagnostics.
185
    CdlNode old_node = interp->push_node(new_interface);
186
 
187
    // Declare these outside the scope of the try statement, to allow
188
    // goto calls for the error handling.
189
    std::string tcl_result;
190
    std::vector<CdlInterpreterCommandEntry>  new_commands;
191
    std::vector<CdlInterpreterCommandEntry>* old_commands = 0;
192
    static CdlInterpreterCommandEntry   commands[] =
193
    {
194
        CdlInterpreterCommandEntry("", 0)
195
    };
196
    int i;
197
 
198
    // All parsing errors may result in an exception, under the control of
199
    // application code. This exception must not pass through the Tcl interpreter.
200
    try {
201
 
202
        for (i = 0; 0 != commands[i].command; i++) {
203
            new_commands.push_back(commands[i]);
204
        }
205
        CdlDefinableBody::add_property_parsers(new_commands);
206
        CdlBuildableBody::add_property_parsers(new_commands);
207
        CdlParentableBody::add_property_parsers(new_commands);
208
        CdlValuableBody::add_property_parsers(new_commands);
209
        CdlUserVisibleBody::add_property_parsers(new_commands);
210
        CdlNodeBody::add_property_parsers(new_commands);
211
 
212
        // Now evaluate the body. If an error occurs then typically
213
        // this will be reported via CdlParse::report_error(),
214
        // but any exceptions will have been intercepted and
215
        // turned into a Tcl error.
216
        old_commands = interp->push_commands(new_commands);
217
        result = interp->eval(argv[2], tcl_result);
218
        interp->pop_commands(old_commands);
219
 
220
        if (TCL_OK == result) {
221
            // Even if there were errors, they were not fatal. There may
222
            // now be a number of properties for this option, and some
223
            // validation should take place. Start with the base classes.
224
            new_interface->CdlNodeBody::check_properties(interp);
225
            new_interface->CdlUserVisibleBody::check_properties(interp);
226
            new_interface->CdlValuableBody::check_properties(interp);
227
            new_interface->CdlParentableBody::check_properties(interp);
228
            new_interface->CdlBuildableBody::check_properties(interp);
229
            new_interface->CdlDefinableBody::check_properties(interp);
230
 
231
            // The flavor "none" makes no sense for interfaces.
232
            // The flavor "bool" makes very little sense, but may be useful
233
            // in weird cases. Both booldata and data make sense.
234
            // The default flavor is "data", because interfaces are
235
            // essentially just counters.
236
            if (new_interface->has_property(CdlPropertyId_Flavor)) {
237
                if (CdlValueFlavor_None == new_interface->get_flavor()) {
238
                    CdlParse::report_error(interp, "", "An interface should not have the `none' flavor.");
239
                }
240
            }
241
 
242
            // Interfaces cannot be modified directly by the user, so
243
            // there is no point in entry_proc, check_proc, dialog or
244
            // wizard
245
            if (new_interface->has_property(CdlPropertyId_EntryProc)) {
246
                CdlParse::report_error(interp, "", "An interface should not have an `entry_proc' property.");
247
            }
248
            if (new_interface->has_property(CdlPropertyId_CheckProc)) {
249
                CdlParse::report_error(interp, "", "An interface should not have a `check_proc' property.");
250
            }
251
            if (new_interface->has_property(CdlPropertyId_Dialog)) {
252
                CdlParse::report_error(interp, "", "An interface should not have a `dialog' property.");
253
            }
254
            if (new_interface->has_property(CdlPropertyId_Wizard)) {
255
                CdlParse::report_error(interp, "", "An interface should not have a `wizard' property.");
256
            }
257
            // Calculated does not make sense, an interface is implicitly calculated
258
            // Nor does default_value.
259
            if (new_interface->has_property(CdlPropertyId_Calculated)) {
260
                CdlParse::report_error(interp, "", "An interface should not have a `calculated' property.");
261
            }
262
            if (new_interface->has_property(CdlPropertyId_DefaultValue)) {
263
                CdlParse::report_error(interp, "", "An interface should not have a `default_value' property.");
264
            }
265
            // active_if might make sense, as a way of controlling
266
            // whether or not a #define will be generated.
267
 
268
            // legal_values, requires and implements are all sensible
269
            // properties for an interface.
270
 
271
            // group may or may not make sense, allow it for now.
272
 
273
            // For the uservisible base class, allow all of display,
274
            // doc and description. At worst these are harmless
275
 
276
            // For the parentable base class, allow parent. Again this
277
            // is harmless.
278
 
279
            // Also allow all of the definable and buildable
280
            // properties.
281
        }
282
    } catch (std::bad_alloc e) {
283
        // Errors at this stage should be reported via Tcl, not via C++.
284
        // However there is no point in continuing with the parsing operation,
285
        // just give up.
286
        interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Out of memory"));
287
        result = TCL_ERROR;
288
    } catch (CdlParseException e) {
289
        interp->set_result(e.get_message());
290
        result = TCL_ERROR;
291
    } catch(...) {
292
        interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Unexpected C++ exception"));
293
        result = TCL_ERROR;
294
    }
295
 
296
    // Restore the interpreter to its prior state.
297
    interp->pop_node(old_node);
298
    if (0 != old_commands) {
299
        interp->pop_commands(old_commands);
300
    }
301
 
302
    CYG_REPORT_RETVAL(result);
303
    return result;
304
}
305
 
306
//}}}
307
//{{{  Persistence support              
308
 
309
// ----------------------------------------------------------------------------
310
 
311
void
312
CdlInterfaceBody::initialize_savefile_support(CdlToplevel toplevel)
313
{
314
    CYG_REPORT_FUNCNAME("CdlInterface::initialize_savefile_support");
315
 
316
    toplevel->add_savefile_command("cdl_interface", 0, &savefile_interface_command);
317
    CdlValuableBody::initialize_savefile_support(toplevel, "cdl_interface");
318
 
319
    CYG_REPORT_RETURN();
320
}
321
 
322
void
323
CdlInterfaceBody::save(CdlInterpreter interp, Tcl_Channel chan, int indentation, bool minimal)
324
{
325
    CYG_REPORT_FUNCNAME("CdlInterface::save");
326
    CYG_REPORT_FUNCARG5XV(this, interp, chan, indentation, minimal);
327
    CYG_PRECONDITION_THISC();
328
    CYG_PRECONDITION_CLASSC(interp);
329
 
330
    // Interfaces contain only calculated data, so for a minimal save
331
    // there is no point in storing any of the data.
332
    if (!minimal || this->has_additional_savefile_information()) {
333
        // Start with the UserVisible data, which will result in a suitable set
334
        // of comments before the package definition itself.
335
        this->CdlUserVisibleBody::save(interp, chan, indentation, minimal);
336
 
337
        // Now output the line "cdl_interface <name> {"
338
        // The name is guaranteed to be a valid C preprocessor symbol, so it
339
        // is not going to need any quoting.
340
        std::string data = std::string(indentation, ' ') + "cdl_interface " + get_name() + " {\n";
341
 
342
        // Start with details of everything that implements this interface.
343
        if (!minimal) {
344
            const std::vector<CdlReferrer>& referrers = this->get_referrers();
345
            std::vector<CdlReferrer>::const_iterator ref_i;
346
            int real_referrers = 0;
347
            for (ref_i = referrers.begin(); ref_i != referrers.end(); ref_i++) {
348
                CdlNode     node = ref_i->get_source();
349
                CdlProperty prop = ref_i->get_source_property();
350
 
351
                CdlValuable valuable = dynamic_cast<CdlValuable>(node);
352
                if ((0 != valuable) && (CdlPropertyId_Implements == prop->get_property_name())) {
353
                    real_referrers++;
354
                    data += std::string(indentation, ' ') + "    # Implemented by " + valuable->get_name() + ", " +
355
                        (valuable->is_active()  ? "active"  : "inactive") + ", " +
356
                        (valuable->is_enabled() ? "enabled" : "disabled") + '\n';
357
                }
358
            }
359
            if (0 == real_referrers) {
360
                data += std::string(indentation, ' ') + "    # No options implement this inferface\n";
361
            }
362
        }
363
        interp->write_data(chan, data);
364
 
365
        // Deal with the value
366
        this->CdlValuableBody::save_valuable(interp, chan, indentation + 4, false, minimal);
367
 
368
        // Close the cdl_interface body. A blank line is added here.
369
        data = "};\n\n";
370
        interp->write_data(chan, data);
371
    }
372
 
373
    CYG_REPORT_RETURN();
374
}
375
 
376
int
377
CdlInterfaceBody::savefile_interface_command(CdlInterpreter interp, int argc, const char* argv[])
378
{
379
    CYG_REPORT_FUNCNAMETYPE("CdlInterface::savefile_interface_command", "result %d");
380
    CYG_PRECONDITION_CLASSC(interp);
381
 
382
    int result = TCL_OK;
383
    CdlToplevel toplevel = interp->get_toplevel();
384
    CYG_ASSERT_CLASSC(toplevel);
385
 
386
    std::vector<CdlInterpreterCommandEntry> subcommands;
387
    std::vector<CdlInterpreterCommandEntry>* toplevel_commands = 0;
388
    CdlNode old_node = 0;
389
 
390
    try {
391
 
392
        if (3 != argc) {
393
            CdlParse::report_error(interp, "", "Invalid cdl_interface command in savefile, expecting two arguments.");
394
        } else {
395
 
396
            CdlNode current_node = toplevel->lookup(argv[1]);
397
            if (0 == current_node) {
398
                // FIXME: save value in limbo
399
                CdlParse::report_error(interp, "",
400
                                       std::string("The savefile contains a cdl_interface command for an unknown interface `")
401
                                       + argv[1] + "'.");
402
            } else {
403
                toplevel->get_savefile_subcommands("cdl_interface", subcommands);
404
                toplevel_commands = interp->push_commands(subcommands);
405
                old_node = interp->push_node(current_node);
406
 
407
                std::string tcl_result;
408
                result = interp->eval(argv[2], tcl_result);
409
 
410
                interp->pop_commands(toplevel_commands);
411
                toplevel_commands = 0;
412
                interp->pop_node(old_node);
413
                old_node = 0;
414
            }
415
        }
416
    } catch(...) {
417
        if (0 != old_node) {
418
            interp->pop_node(old_node);
419
        }
420
        if (0 != toplevel_commands) {
421
            interp->pop_commands(toplevel_commands);
422
        }
423
        throw;
424
    }
425
 
426
    CYG_REPORT_RETVAL(result);
427
    return result;
428
}
429
 
430
//}}}
431
//{{{  check_this()                     
432
 
433
// ----------------------------------------------------------------------------
434
 
435
bool
436
CdlInterfaceBody::check_this(cyg_assert_class_zeal zeal) const
437
{
438
    if (CdlInterfaceBody_Magic != cdlinterfacebody_cookie) {
439
        return false;
440
    }
441
    CYGDBG_MEMLEAK_CHECKTHIS();
442
    return CdlNodeBody::check_this(zeal)        &&
443
           CdlUserVisibleBody::check_this(zeal) &&
444
           CdlValuableBody::check_this(zeal)    &&
445
           CdlParentableBody::check_this(zeal)  &&
446
           CdlBuildableBody::check_this(zeal)   &&
447
           CdlDefinableBody::check_this(zeal);
448
}
449
 
450
//}}}
451
//{{{  recalculate()                    
452
 
453
// ----------------------------------------------------------------------------
454
// There has been a change in the configuration which may affect the value
455
// of an interface. This can happen for a variety of reasons. For simplicity
456
// the entire value is just recalculated, with no attempt at optimisation.
457
// This may have to change in future.
458
 
459
void
460
CdlInterfaceBody::recalculate(CdlTransaction transaction)
461
{
462
    CYG_REPORT_FUNCNAME("CdlInterface::recalculate");
463
 
464
    const CdlValue& old_value = transaction->get_whole_value(this);
465
 
466
    cdl_int count = 0;
467
    std::vector<CdlValuable> implementers;
468
    std::vector<CdlValuable>::const_iterator valuable_i;
469
    this->get_implementers(implementers);
470
    for (valuable_i = implementers.begin(); valuable_i != implementers.end(); valuable_i++) {
471
        if (transaction->is_active(*valuable_i)) {
472
            const CdlValue& implementer_value = transaction->get_whole_value(*valuable_i);
473
            if (implementer_value.is_enabled()) {
474
                count++;
475
            }
476
        }
477
    }
478
 
479
    // What to do with the count depends on the flavor.
480
    switch(this->get_flavor()) {
481
      case CdlValueFlavor_Bool :
482
        {
483
            bool new_bool = (count > 0);
484
            if (new_bool != old_value.is_enabled()) {
485
                CdlValue new_value = old_value;
486
                new_value.set_enabled(new_bool, CdlValueSource_Default);
487
                transaction->set_whole_value(this, old_value, new_value);
488
            }
489
            break;
490
        }
491
      case CdlValueFlavor_BoolData:
492
        {
493
            // The only thing that actually needs checking is the count value.
494
            // Iff that has changed then the boolean part may need changing as well.
495
            if (count != old_value.get_integer_value()) {
496
                CdlValue new_value = old_value;
497
                new_value.set_enabled_and_value(count > 0, count, CdlValueSource_Default);
498
                transaction->set_whole_value(this, old_value, new_value);
499
            }
500
 
501
            break;
502
        }
503
      case CdlValueFlavor_Data:
504
        {
505
            if (count != old_value.get_integer_value()) {
506
                CdlValue new_value = old_value;
507
                new_value.set_integer_value(count, CdlValueSource_Default);
508
                transaction->set_whole_value(this, old_value, new_value);
509
            }
510
            break;
511
        }
512
 
513
      default:
514
        break;
515
    }
516
 
517
    CYG_REPORT_RETURN();
518
}
519
 
520
//}}}
521
//{{{  misc                             
522
 
523
// ----------------------------------------------------------------------------
524
 
525
bool
526
CdlInterfaceBody::is_modifiable() const
527
{
528
    CYG_REPORT_FUNCNAME("CdlInterface::is_modifiable (false)");
529
    CYG_PRECONDITION_THISC();
530
    CYG_REPORT_RETURN();
531
    return false;
532
}
533
 
534
std::string
535
CdlInterfaceBody::get_class_name() const
536
{
537
    CYG_REPORT_FUNCNAME("CdlInterface::get_class_name");
538
    CYG_PRECONDITION_THISC();
539
    CYG_REPORT_RETURN();
540
    return "interface";
541
}
542
 
543
void
544
CdlInterfaceBody::get_implementers(std::vector<CdlValuable>& implementers) const
545
{
546
    CYG_REPORT_FUNCNAME("CdlInterface::get_implementers");
547
    CYG_PRECONDITION_THISC();
548
 
549
    const std::vector<CdlReferrer>& referrers = this->get_referrers();
550
    std::vector<CdlReferrer>::const_iterator ref_i;
551
    for (ref_i = referrers.begin(); ref_i != referrers.end(); ref_i++) {
552
        CdlNode     node = ref_i->get_source();
553
        CdlProperty prop = ref_i->get_source_property();
554
 
555
        CdlValuable valuable = dynamic_cast<CdlValuable>(node);
556
        if ((0 != valuable) && (CdlPropertyId_Implements == prop->get_property_name())) {
557
            implementers.push_back(valuable);
558
        }
559
    }
560
 
561
    CYG_REPORT_RETURN();
562
}
563
 
564
// Interfaces are somewhat peculiar, in that they can be defined implicitly
565
// simply by occurring in an "implements" property, or explicitly inside
566
// a CDL script. Worse, they can switch between these two states when
567
// loadables are added or removed.
568
 
569
bool
570
CdlInterfaceBody::was_generated() const
571
{
572
    CYG_REPORT_FUNCNAMETYPE("CdlInterface::was_generated", "result %d");
573
    CYG_REPORT_FUNCARG1XV(this);
574
    CYG_PRECONDITION_THISC();
575
 
576
    bool result = this->generated;
577
    CYG_REPORT_RETVAL(result);
578
    return result;
579
}
580
 
581
//}}}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.