Skip to content

Commit 3a76880

Browse files
authored
Merge pull request #2062 from mccode-dev/COPY_to_INHERIT_in_comps
Awaiting reviews. Grammar change, use INHERIT instead of COPY in comps
2 parents 28d4797 + 1875d2b commit 3a76880

19 files changed

Lines changed: 273 additions & 94 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# New COMPONENT keyword 'INHERIT'
2+
3+
## Status
4+
5+
*Proposed* and prototype *implemented*
6+
7+
## Context
8+
9+
As discussed in https://github.com/mccode-dev/McCode/discussions/2041,
10+
a recent addition of a 'multi-COPY for flexible mix and match of
11+
COMPONENT sections lead to different syntax / meaning of the COPY
12+
keyword in:
13+
* instruments: `COMPONENT COPY(instance) = COPY(instance)(par1=val1, par2=val2)`
14+
* component sections: `SHARE COPY PowderN COPY Fluorescence`
15+
16+
## Decision
17+
18+
In a zoom meeting on June 11th 2025 it was agreed between @farhi,
19+
@g5t, @mads-bertelsen, @willend to
20+
* Keep the `COPY` syntax in instrument files as is
21+
* Stop supporting `COPY` in components, and define a new `INHERIT` keyword for component section copying (mix
22+
and match as used in e.g. McXtrace`FluoPowder`)
23+
* Prototype implemented and ready for review in PR https://github.com/mccode-dev/McCode/pull/2062
24+
25+
26+
## Consequences
27+
28+
* Syntax becomes clearer - either `COPY(reference)` with parenthesis in
29+
instruments or `INHERIT reference` in components.
30+
* Existing components with use of `COPY` must be migrated (already
31+
done for repo comps in PR.
32+
* User home-grown McStas or McXtrace components will break and need
33+
migration to `INHERIT`. It is believed that very few - if any - such
34+
components exist in the wild.
35+
* Important: `INHERIT` does not ensure that component setting
36+
parameters used in copied section(s) are copied too. (This to allow
37+
complete modification of the parameter interface for a "derived" component.)
38+
* CHANGELOG should be crystal clear on this change / potential incompatibility
39+
40+
## Behaviour
41+
42+
For trivial components `n_part.comp`:
43+
44+
```
45+
DEFINE COMPONENT n_part
46+
SETTING PARAMETERS (int n)
47+
TRACE
48+
%{
49+
for (int i= 0; i < n; i++) {
50+
printf("n=%d\\n", i);
51+
}
52+
%}
53+
END
54+
```
55+
and `m_part.comp`:
56+
57+
```
58+
DEFINE COMPONENT m_part
59+
SETTING PARAMETERS (int m)
60+
TRACE
61+
%{
62+
for (int i = 0; i < m; i++) {
63+
printf("m=%d\\n", i);
64+
}
65+
%}
66+
END
67+
```
68+
a third component can now be defined pulling together sections from each, `both_parts.comp`:
69+
70+
```
71+
DEFINE COMPONENT both_parts
72+
SETTING PARAMETERS (int n, int m)
73+
TRACE INHERIT n_part INHERIT m_part
74+
END
75+
```
76+
Using the third component in a trivial instrument, e.g., `test_both_parts.instr`:
77+
78+
```
79+
define instrument test_both_parts(dummy=0.)
80+
trace
81+
component origin = both_parts(n=3, m=2) at (0, 0, 0) absolute
82+
end
83+
```
84+
will produce runtime output
85+
86+
```
87+
$ mcrun test_both_parts -n 1 dummy=0
88+
n=0
89+
n=1
90+
n=2
91+
m=0
92+
m=1
93+
```

doc/GRAMMAR/ADR-records/template.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ What is the change that we're proposing and/or doing?
2222
## Consequences
2323

2424
What becomes easier or more difficult to do because of this change?
25+
26+
## Behaviour
27+
28+
Describe important aspects of the new/changed behaviour

mccode/src/instrument.l

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ WHEN return TOK_WHEN; /* extended McStas grammar */
145145
NEXT return TOK_NEXT; /* extended McStas grammar */
146146
ITERATE return TOK_ITERATE; /* extended McStas grammar */
147147
MYSELF return TOK_MYSELF; /* extended McStas grammar */
148-
COPY return TOK_COPY; /* extended McStas grammar */
148+
INHERIT return TOK_INHERIT; /* extended McStas COMP grammar */
149+
COPY return TOK_COPY; /* extended McStas INSTR grammar */
149150
SPLIT return TOK_SPLIT; /* extended McStas grammar */
150151
REMOVABLE return TOK_REMOVABLE; /* extended McStas grammar with include */
151152
CPU return TOK_CPUONLY; /* extended McStas grammar with GPU-CPU support */

0 commit comments

Comments
 (0)