File: genregistry.perl

package info (click to toggle)
gecode 2.1.1-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,432 kB
  • ctags: 8,211
  • sloc: cpp: 60,596; ansic: 6,727; sh: 2,934; perl: 1,800; makefile: 1,382
file content (468 lines) | stat: -rw-r--r-- 13,666 bytes parent folder | download
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
#!/usr/bin/perl -w
#
#  Main authors:
#     Guido Tack <tack@gecode.org>
#
#  Copyright:
#     Guido Tack, 2008
#
#  Last modified:
#     $Date: 2008-02-05 18:31:20 +0100 (Tue, 05 Feb 2008) $ by $Author: schulte $
#     $Revision: 6066 $
#
#  This file is part of Gecode, the generic constraint
#  development environment:
#     http://www.gecode.org
#
#  Permission is hereby granted, free of charge, to any person obtaining
#  a copy of this software and associated documentation files (the
#  "Software"), to deal in the Software without restriction, including
#  without limitation the rights to use, copy, modify, merge, publish,
#  distribute, sublicense, and/or sell copies of the Software, and to
#  permit persons to whom the Software is furnished to do so, subject to
#  the following conditions:
#
#  The above copyright notice and this permission notice shall be
#  included in all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
#  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
#  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
#  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#

%ignore = ("init" => "", "NodeType" => "");

