summaryrefslogtreecommitdiffstats
path: root/kig/misc/object_hierarchy.cc
blob: 9b5a09a85e39747881b15513563646dfb3684bc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
// Copyright (C)  2003  Dominique Devriese <devriese@kde.org>

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
// 02110-1301, USA.

#include "object_hierarchy.h"

#include "../objects/object_holder.h"
#include "../objects/other_type.h"
#include "../objects/object_imp.h"
#include "../objects/object_imp_factory.h"
#include "../objects/object_type_factory.h"
#include "../objects/bogus_imp.h"
#include "../objects/transform_types.h"
#include "../objects/object_type.h"

#include <kglobal.h>
#include <tqdom.h>

class ObjectHierarchy::Node
{
public:
  enum { ID_PushStack, ID_ApplyType, ID_FetchProp };
  virtual int id() const = 0;

  virtual ~Node();
  virtual Node* copy() const = 0;

  virtual void apply( std::vector<const ObjectImp*>& stack, int loc,
                      const KigDocument& ) const = 0;

  virtual void apply( std::vector<ObjectCalcer*>& stack, int loc ) const = 0;

  // this function is used to check whether the final objects depend
  // on the given objects.  The dependsstack contains a set of
  // booleans telling which parts of the hierarchy certainly depend on
  // the given objects.  In this function, the node should check
  // whether any of its parents have true set, and if so, set its own
  // value to true.
  virtual void checkDependsOnGiven( std::vector<bool>& dependsstack, int loc ) const = 0;
  // this function is used to check whether the given objects are all
  // used by one or more of the final objects.  The usedstack contains
  // a set of booleans telling which parts of the hierarchy are
  // certainly ancestors of the final objects.  In this function, the
  // node should set all of its parents' booleans to true.
  virtual void checkArgumentsUsed( std::vector<bool>& usedstack ) const = 0;
};

ObjectHierarchy::Node::~Node()
{
}

class PushStackNode
  : public ObjectHierarchy::Node
{
  ObjectImp* mimp;
public:
  PushStackNode( ObjectImp* imp ) : mimp( imp ) {}
  ~PushStackNode();

  const ObjectImp* imp() const { return mimp; }

  int id() const;
  Node* copy() const;
  void apply( std::vector<const ObjectImp*>& stack,
              int loc, const KigDocument& ) const;
  void apply( std::vector<ObjectCalcer*>& stack, int loc ) const;

  void checkDependsOnGiven( std::vector<bool>& dependsstack, int loc ) const;
  void checkArgumentsUsed( std::vector<bool>& usedstack ) const;
};

void PushStackNode::checkArgumentsUsed( std::vector<bool>& ) const
{
}

void PushStackNode::apply( std::vector<ObjectCalcer*>& stack, int loc ) const
{
  stack[loc] = new ObjectConstCalcer( mimp->copy() );
}

void PushStackNode::checkDependsOnGiven( std::vector<bool>&, int ) const {
  // pushstacknode depends on nothing..
  return;
}

int PushStackNode::id() const { return ID_PushStack; }

PushStackNode::~PushStackNode()
{
  delete mimp;
}

ObjectHierarchy::Node* PushStackNode::copy() const
{
  return new PushStackNode( mimp->copy() );
}

void PushStackNode::apply( std::vector<const ObjectImp*>& stack,
                           int loc, const KigDocument& ) const
{
  stack[loc] = mimp->copy();
}

class ApplyTypeNode
  : public ObjectHierarchy::Node
{
  const ObjectType* mtype;
  std::vector<int> mparents;
public:
  ApplyTypeNode( const ObjectType* type, const std::vector<int>& parents )
    : mtype( type ), mparents( parents ) {}
  ~ApplyTypeNode();
  Node* copy() const;

  const ObjectType* type() const { return mtype; }
  const std::vector<int>& parents() const { return mparents; }

  int id() const;
  void apply( std::vector<const ObjectImp*>& stack,
              int loc, const KigDocument& ) const;
  void apply( std::vector<ObjectCalcer*>& stack, int loc ) const;

  void checkDependsOnGiven( std::vector<bool>& dependsstack, int loc ) const;
  void checkArgumentsUsed( std::vector<bool>& usedstack ) const;
};

