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

Subversion Repositories openrisc

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

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//{{{  Banner                           
2
 
3
//============================================================================
4
//
5
//     component.cxx
6
//
7
//     Implementation of the CdlComponent 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
// <cdl.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 <cdl.hxx>
57
 
58
//}}}
59
 
60
//{{{  Statics                          
61
 
62
// ----------------------------------------------------------------------------
63
CYGDBG_DEFINE_MEMLEAK_COUNTER(CdlComponentBody);
64
 
65
//}}}
66
//{{{  Constructor                      
67
 
68
// ----------------------------------------------------------------------------
69
CdlComponentBody::CdlComponentBody(std::string name_arg)
70
    : CdlNodeBody(name_arg),
71
      CdlContainerBody(),
72
      CdlUserVisibleBody(),
73
      CdlValuableBody(),
74
      CdlParentableBody(),
75
      CdlBuildableBody(),
76
      CdlDefinableBody()
77
{
78
    CYG_REPORT_FUNCNAME("CdlComponentBody:: constructor");
79
    CYG_REPORT_FUNCARG1XV(this);
80
 
81
    cdlcomponentbody_cookie = CdlComponentBody_Magic;
82
    CYGDBG_MEMLEAK_CONSTRUCTOR();
83
 
84
    CYG_POSTCONDITION_THISC();
85
    CYG_REPORT_RETURN();
86
}
87
 
88
//}}}
89
//{{{  Destructor                       
90
 
91
// ----------------------------------------------------------------------------
92
 
93
CdlComponentBody::~CdlComponentBody()
94
{
95
    CYG_REPORT_FUNCNAME("CdlComponentBody:: destructor");
96
    CYG_REPORT_FUNCARG1XV(this);
97
    CYG_PRECONDITION_THISC();
98
 
99
    cdlcomponentbody_cookie = CdlComponentBody_Invalid;
100
    CYGDBG_MEMLEAK_DESTRUCTOR();
101
 
102
    CYG_REPORT_RETURN();
103
}
104
 
105
//}}}
106
//{{{  parse_component()                
107
 
108
// ----------------------------------------------------------------------------
109
// Parsing a component definition. This routine gets invoked directly from the
110
// Tcl interpreter.
111
 