print <<EOF
/*
 *  CAUTION:
 *    This file has been automatically generated. Do not edit,
 *    edit the following files instead:
EOF
;

foreach $file (@ARGV) {
  print " *     - $file\n";
}

print <<EOF
 *
 *  This file contains generated code fragments which are
 *  copyrighted as follows:
 *
 *  Main author:
 *     Guido Tack <tack\@gecode.org>
 *
 *  Copyright:
 *     Guido Tack, 2008
 *
 *  The generated code fragments are part of Gecode, the generic
 *  constraint development environment:
 *     http://www.gecode.org
 *
 *  Permission is hereby granted, free of charge, to any person obtaining
 *  a copy of this software and associated documentation files (the
 *  "Software"), to deal in the Software without restriction, including
 *  without limitation the rights to use, copy, modify, merge, publish,
 *  distribute, sublicense, and/or sell copies of the Software, and to
 *  permit persons to whom the Software is furnished to do so, subject to
 *  the following conditions:
 *
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

EOF
;

my %postFunctions;
my %enumerations = ("PropKind" => [ ("gecode/kernel.hh",
                                     "PK_DEF", "PK_SPEED", "PK_MEMORY") ]);

my %ifdefs = ("gecode/kernel.hh" => "1");

# Collect post functions from header files, filenames given on command line
foreach $file (@ARGV) {

  open HEADERFILE, $file || die "open failed";

  $currentPost = "";
  $currentEnum = "";
  my @enumArgs;
  $withinPost = 0;
  $withinEnum = 0;
  while ($l = <HEADERFILE>) {
    if ($l =~ /\/\/ IFDEF: (.*)/) {
      $ifdefs{$file} = "defined($1)";
    }
    if ($l =~ /GECODE_[A-Z]+_EXPORT.*void(.*)/) {
      $currentPost = $1;
      chomp($currentPost);
      $withinPost = 1;
    } elsif ($l =~ /inline void(.*)/) {
      $currentPost = $1;
      chomp($currentPost);
      $withinPost = 1;
    } elsif ($withinPost == 1) {
      chomp($l);
      $l =~ s/ [ ]+/ /g;
      $currentPost .= $l;
    } elsif ($l =~ /enum ([a-zA-Z]+)(.*)/) {
      if (not exists $ignore{$1}) {
        $withinEnum = 1;
        $currentEnum = $1;
        @enumArgs = ();
        $l = $2;
      }
    }
    if ($withinEnum == 1) {
      if ($l =~ /([A-Z]+_[A-Z_]+)/) {
        push(@enumArgs, $1);
      }
      if ($l =~ /};/) {
        $withinEnum = 0;
        unshift(@enumArgs, $file);
        $enumerations{$currentEnum} = [ @enumArgs ];
      }
    }
    if ($withinPost && $l =~ /;/) {
      $withinPost = 0;
      if ($currentPost =~ 
          /([a-z][a-zA-Z_]*)\(Space\s*\*\s*home\s*,\s*([^)]*)\)/) 
      {
        my $postFunName = $1;
        if (exists $ignore{$postFunName}) {
          print STDERR "Ignoring function $postFunName\n";
        } else {
          @args = split(/,/, $2);
          # Push full version
          my @processedArgs = processArgs(@args);
          if (recognized(@processedArgs)) {
            unshift(@processedArgs, $file);
            push(@{$postFunctions{$postFunName}}, [ @processedArgs ]);
          } else {
            print STDERR "ignored:\n  $currentPost\n";
          }
          # If function has default arguments, push shorter versions, too
          for ($i=@args-1; $i >= 0; $i--) {
            if ($args[$i] =~ /=/) {
              my @shorterArgs;
              for ($j=0; $j<$i; $j++) {
                $shorterArgs[$j] = $args[$j];
              }
              @processedArgs = processArgs(@shorterArgs);
              if (recognized(@processedArgs)) {
                unshift(@processedArgs, $file);
                push(@{$postFunctions{$postFunName}}, [ @processedArgs ]);
              }
            }
          }
        }
      }
    }
  }
}

# Sort post function argument lists by length (arity)
foreach $key (keys %postFunctions) {
  $postFunctions{$key} = [ sort {@{$a} <=> @{$b}} (@{$postFunctions{$key}}) ];
}

print "#include \"gecode/kernel.hh\"\n";

foreach $file (@ARGV) {
  print "#if $ifdefs{$file}\n";
  print "#include \"$file\"\n";
  print "#endif\n";
}

print <<EOF

#include "gecode/serialization.hh"

namespace {
EOF
;

foreach $e (keys %enumerations) {
my $file = shift(@{$enumerations{$e}});
my $ifdef = $ifdefs{$file};
  print <<EOF
#if $ifdef
Gecode::$e toEnum_$e(Gecode::Reflection::Arg* a) {
  assert(a->isString());
  const char* av = a->toString();
EOF
;
  foreach $ev (@{$enumerations{$e}}) {
    print "  if (!strcmp(av, \"$ev\"))\n";
    print "    return Gecode::$ev;\n"
  }
  print <<EOF
  throw Gecode::Reflection::ReflectionException("Internal error");
}
bool isEnum_$e(Gecode::Reflection::Arg* a) {
  if (!a->isString())
    return false;
  const char* av = a->toString();
EOF
  ;
  foreach $ev (@{$enumerations{$e}}) {
    print "  if (!strcmp(av, \"$ev\"))\n";
    print "    return true;\n"
  }
  print "  return false;\n";
  print "}\n";
  print "#endif\n";
}

print <<EOF

/// Check if \\a a corresponds to a variable in \\a vm with type \\a Var
template <class Var>
bool isVar(Gecode::Reflection::VarMap& vm, Gecode::Reflection::Arg* a) {
  if (!a->isVar())
    return false;
  Gecode::Reflection::VarSpec& s = vm.spec(a->toVar());
  typedef typename Gecode::VarViewTraits<Var>::View View;
  typedef typename Gecode::ViewVarImpTraits<View>::VarImp VarImp;
  return s.vti() == VarImp::vti;
}

/// Check if \\a a corresponds to an array of \\a Var variables in \\a vm
template <class Var>
bool isVarArgs(Gecode::Reflection::VarMap& vm, Gecode::Reflection::Arg* a) {
  if (!a->isArray())
    return false;
  Gecode::Reflection::ArrayArg& aa = *a->toArray();
  for (int i=aa.size(); i--;)
    if (!isVar<Var>(vm, aa[i]))
      return false;
  return true;
}

bool isIntSetArgs(Gecode::Reflection::Arg* a) {
  if (!a->isArray())
    return false;
  Gecode::Reflection::ArrayArg& aa = *a->toArray();
  for (int i=aa.size(); i--;)
    if (!aa[i]->isIntArray())
      return false;
  return true;
}

EOF
;

# Output one registration class per (overloaded) post function name
foreach $key (keys %postFunctions) {
  print <<EOF
class Register_$key {
public:
  /// Identifier for this post function
  static Gecode::Support::Symbol ati(void) {
    return \"Gecode::Post::$key\";
  }
  
  /// Post constraint described by \\a spec
  static void post(Gecode::Space* home, Gecode::Reflection::VarMap& vm,
                   const Gecode::Reflection::ActorSpec& spec) {
    switch(spec.noOfArgs()) {
EOF
;
  
  $arity = -1;
  foreach $f (@{$postFunctions{$key}}) {
    my $file = shift (@{$f});
    if ($arity != scalar(@{$f})) {
      if ($arity >= 0) {
        print "        throw Gecode::Reflection::ReflectionException(\"Argument type mismatch for Gecode::Post::$key\");\n";
        print "      }\n";
        print "      break;\n";
      }
      $arity = scalar(@{$f});
      print "    case $arity:\n";
      print "      {\n";
    }

    my $ifdef = $ifdefs{$file};
    print "#if $ifdef\n";
    # Check arguments
    print   "        if (";
    my $count = 0;
    my @args = @{$f};
    foreach $a (@args) {
      outputArgCond($a, $count);
      if ($count < scalar(@args)-1) {
        print " &&\n            ";
      }
      $count++;
    }
    print ")\n        {\n";

    # Declare arguments
    $count = 0;
    foreach $a (@args) {
      outputArg($a, $count);
      $count++;
    }
    
    
    # Post constraint
    print "          Gecode::$key(home, ";
    for (my $i=0; $i<$count; $i++) {
      print "x$i";
      if ($i<$count-1) {
        print ",";
      }
    }    
    print ");\n";
    print "          return;\n";
    print "        }\n";
    print "#endif\n";
  }
  if ($arity >= 0) {
    print "        throw Gecode::Reflection::ReflectionException(\"Argument type mismatch for Gecode::Post::$key\");\n";
    print "      }\n";
    print "      break;\n";
  }
  print "    default: throw Gecode::Reflection::ReflectionException(\"Argument type mismatch for Gecode::Post::$key\");\n";
  print "    }\n";
  print "  }\n";
  print "};\n\n";
}

print "class PostRegistrar {\n";
foreach $key (keys %postFunctions) {
  print "  GECODE_REGISTER1(Register_$key);\n";
}
print "};\n\n";

print "} // end anonymous namespace\n";

print <<EOF
namespace Gecode { namespace Serialization {
  
  void initRegistry(void) {
    static PostRegistrar r;
    return;
  };

  
}}
EOF
;

sub processArgs {
  my @args = @_;
  foreach (@args) {
    s/\s*(const)*\s*(unsigned int|[a-zA-Z]+).*/$2/;
  }
  return @args;
}