int ApplyTypeNode::id() const { return ID_ApplyType; }

void ApplyTypeNode::checkArgumentsUsed( std::vector<bool>& usedstack ) const
{
  for ( uint i = 0; i < mparents.size(); ++i )
  {
    usedstack[mparents[i]] = true;
  }
}

void ApplyTypeNode::checkDependsOnGiven( std::vector<bool>& dependsstack, int loc ) const
{
  bool result = false;
  for ( uint i = 0; i < mparents.size(); ++i )
    if ( dependsstack[mparents[i]] == true ) result = true;
  dependsstack[loc] = result;
}

ApplyTypeNode::~ApplyTypeNode()
{
}

ObjectHierarchy::Node* ApplyTypeNode::copy() const
{
  return new ApplyTypeNode( mtype, mparents );
}

void ApplyTypeNode::apply( std::vector<ObjectCalcer*>& stack, int loc ) const
{
  std::vector<ObjectCalcer*> parents;
  for ( uint i = 0; i < mparents.size(); ++i )
    parents.push_back( stack[ mparents[i] ] );
  stack[loc] = new ObjectTypeCalcer( mtype, parents );
}

void ApplyTypeNode::apply( std::vector<const ObjectImp*>& stack,
                           int loc, const KigDocument& doc ) const
{
  Args args;
  for ( uint i = 0; i < mparents.size(); ++i )
    args.push_back( stack[mparents[i]] );
  args = mtype->sortArgs( args );
  stack[loc] = mtype->calc( args, doc );
}

class FetchPropertyNode
  : public ObjectHierarchy::Node
{
  mutable int mpropid;
  int mtqparent;
  const TQCString mname;
public:
  // propid is a cache of the location of name in the tqparent's
  // propertiesInternalNames(), just as it is in PropertyObject.  We
  // don't want to ever save this value, since we cannot guarantee it
  // remains consistent if we add properties some place..
  FetchPropertyNode( const int tqparent, const TQCString& name, const int propid = -1 )
    : mpropid( propid ), mtqparent( tqparent ), mname( name ) {}
  ~FetchPropertyNode();
  Node* copy() const;

  void checkDependsOnGiven( std::vector<bool>& dependsstack, int loc ) const;
  void checkArgumentsUsed( std::vector<bool>& usedstack ) const;
  int tqparent() const { return mtqparent; }
  const TQCString& propinternalname() const { return mname; }

  int id() const;
  void apply( std::vector<const ObjectImp*>& stack,
              int loc, const KigDocument& ) const;
  void apply( std::vector<ObjectCalcer*>& stack, int loc ) const;
};

FetchPropertyNode::~FetchPropertyNode()
{
}

void FetchPropertyNode::checkArgumentsUsed( std::vector<bool>& usedstack ) const
{
  usedstack[mtqparent] = true;
}

void FetchPropertyNode::checkDependsOnGiven( std::vector<bool>& dependsstack, int loc ) const
{
  dependsstack[loc] = dependsstack[mtqparent];
}

ObjectHierarchy::Node* FetchPropertyNode::copy() const
{
  return new FetchPropertyNode( mtqparent, mname, mpropid );
}

int FetchPropertyNode::id() const
{
  return ID_FetchProp;
}

void FetchPropertyNode::apply( std::vector<const ObjectImp*>& stack,
                               int loc, const KigDocument& d ) const
{
  assert( stack[mtqparent] );
  if ( mpropid == -1 ) mpropid = stack[mtqparent]->propertiesInternalNames().findIndex( mname );
  if ( mpropid != -1 )
    stack[loc] = stack[mtqparent]->property( mpropid, d );
  else
    stack[loc] = new InvalidImp();
}

void FetchPropertyNode::apply( std::vector<ObjectCalcer*>& stack, int loc ) const
{
  if ( mpropid == -1 )
    mpropid = stack[mtqparent]->imp()->propertiesInternalNames().findIndex( mname );
  assert( mpropid != -1 );
  stack[loc] = new ObjectPropertyCalcer( stack[mtqparent], mpropid );
}

