aboutsummaryrefslogtreecommitdiff
path: root/Tools/CodeGen/ParamCheck.m
blob: 263bee86e783d847848b97aa3c59c8d45a736131 (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
(*  Copyright 2012 Ian Hinder

    This file is part of Kranc.

    Kranc 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.

    Kranc 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 Kranc; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*)

BeginPackage["ParamCheck`", {"Errors`", "Helpers`", "Kranc`", "CodeGenCactus`",
                             "CodeGenC`", "CodeGen`"}];

ParameterCheckSource;
ParameterCheckSchedule;

Begin["`Private`"];

DefFn[
  ParameterCheckSource[thornName_String, conditions_List] :=
  {Map[IncludeFile, 
       {"cctk.h", "cctk_Arguments.h", "cctk_Parameters.h"}],

   DefineCCTKFunction[
     thornName<>"_ParamCheck", "void",
     ConditionalOnParameterTextual[
       "CCTK_MyProc(cctkGH) == 0",
       Map[checkCondition, conditions]]]}];

DefFn[
  ParameterCheckSchedule[thornName_String] :=
  {Name          -> thornName<>"_ParamCheck",
   SchedulePoint -> "at CCTK_PARAMCHECK",
   Options       -> "global",
   Language      -> "C", 
   Comment       -> "Check parameter consistency"}];

DefFn[checkCondition[{cond_, error_String}] :=
  Module[
    {rendervalue, renderbool, paramPattern},

    paramPattern = Except[True | False, _Symbol | _Parameter];

    renderbool[Equal[a:paramPattern,b_String]] := {"CCTK_EQUALS(", rendervalue[a], ",\"", b,"\")"};
    renderbool[Unequal[a:paramPattern,b_String]] := {"!CCTK_EQUALS(", rendervalue[a], ",\"", b,"\")"};
    renderbool[Equal[a:paramPattern,b_?NumberQ]] := {rendervalue[a], " == ", rendervalue[b]};
    renderbool[Unequal[a:paramPattern,b_?NumberQ]] := {rendervalue[a], " != ", rendervalue[b]};

    renderbool[Or[a_,b_]] := {"(",renderbool[a]," || ", renderbool[b],")"};
    renderbool[And[a_,b_]] := {"(",renderbool[a]," && ", renderbool[b],")"};
    renderbool[Not[a_]] := {"(!", renderbool[a],")"};
    renderbool[a:paramPattern] := ToString[a/.(Parameter[x_]->x)]; (* Boolean parameter *)

    (* rendervalue[a_String] := a; -- Allow literal pass-through *)
    rendervalue[a_?NumberQ] := ToString[a];
    rendervalue[Parameter[a_String]] := a;
    renderbool[x_] := ThrowError["Unexpected value in run-time conditional expression (boolean):", x, "in", cond];

    unparen[s_] := 
    Module[
      {s2 = FlattenBlock[s],result},
      result = StringReplace[FlattenBlock[s2],StartOfString ~~ "(" ~~ any__ ~~ ")" ~~ EndOfString :> any];
      If[result === s2, result, unparen[result]]];

    ConditionalOnParameterTextual[
      unparen@renderbool[cond],
      {"CCTK_WARN(0, ", StringDrop[Stringify[error],-1], ");\n"}]]];

End[];

EndPackage[];