sub recognized {
  foreach $a (@_) {
    if ($a eq "int" || $a eq "unsigned int" || $a eq "bool") {
    } elsif ($a =~ /([a-zA-Z]+)VarArgs$/) {
    } elsif ($a =~ /([a-zA-Z]+)Var$/) {
    } elsif ($a eq "IntArgs") {
    } elsif ($a eq "IntSetArgs") {
    } elsif ($a eq "IntSet") {
    } elsif (exists $enumerations{$a}) {
    } else {
      return 0;
    }
  }
  return 1;
}

sub outputArgCond {
  if ($_[0] eq "int" || $_[0] eq "unsigned int" || $_[0] eq "bool") {
    print "spec[$_[1]]->isInt()";
  } elsif ($_[0] =~ /([a-zA-Z]+)VarArgs$/) {
    print "isVarArgs<Gecode::$1Var>(vm, spec[$_[1]])";
  } elsif ($_[0] =~ /([a-zA-Z]+)Var$/) {
    print "isVar<Gecode::$1Var>(vm, spec[$_[1]])";
  } elsif ($_[0] eq "IntArgs") {
    print "spec[$_[1]]->isIntArray()";
  } elsif ($_[0] eq "IntSetArgs") {
    print "isIntSetArgs(spec[$_[1]])";
  } elsif ($_[0] eq "IntSet") {
    print "spec[$_[1]]->isIntArray()";
  } elsif (exists $enumerations{$_[0]}) {
    print "isEnum_$_[0](spec[$_[1]])";
  } else {
    print "unknown($_[0])";
  }
}