std::vector<ObjectImp*> ObjectHierarchy::calc( const Args& a, const KigDocument& doc ) const
{
  assert( a.size() == mnumberofargs );
  for ( uint i = 0; i < a.size(); ++i )
    assert( a[i]->inherits( margrequirements[i] ) );

  std::vector<const ObjectImp*> stack;
  stack.resize( mnodes.size() + mnumberofargs, 0 );
  std::copy( a.begin(), a.end(), stack.begin() );
  for( uint i = 0; i < mnodes.size(); ++i )
  {
    mnodes[i]->apply( stack, mnumberofargs + i, doc );
  };
  for ( uint i = mnumberofargs; i < stack.size() - mnumberofresults; ++i )
    delete stack[i];
  if ( stack.size() < mnumberofargs + mnumberofresults )
  {
    std::vector<ObjectImp*> ret;
    ret.push_back( new InvalidImp );
    return ret;
  }
  else
  {
    std::vector<ObjectImp*> ret;
    for ( uint i = stack.size() - mnumberofresults; i < stack.size(); ++i )
      ret.push_back( const_cast<ObjectImp*>( stack[i] ) );
    return ret;
  };
}

int ObjectHierarchy::visit( const ObjectCalcer* o, std::map<const ObjectCalcer*, int>& seenmap,
                            bool needed, bool neededatend )
{
  using namespace std;

  std::map<const ObjectCalcer*, int>::iterator smi = seenmap.find( o );
  if ( smi != seenmap.end() )
  {
    if ( neededatend )
    {
      // neededatend means that this object is one of the resultant
      // objects.  Therefore, its node has to appear at the end,
      // because that's where we expect it..  We therefore copy it
      // there using CopyObjectType..
      int ret = mnumberofargs + mnodes.size();
      std::vector<int> parents;
      parents.push_back( smi->second );
      mnodes.push_back( new ApplyTypeNode( CopyObjectType::instance(), parents ) );
      return ret;
    }
    else return smi->second;
  }

  std::vector<ObjectCalcer*> p( o->parents() );
  // we check if o descends from the given objects..
  bool descendsfromgiven = false;
  std::vector<int> parents;
  parents.resize( p.size(), -1 );
  for ( uint i = 0; i < p.size(); ++i )
  {
    int v = visit( p[i], seenmap, false );
    parents[i] = v;
    descendsfromgiven |= (v != -1);
  };

  if ( ! descendsfromgiven && ! ( needed && o->imp()->isCache() ) )
  {
    if ( needed )
    {
      assert( ! o->imp()->isCache() );
      // o is an object that does not depend on the given objects, but
      // is needed by other objects, so we just have to just save its
      // current value here.
      Node* node = new PushStackNode( o->imp()->copy() );
      mnodes.push_back( node );
      int ret = mnodes.size() + mnumberofargs - 1;
      seenmap[o] = ret;
      return ret;
    }
    else
      return -1;
  };

  return storeObject( o, p, parents, seenmap );
}

ObjectHierarchy::~ObjectHierarchy()
{
  for ( uint i = 0; i < mnodes.size(); ++i ) delete mnodes[i];
}

ObjectHierarchy::ObjectHierarchy( const ObjectHierarchy& h )
  : mnumberofargs( h.mnumberofargs ), mnumberofresults( h.mnumberofresults ),
    margrequirements( h.margrequirements ), musetexts( h.musetexts ),
    mselectstatements( h.mselectstatements )
{
  mnodes.reserve( h.mnodes.size() );
  for ( uint i = 0; i < h.mnodes.size(); ++i )
    mnodes.push_back( h.mnodes[i]->copy() );
}

ObjectHierarchy ObjectHierarchy::withFixedArgs( const Args& a ) const
{
  assert( a.size() <= mnumberofargs );
  ObjectHierarchy ret( *this );

  ret.mnumberofargs -= a.size();
  ret.margrequirements.resize( ret.mnumberofargs );

  std::vector<Node*> newnodes( mnodes.size() + a.size() );
  std::vector<Node*>::iterator newnodesiter = newnodes.begin();
  for ( uint i = 0; i < a.size(); ++i )
  {
    assert( ! a[i]->isCache() );
    *newnodesiter++ = new PushStackNode( a[i]->copy() );
  };
  std::copy( ret.mnodes.begin(), ret.mnodes.end(), newnodesiter );
  ret.mnodes = newnodes;

  return ret;
}