112
int
113
CdlComponentBody::parse_component(CdlInterpreter interp, int argc, const char* argv[])
114
{
115
    CYG_REPORT_FUNCNAMETYPE("CdlComponentBody::parse_component", "result %d");
116
    CYG_REPORT_FUNCARG1("argc %d", argc);
117
    CYG_PRECONDITION_CLASSC(interp);
118
 
119
    std::string  diag_argv0      = CdlParse::get_tcl_cmd_name(argv[0]);
120
 
121
    CdlLoadable  loadable       = interp->get_loadable();
122
    CdlPackage   package        = dynamic_cast<CdlPackage>(loadable);
123
    CdlContainer parent         = interp->get_container();
124
    CdlToplevel  toplevel       = interp->get_toplevel();
125
    CYG_ASSERT_CLASSC(loadable);        // There should always be a loadable during parsing
126
    CYG_ASSERT_CLASSC(package);         // And packages are the only loadable for software CDL.
127
    CYG_ASSERT_CLASSC(parent);
128
    CYG_ASSERT_CLASSC(toplevel);
129
 
130
    // The new component should be created and added to the package
131
    // early on. If there is a parsing error it will get cleaned up
132
    // automatically as a consequence of the package destructor.
133
    // However it is necessary to validate the name first. Errors
134
    // should be reported via CdlParse::report_error(),
135
    // which may result in an exception.
136
    CdlComponent new_component  = 0;
137
    bool         ok             = true;
138
    int          result         = TCL_OK;
139
    try {
140
 
141
        // Currently there are no options. This may change in future.
142
        if (3 != argc) {
143
            CdlParse::report_error(interp, "",
144
                                   std::string("Incorrect number of arguments to `") + diag_argv0 +
145
                                         "'\nExpecting name and properties list.");
146
            ok = false;
147
            goto done;
148
        }
149
        if (!Tcl_CommandComplete(CDL_TCL_CONST_CAST(char*, argv[2]))) {
150
            CdlParse::report_error(interp, "",
151
                                   std::string("Invalid property list for cdl_component `") + argv[1] + "'.");
152
            ok = false;
153
            goto done;
154
        }
155
 
156
        if (0 != toplevel->lookup(argv[1])) {
157
            CdlParse::report_error(interp, "",
158
                                   std::string("Component `") + argv[1] +
159
                                   "' cannot be loaded.\nThe name is already in use.");
160
            ok = false;
161
        } else {
162
            new_component = new CdlComponentBody(argv[1]);
163
            toplevel->add_node(package, parent, new_component);
164
        }
165
 
166
      done:
167
        if (!ok) {
168
            // Just because this component cannot be created, that is no
169
            // reason to abort the whole parsing process.
170
            CYG_REPORT_RETVAL(TCL_OK);
171
            return TCL_OK;
172
        }
173
    } catch(std::bad_alloc e) {
174
        interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Out of memory"));
175
        result = TCL_ERROR;
176
    } catch(CdlParseException e) {
177
        interp->set_result(e.get_message());
178
        result = TCL_ERROR;
179
    } catch(...) {
180
        interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Unexpected C++ exception"));
181
        result = TCL_ERROR;
182
    }
183
    if (TCL_OK != result) {
184
        CYG_REPORT_RETVAL(result);
185
        return result;
186
    }
187
 
188
    // At this stage new_component has been created and added to the hierarchy.
189
    // The main work now is to add the properties.
190
 
191
    // Push the component as the current node early on. This aids
192
    // diagnostics. Also make it the new container.
193
    CdlNode      old_node       = interp->push_node(new_component);
194
    CdlContainer old_container  = interp->push_container(new_component);
195
    std::string  old_context;
196
    CYG_ASSERTC(parent == old_container);
197
 
198
    // Declare these outside the scope of the try statement, to allow
199
    // goto calls for the error handling.
200
    std::string tcl_result;
201
    std::vector<CdlInterpreterCommandEntry>  new_commands;
202
    std::vector<CdlInterpreterCommandEntry>* old_commands = 0;
203
    static CdlInterpreterCommandEntry commands[] =
204
    {
205
        CdlInterpreterCommandEntry("script",             &CdlComponentBody::parse_script    ),
206
        CdlInterpreterCommandEntry("cdl_component",      &CdlComponentBody::parse_component ),
207
        CdlInterpreterCommandEntry("cdl_option",         &CdlOptionBody::parse_option       ),
208
        CdlInterpreterCommandEntry("cdl_interface",      &CdlInterfaceBody::parse_interface ),
209
        CdlInterpreterCommandEntry("cdl_dialog",         &CdlDialogBody::parse_dialog       ),
210
        CdlInterpreterCommandEntry("cdl_wizard",         &CdlWizardBody::parse_wizard       ),
211
        CdlInterpreterCommandEntry("",                   0                                  )
212
    };
213
    static CdlInterpreterCommandEntry   script_commands[] =
214
    {
215
        CdlInterpreterCommandEntry("cdl_component",      &CdlComponentBody::parse_component ),
216
        CdlInterpreterCommandEntry("cdl_option",         &CdlOptionBody::parse_option       ),
217
        CdlInterpreterCommandEntry("cdl_interface",      &CdlInterfaceBody::parse_interface ),
218
        CdlInterpreterCommandEntry("cdl_dialog",         &CdlDialogBody::parse_dialog       ),
219
        CdlInterpreterCommandEntry("cdl_wizard",         &CdlWizardBody::parse_wizard       ),
220
        CdlInterpreterCommandEntry("",                   0                                  ),
221
    };
222
    int i;
223
 
224
    // All parsing errors may result in an exception, under the control of
225
    // application code. This exception must not pass through the Tcl interpreter.
226
    try {
227
 
228
        for (i = 0; 0 != commands[i].command; i++) {
229
            new_commands.push_back(commands[i]);
230
        }
231
        CdlBuildableBody::add_property_parsers(new_commands);
232
        CdlDefinableBody::add_property_parsers(new_commands);
233
        CdlParentableBody::add_property_parsers(new_commands);
234
        CdlValuableBody::add_property_parsers(new_commands);
235
        CdlUserVisibleBody::add_property_parsers(new_commands);
236
        CdlNodeBody::add_property_parsers(new_commands);
237
 
238
        // Now evaluate the body. If an error occurs then typically
239
        // this will be reported via CdlParse::report_error(),
240
        // but any exceptions will have been intercepted and
241
        // turned into a Tcl error.
242
        old_commands = interp->push_commands(new_commands);
243
        result = interp->eval(argv[2], tcl_result);
244
        interp->pop_commands(old_commands);
245
 
246
        if (TCL_OK != result) {
247
            // No point in taking any further action, just go with the flow
248
            goto done2;
249
        }
250
 
251
        // Even if there were errors, they were not fatal. There may
252
        // now be a number of properties for this component, and some
253
        // validation should take place. Start with the base classes.
254
        new_component->CdlNodeBody::check_properties(interp);
255
        new_component->CdlUserVisibleBody::check_properties(interp);
256
        new_component->CdlValuableBody::check_properties(interp);
257
        new_component->CdlParentableBody::check_properties(interp);
258
        new_component->CdlBuildableBody::check_properties(interp);
259
        new_component->CdlDefinableBody::check_properties(interp);
260
 
261
        // There should be at most one each of wizard and script.
262
        if (new_component->count_properties(CdlPropertyId_Wizard) > 1) {
263
            CdlParse::report_error(interp, "", "A component should have at most one `wizard' property.");
264
        }
265
        if (new_component->count_properties(CdlPropertyId_Script) > 1) {
266
            CdlParse::report_error(interp, "", "A component should have at most one `script' property.");
267
        }
268
 
269
        // If there is a script property, life gets more interesting.
270
        if (new_component->has_property(CdlPropertyId_Script)) {
271
            CdlProperty_String prop = dynamic_cast<CdlProperty_String>(new_component->get_property(CdlPropertyId_Script));
272
            CYG_PRECONDITION_CLASSC(prop);
273
            std::string script_name = prop->get_string();
274
 
275
            // Try to locate this script.
276
            std::string script_filename = package->find_absolute_file(script_name, "cdl", false);
277
            if ("" == script_filename) {
278
                CdlParse::report_error(interp, "", "Unable to find script `" + script_name + "'.");
279
            } else {
280
                // The script exists, so we need to try and execute it.
281
                // The current container is still set correctly, but we need
282
                // to change the filename and install a different set
283
                // of commands.
284
                old_context = interp->push_context(script_filename);
285
                new_commands.clear();
286
                for (i = 0; 0 != script_commands[i].command; i++) {
287
                    new_commands.push_back(script_commands[i]);
288
                }
289
                old_commands = interp->push_commands(new_commands);
290
                result = interp->eval_file(script_filename, tcl_result);
291
                interp->pop_commands(old_commands);
292
                interp->pop_context(old_context);
293
            }
294
        }
295
 
296
      done2:
297
        // Dummy command just to keep the compiler happy
298
        old_context = "";
299
 
300
    } catch (std::bad_alloc e) {
301
        // Errors at this stage should be reported via Tcl, not via C++.
302
        // However there is no point in continuing with the parsing operation,
303
        // just give up.
304
        interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Out of memory"));
305
        result = TCL_ERROR;
306
    } catch (CdlParseException e) {
307
        interp->set_result(e.get_message());
308
        result = TCL_ERROR;
309
    } catch(...) {
310
        interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Unexpected C++ exception"));
311
        result = TCL_ERROR;
312
    }
313
 
314
    // Restore the interpreter to its prior state.
315
    interp->pop_node(old_node);
316
    interp->pop_container(old_container);
317
    if (0 != old_commands) {
318
        interp->pop_commands(old_commands);
319
    }
320
 
321
    CYG_REPORT_RETVAL(result);
322
    return result;
323
}
324
 