sub outputArg {
  if ($_[0] eq "int" || $_[0] eq "unsigned int" || $_[0] eq "bool") {
    print "          ";
    print "$_[0] x$_[1] = static_cast<$_[0]>(spec[$_[1]]->toInt());\n";
  } elsif ($_[0] =~ /([a-zA-Z]+)VarArgs$/) {
    print "          ";
    print "Gecode::Reflection::ArrayArg& a$_[1] = *spec[$_[1]]->toArray();\n";
    print "          ";
    print "Gecode::VarArgArray<Gecode::$1Var> x$_[1](a$_[1].size());\n";
    print "          ";
    print "for (int i=a".$_[1].".size(); i--;)\n";
    print "          ";
    print "  x$_[1]"."[i] = Gecode::$1Var(vm.var(a$_[1]"."[i]->toVar()));\n"
  } elsif ($_[0] =~ /([a-zA-Z]+)Var$/) {
    print "          ";
    print "Gecode::$1Var x$_[1](vm.var(spec[$_[1]]->toVar()));\n"
  } elsif ($_[0] eq "IntArgs") {
    print "          ";
    print "Gecode::Reflection::IntArrayArg& a$_[1] = ".
          "*spec[$_[1]]->toIntArray();\n";
    print "          ";
    print "Gecode::IntArgs x$_[1](a$_[1].size());\n";
    print "          ";
    print "for (int i=a".$_[1].".size(); i--;) ".
          "x$_[1]"."[i] = a$_[1]"."[i];\n"
  } elsif ($_[0] eq "IntSetArgs") {
    print "          ";
    print "Gecode::Reflection::ArrayArg& a$_[1] = *spec[$_[1]]->toArray();\n";
    print "          ";
    print "Gecode::IntSetArgs x$_[1](a$_[1].size());\n";
    print "          ";
    print "for (int i=a".$_[1].".size(); i--;) {\n";

    print "            ";
    print "Gecode::Reflection::IntArrayArg* aa$_[1] = ".
          "a$_[1]"."[i]->toIntArray();\n";
    print "            ";
    print "Gecode::Reflection::IntArrayArgRanges aar$_[1](aa$_[1]);\n";
    print "            ";
    print "x$_[1]"."[i] = Gecode::IntSet(aar$_[1]);\n";
    print "          }\n";
  } elsif ($_[0] eq "IntSet") {
    print "          ";
    print "Gecode::Reflection::IntArrayArg* a$_[1] = ".
          "spec[$_[1]]->toIntArray();\n";
    print "          ";
    print "Gecode::Reflection::IntArrayArgRanges ar$_[1](a$_[1]);\n";
    print "          ";
    print "Gecode::IntSet x$_[1](ar$_[1]);\n";
  } elsif (exists $enumerations{$_[0]}) {
    print "          ";
    print "Gecode::$_[0] x$_[1] = toEnum_$_[0](spec[$_[1]]);\n";
  } else {
    print "unknown($_[0])";
  }  
}