void ObjectHierarchy::init( const std::vector<ObjectCalcer*>& from, const std::vector<ObjectCalcer*>& to )
{
  mnumberofargs = from.size();
  mnumberofresults = to.size();
  margrequirements.resize( from.size(), ObjectImp::stype() );
  musetexts.resize( margrequirements.size(), "" );
  std::map<const ObjectCalcer*, int> seenmap;
  for ( uint i = 0; i < from.size(); ++i )
    seenmap[from[i]] = i;
  for ( std::vector<ObjectCalcer*>::const_iterator i = to.begin(); i != to.end(); ++i )
  {
    std::vector<ObjectCalcer*> parents = (*i)->parents();
    for ( std::vector<ObjectCalcer*>::const_iterator j = parents.begin();
          j != parents.end(); ++j )
      visit( *j, seenmap, true );
  }
  for ( std::vector<ObjectCalcer*>::const_iterator i = to.begin(); i != to.end(); ++i )
    visit( *i, seenmap, true, true );

  mselectstatements.resize( margrequirements.size(), "" );
}

ObjectHierarchy::ObjectHierarchy( const std::vector<ObjectCalcer*>& from, const ObjectCalcer* to )
{
  std::vector<ObjectCalcer*> tov;
  tov.push_back( const_cast<ObjectCalcer*>( to ) );
  init( from, tov );
}

ObjectHierarchy::ObjectHierarchy( const std::vector<ObjectCalcer*>& from, const std::vector<ObjectCalcer*>& to )
{
  init( from, to );
}

void ObjectHierarchy::serialize( TQDomElement& tqparent, TQDomDocument& doc ) const
{
  int id = 1;
  for ( uint i = 0; i < mnumberofargs; ++i )
  {
    TQDomElement e = doc.createElement( "input" );
    e.setAttribute( "id", id++ );
    e.setAttribute( "requirement", margrequirements[i]->internalName() );
    // we don't save these atm, since the user can't define them.
    // we only load them from builtin macro's.
//     TQDomElement ut = doc.createElement( "UseText" );
//     ut.appendChild( doc.createTextNode( TQString::tqfromLatin1(musetexts[i].c_str() ) ) );
//     e.appendChild( ut );
//     TQDomElement ss = doc.createElement( "SelectStatement" );
//     ss.appendChild( doc.createTextNode( TQString::tqfromLatin1(mselectstatements[i].c_str() ) ) );
//     e.appendChild( ss );
    tqparent.appendChild( e );
  }

  for ( uint i = 0; i < mnodes.size(); ++i )
  {
    bool result = mnodes.size() - ( id - mnumberofargs - 1 ) <= mnumberofresults;
    TQDomElement e = doc.createElement( result ? "result" : "intermediate" );
    e.setAttribute( "id", id++ );

    if ( mnodes[i]->id() == Node::ID_ApplyType )
    {
      const ApplyTypeNode* node = static_cast<const ApplyTypeNode*>( mnodes[i] );
      e.setAttribute( "action", "calc" );
      e.setAttribute( "type", TQString::tqfromLatin1( node->type()->fullName() ) );
      for ( uint i = 0; i < node->parents().size(); ++i )
      {
        int tqparent = node->parents()[i] + 1;
        TQDomElement arge = doc.createElement( "arg" );
        arge.appendChild( doc.createTextNode( TQString::number( tqparent ) ) );
        e.appendChild( arge );
      };
    }
    else if ( mnodes[i]->id() == Node::ID_FetchProp )
    {
      const FetchPropertyNode* node = static_cast<const FetchPropertyNode*>( mnodes[i] );
      e.setAttribute( "action", "fetch-property" );
      e.setAttribute( TQString("property"), TQString(node->propinternalname()) );
      TQDomElement arge = doc.createElement( "arg" );
      arge.appendChild( doc.createTextNode( TQString::number( node->tqparent() + 1 ) ) );
      e.appendChild( arge );
    }
    else
    {
      assert( mnodes[i]->id() == ObjectHierarchy::Node::ID_PushStack );
      const PushStackNode* node = static_cast<const PushStackNode*>( mnodes[i] );
      e.setAttribute( "action", "push" );
      TQString type = ObjectImpFactory::instance()->serialize( *node->imp(), e, doc );
      e.setAttribute( "type", type );
    };

    tqparent.appendChild( e );
  };
}