325
 
326
// ----------------------------------------------------------------------------
327
// Syntax: script <filename>
328
int
329
CdlComponentBody::parse_script(CdlInterpreter interp, int argc, const char* argv[])
330
{
331
    CYG_REPORT_FUNCNAMETYPE("parse_script", "result %d");
332
 
333
    int result = CdlParse::parse_string_property(interp, argc, argv, CdlPropertyId_Script, 0, 0);
334
 
335
    CYG_REPORT_RETVAL(result);
336
    return result;
337
}
338
 
339
//}}}
340
//{{{  Propagation support              
341
 
342
// ----------------------------------------------------------------------------
343
void
344
CdlComponentBody::update(CdlTransaction transaction, CdlUpdate update)
345
{
346
    CYG_REPORT_FUNCNAME("CdlComponent::update");
347
 
348
    this->CdlValuableBody::update(transaction, update);
349
    this->CdlContainerBody::update(transaction, update);
350
 
351
    CYG_REPORT_RETURN();
352
}
353
 
354
//}}}
355
//{{{  Persistence support              
356
 
357
// ----------------------------------------------------------------------------
358
void
359
CdlComponentBody::initialize_savefile_support(CdlToplevel toplevel)
360
{
361
    CYG_REPORT_FUNCNAME("CdlComponent::initialize_savefile_support");
362
 
363
    toplevel->add_savefile_command("cdl_component", 0, &savefile_component_command);
364
    CdlValuableBody::initialize_savefile_support(toplevel, "cdl_component");
365
 
366
    CYG_REPORT_RETURN();
367
}
368
 