ObjectHierarchy::ObjectHierarchy()
  : mnumberofargs( 0 ), mnumberofresults( 0 )
{
}

ObjectHierarchy* ObjectHierarchy::buildSafeObjectHierarchy( const TQDomElement& tqparent, TQString& error )
{
#define KIG_GENERIC_PARSE_ERROR \
  { \
    error = i18n( "An error was encountered at line %1 in file %2." ) \
            .tqarg( __LINE__ ).tqarg( __FILE__ ); \
    return 0; \
  }

  ObjectHierarchy* obhi = new ObjectHierarchy();

  bool ok = true;
  TQString tmp;
  TQDomElement e = tqparent.firstChild().toElement();
  for (; !e.isNull(); e = e.nextSibling().toElement() )
  {
    if ( e.tagName() != "input" ) break;

    tmp = e.attribute( "id" );
    uint id = tmp.toInt( &ok );
    if ( !ok ) KIG_GENERIC_PARSE_ERROR;

    obhi->mnumberofargs = kMax( id, obhi->mnumberofargs );

    tmp = e.attribute( "requirement" );
    const ObjectImpType* req = ObjectImpType::typeFromInternalName( tmp.latin1() );
    if ( req == 0 ) req = ObjectImp::stype(); // sucks, i know..
    obhi->margrequirements.resize( obhi->mnumberofargs, ObjectImp::stype() );
    obhi->musetexts.resize( obhi->mnumberofargs, "" );
    obhi->mselectstatements.resize( obhi->mnumberofargs, "" );
    obhi->margrequirements[id - 1] = req;
    obhi->musetexts[id - 1] = req->selectStatement();
    TQDomElement esub = e.firstChild().toElement();
    for ( ; !esub.isNull(); esub = esub.nextSibling().toElement() )
    {
      if ( esub.tagName() == "UseText" )
      {
        obhi->musetexts[id - 1] = esub.text().latin1();
      }
      else if ( esub.tagName() == "SelectStatement" )
      {
        obhi->mselectstatements[id - 1] = esub.text().latin1();
      }
      else
      {
        // broken file ? ignore...
      }
    }
  }
  for (; !e.isNull(); e = e.nextSibling().toElement() )
  {
    bool result = e.tagName() == "result";
    if ( result ) ++obhi->mnumberofresults;

    tmp = e.attribute( "id" );
    int id = tmp.toInt( &ok );
    if ( !ok ) KIG_GENERIC_PARSE_ERROR;

    tmp = e.attribute( "action" );
    Node* newnode = 0;
    if ( tmp == "calc" )
    {
      // ApplyTypeNode
      TQCString typen = e.attribute( "type" ).latin1();
      const ObjectType* type = ObjectTypeFactory::instance()->find( typen );
      if ( ! type )
      {
        error = i18n( "This Kig file uses an object of type \"%1\", "
                      "which this Kig version does not support."
                      "Perhaps you have compiled Kig without support "
                      "for this object type,"
                      "or perhaps you are using an older Kig version." ).tqarg( TQString(typen) );
        return 0;
      }

      std::vector<int> parents;
      for ( TQDomNode p = e.firstChild(); !p.isNull(); p = p.nextSibling() )
      {
        TQDomElement q = p.toElement();
        if ( q.isNull() ) KIG_GENERIC_PARSE_ERROR; // see above
        if ( q.tagName() != "arg" ) KIG_GENERIC_PARSE_ERROR;
        int pid = q.text().toInt(&ok );
        if ( !ok ) KIG_GENERIC_PARSE_ERROR;
        parents.push_back( pid - 1 );
      };
      newnode = new ApplyTypeNode( type, parents );
    }
    else if ( tmp == "fetch-property" )
    {
      // FetchPropertyNode
      TQCString propname = e.attribute( "property" ).latin1();
      TQDomElement arge = e.firstChild().toElement();
      int tqparent = arge.text().toInt( &ok );
      if ( !ok ) KIG_GENERIC_PARSE_ERROR;
      newnode = new FetchPropertyNode( tqparent - 1, propname );
    }
    else
    {
      // PushStackNode
      if ( e.attribute( "action" ) != "push" ) KIG_GENERIC_PARSE_ERROR;
      TQString typen = e.attribute( "type" );
      if ( typen.isNull() ) KIG_GENERIC_PARSE_ERROR;
      ObjectImp* imp = ObjectImpFactory::instance()->deserialize( typen, e, error );
      if ( ( ! imp ) && !error.isEmpty() ) return 0;
      newnode = new PushStackNode( imp );
    };
    obhi->mnodes.resize( kMax( size_t(id - obhi->mnumberofargs), obhi->mnodes.size() ) );
    obhi->mnodes[id - obhi->mnumberofargs - 1] = newnode;
  };

  // if we are here, all went fine
  return obhi;
}

ArgsParser ObjectHierarchy::argParser() const
{
  std::vector<ArgsParser::spec> specs;
  for ( uint i = 0; i < margrequirements.size(); ++i )
  {
    const ObjectImpType* req = margrequirements[i];
    ArgsParser::spec spec;
    spec.type = req;
    spec.usetext = musetexts[i];
    spec.selectstat = mselectstatements[i];
    specs.push_back( spec );
  };
  return ArgsParser( specs );
}

std::vector<ObjectCalcer*> ObjectHierarchy::buildObjects( const std::vector<ObjectCalcer*>& os, const KigDocument& doc ) const
{
  assert( os.size() == mnumberofargs );
  for ( uint i = 0; i < os.size(); ++i )
    assert( os[i]->imp()->inherits( margrequirements[i] ) );

  std::vector<ObjectCalcer*> stack;
  stack.resize( mnodes.size() + mnumberofargs, 0 );
  std::copy( os.begin(), os.end(), stack.begin() );

  for( uint i = 0; i < mnodes.size(); ++i )
  {
    mnodes[i]->apply( stack, mnumberofargs + i );
    stack[mnumberofargs + i]->calc( doc );
  };

  std::vector<ObjectCalcer*> ret( stack.end() - mnumberofresults, stack.end() );

  return ret;
}

const ObjectImpType* ObjectHierarchy::idOfLastResult() const
{
  const Node* n = mnodes.back();
  if ( n->id() == Node::ID_PushStack )
    return static_cast<const PushStackNode*>( n )->imp()->type();
  else if ( n->id() == Node::ID_FetchProp )
    return ObjectImp::stype();
  else
    return static_cast<const ApplyTypeNode*>( n )->type()->resultId();
}

ObjectHierarchy ObjectHierarchy::transformFinalObject( const Transformation& t ) const
{
  assert( mnumberofresults == 1 );
  ObjectHierarchy ret( *this );
  ret.mnodes.push_back( new PushStackNode( new TransformationImp( t ) ) );

  std::vector<int> parents;
  parents.push_back( ret.mnodes.size() - 1);
  parents.push_back( ret.mnodes.size() );
  const ObjectType* type = ApplyTransformationObjectType::instance();
  ret.mnodes.push_back( new ApplyTypeNode( type, parents ) );
  return ret;
}

bool operator==( const ObjectHierarchy& lhs, const ObjectHierarchy& rhs )
{
  if ( ! ( lhs.mnumberofargs == rhs.mnumberofargs &&
           lhs.mnumberofresults == rhs.mnumberofresults &&
           lhs.margrequirements == rhs.margrequirements &&
           lhs.mnodes.size() == rhs.mnodes.size() ) )
    return false;

  // this isn't entirely correct, but it will do, because we don't
  // really want to know whether the hierarchies are different, but
  // whether rhs has changed with regard to lhs..
  for ( uint i = 0; i < lhs.mnodes.size(); ++i )
    if ( lhs.mnodes[i] != lhs.mnodes[i] )
      return false;

  return true;
}

bool ObjectHierarchy::resultDoesNotDependOnGiven() const
{
  std::vector<bool> dependsstack( mnodes.size() + mnumberofargs, false );

  for ( uint i = 0; i < mnumberofargs; ++i )
    dependsstack[i] = true;
  for ( uint i = 0; i < mnodes.size(); ++i )
    mnodes[i]->checkDependsOnGiven( dependsstack, i + mnumberofargs );
  for ( uint i = dependsstack.size() - mnumberofresults; i < dependsstack.size(); ++i )
    if ( !dependsstack[i] )
      return true;
  return false;
}