369
void
370
CdlComponentBody::save(CdlInterpreter interp, Tcl_Channel chan, int indentation, bool minimal)
371
{
372
    CYG_REPORT_FUNCNAME("CdlComponent::save");
373
    CYG_REPORT_FUNCARG5XV(this, interp, chan, indentation, minimal);
374
    CYG_PRECONDITION_THISC();
375
    CYG_PRECONDITION_CLASSC(interp);
376
 
377
    if (!minimal || this->has_additional_savefile_information() || this->value_savefile_entry_needed()) {
378
        // Start with the UserVisible data, which will result in a suitable set
379
        // of comments before the package definition itself.
380
        this->CdlUserVisibleBody::save(interp, chan, indentation, minimal);
381
 
382
        // Now output the line "cdl_component <name> {"
383
        // The name is guaranteed to be a valid C preprocessor symbol, so it
384
        // is not going to need any quoting.
385
        std::string data = std::string(indentation, ' ') + "cdl_component " + get_name() + " {\n";
386
        interp->write_data(chan, data);
387
 
388
        // Deal with the value
389
        bool modifiable = !(CdlValueFlavor_None == this->get_flavor()) &&
390
            !this->has_property(CdlPropertyId_Calculated);
391
        this->CdlValuableBody::save_valuable(interp, chan, indentation + 4, modifiable, minimal);
392
 
393
        // And with any unrecognised data
394
        this->CdlNodeBody::save(interp, chan, indentation + 4, minimal);
395
 
396
        // Close the cdl_component body. A blank line is added here.
397
        interp->write_data(chan, "};\n\n");
398
    }
399
 
400
    // Packages are containers, so dump the contents as well.
401
    this->CdlContainerBody::save(interp, chan, indentation, minimal);
402
 
403
    CYG_REPORT_RETURN();
404
}
405
 
406
int
407
CdlComponentBody::savefile_component_command(CdlInterpreter interp, int argc, const char* argv[])
408
{
409
    CYG_REPORT_FUNCNAMETYPE("CdlComponent::savefile_component_command", "result %d");
410
    CYG_PRECONDITION_CLASSC(interp);
411
 
412
    int result = TCL_OK;
413
    CdlToplevel toplevel = interp->get_toplevel();
414
    CYG_ASSERT_CLASSC(toplevel);
415
    CdlConfiguration config = dynamic_cast<CdlConfiguration>(toplevel);
416
    CYG_ASSERT_CLASSC(config);
417
 
418
    std::vector<CdlInterpreterCommandEntry> subcommands;
419
    std::vector<CdlInterpreterCommandEntry>* toplevel_commands = 0;
420
    CdlNode old_node = 0;
421
 
422
    try {
423
 
424
        if (3 != argc) {
425
            CdlParse::report_error(interp, "", "Invalid cdl_component command in savefile, expecting two arguments.");
426
        } else {
427
 
428
            CdlNode current_node = config->lookup(argv[1]);
429
            if (0 == current_node) {
430
                // FIXME: save value in limbo
431
                CdlParse::report_error(interp, "",
432
                                       std::string("The savefile contains a cdl_component command for an unknown component `")
433
                                       + argv[1] + "'");
434
            } else {
435
                config->get_savefile_subcommands("cdl_component", subcommands);
436
                toplevel_commands = interp->push_commands(subcommands);
437
                old_node = interp->push_node(current_node);
438
 
439
                std::string tcl_result;
440
                result = interp->eval(argv[2], tcl_result);
441
 
442
                interp->pop_commands(toplevel_commands);
443
                toplevel_commands = 0;
444
                interp->pop_node(old_node);
445
                old_node = 0;
446
            }
447
        }
448
    } catch(...) {
449
        if (0 != old_node) {
450
            interp->pop_node(old_node);
451
        }
452
        if (0 != toplevel_commands) {
453
            interp->pop_commands(toplevel_commands);
454
        }
455
        throw;
456
    }
457
 
458
    CYG_REPORT_RETVAL(result);
459
    return result;
460
}
461
 
462
//}}}
463
//{{{  check_this()                     
464
 
465
// ----------------------------------------------------------------------------
466
 
467
bool
468
CdlComponentBody::check_this(cyg_assert_class_zeal zeal) const
469
{
470
    if (CdlComponentBody_Magic != cdlcomponentbody_cookie) {
471
        return false;
472
    }
473
    CYGDBG_MEMLEAK_CHECKTHIS();
474
 
475
    return CdlNodeBody::check_this(zeal)        &&
476
           CdlContainerBody::check_this(zeal)   &&
477
           CdlUserVisibleBody::check_this(zeal) &&
478
           CdlParentableBody::check_this(zeal)  &&
479
           CdlValuableBody::check_this(zeal)    &&
480
           CdlBuildableBody::check_this(zeal)   &&
481
           CdlDefinableBody::check_this(zeal);
482
}
483
 
484
//}}}
485
//{{{  Misc                             
486
 
487
// ----------------------------------------------------------------------------
488
 
489
std::string
490
CdlComponentBody::get_class_name() const
491
{
492
    CYG_REPORT_FUNCNAME("CdlComponent::get_class_name");
493
    CYG_PRECONDITION_THISC();
494
    CYG_REPORT_RETURN();
495
    return "component";
496
}
497
 
498
//}}}

powered by: WebSVN 2.1.0

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