// returns the "minimum" of a and b ( in the partially ordered set of
// ObjectImpType's, using the inherits member function as comparison,
// if you for some reason like this sort of non-sense ;) ).  This
// basically means: return the type that inherits the other type,
// because if another type inherits the lowermost type, then it will
// also inherit the other..
const ObjectImpType* lowermost( const ObjectImpType* a, const ObjectImpType* b )
{
  if ( a->inherits( b ) ) return a;
  assert( b->inherits( a ) );
  return b;
}

// this function is part of the visit procedure really.  It is
// factored out, because it recurses for cache ObjectImp's.  What this
// does is, it makes sure that object o is calcable, by putting
// appropriate Node's in mnodes..  po is o->parents() and pl contains
// the location of objects that are already in mnodes and -1
// otherwise..  -1 means we have to store their ObjectImp, unless
// they're cache ObjectImp's etc.
int ObjectHierarchy::storeObject( const ObjectCalcer* o, const std::vector<ObjectCalcer*>& po, std::vector<int>& pl,
                                  std::map<const ObjectCalcer*, int>& seenmap )
{
  for ( uint i = 0; i < po.size(); ++i )
  {
    if ( pl[i] == -1 )
    {
      // we can't store cache ObjectImp's..
      if ( po[i]->imp()->isCache() )
      {
        pl[i] = visit( po[i], seenmap, true, false );
      }
      else
      {
        Node* argnode = new PushStackNode( po[i]->imp()->copy() );
        mnodes.push_back( argnode );
        int argloc = mnumberofargs + mnodes.size() - 1;
        seenmap[po[i]] = argloc;
        pl[i] = argloc;
      };
    }
    else if ( (uint) pl[i] < mnumberofargs )
    {
      ObjectCalcer* tqparent = o->parents()[i];
      std::vector<ObjectCalcer*> opl = o->parents();

      margrequirements[pl[i]] =
        lowermost( margrequirements[pl[i]],
                   o->impRequirement( tqparent, opl ) );
      musetexts[pl[i]] = margrequirements[pl[i]]->selectStatement();
    };
  };
  if ( dynamic_cast<const ObjectTypeCalcer*>( o ) )
    mnodes.push_back( new ApplyTypeNode( static_cast<const ObjectTypeCalcer*>( o )->type(), pl ) );
  else if ( dynamic_cast<const ObjectPropertyCalcer*>( o ) )
  {
    assert( pl.size() == 1 );
    int tqparent = pl.front();
    ObjectCalcer* op = po.front();
    assert( op );
    uint propid = static_cast<const ObjectPropertyCalcer*>( o )->propId();
    assert( propid < op->imp()->propertiesInternalNames().size() );
    mnodes.push_back( new FetchPropertyNode( tqparent, op->imp()->propertiesInternalNames()[propid], propid ) );
  }
  else
    assert( false );
  seenmap[o] = mnumberofargs + mnodes.size() - 1;
  return mnumberofargs + mnodes.size() - 1;
}

ObjectHierarchy::ObjectHierarchy( const ObjectCalcer* from, const ObjectCalcer* to )
{
  std::vector<ObjectCalcer*> fromv;
  fromv.push_back( const_cast<ObjectCalcer*>( from ) );
  std::vector<ObjectCalcer*> tov;
  tov.push_back( const_cast<ObjectCalcer*>( to ) );
  init( fromv, tov );
}

bool ObjectHierarchy::allGivenObjectsUsed() const
{
  std::vector<bool> usedstack( mnodes.size() + mnumberofargs, false );
  for ( uint i = mnodes.size() - mnumberofresults; i < mnodes.size(); ++i )
    usedstack[i + mnumberofargs] = true;
  for ( int i = mnodes.size() - 1; i >= 0; --i )
    if ( usedstack[i + mnumberofargs] )
      mnodes[i]->checkArgumentsUsed( usedstack );
  for ( uint i = 0; i < mnumberofargs; ++i )
    if ( ! usedstack[i] ) return false;
  return true;
}