Commit af6aee41 authored by Matthias Simon's avatar Matthias Simon
Browse files

Update function and parameterization

parent e0b01b08
Loading
Loading
Loading
Loading
Loading
+0 −451
Original line number Diff line number Diff line
@@ -9,457 +9,6 @@ in a module.
Platform specific behaviour may be provided by the means of [external
functions].


## Parameterization

### General

Parameterization may be used to specify dynamic behaviour -- or [dynamic
templates](15.2).

TTCN-3 distinguishes between _value parameterization_ as specified in the
following chapters and _type parameterization_ as specified in [advanced
parameterization extension].

> NOTE: technically there is also indirect parameterization through the use of
> references. But it is strongly recommended to favor the more explicit _value
> parameterization_ over _indirect parameterization through references_ when
> possible; because the latter can introduce subtle inconsistencies between
> individual tools and edge-cases.

> NOTE: The term _parameterization_ will be used interchangeably with _value
> parameterization_ in this document, unless stated differently.

TTCN-3 language elements that support value parameterization are: [functions],
[external functions], [testcases], [altsteps] and [parameterized templates].


**Examples**

```ttcn3 example="type parameterization"
function f<type T>() { /* ... */ }
control {
    f<integer>(); // function called with type parameter `integer`
    f<boolean>(); // function called with type parameter `boolean`
}
```

```ttcn3 example="indirect parameterization"
modulepar integer mpar;
external function timestamp() return integer;
function f() {
    // each call of f will have its own copy of integers referenced
    // by module parameter `mpar` and external function `timestamp`.
    var integer v := mpar + timestamp();
    log(v);
}
control {
    f();
    f();
}
```

```ttcn3 example="value parameterization"
modulepar integer mpar;
external function timestamp() return integer;
function f(in integer p) {
    // each call of f will have its own copy of integers referenced
    // by module parameter `mpar` and external function `timestamp`.
    log(p);
}
control {
    f(mpar+timestamp());
    f(mpar+timestamp());
}
```

### Formal parameters

A _formal parameter list_ specifies how a TTCN-3 language element is
parameterized, which parameters are accepted and how parameters are accessed.

**Syntactical Description**

```ebnf
FormalPars ::= "(" {FormalPar ","}, ")".
FormalPar ::= [ "in" | "out" | "inout" ] TypeExpr name {"[" integer "]"} ["..."] [ ":=" Expr].
```

**Semantic description**

A _formal parameter_ declaration adds a new parameter with given _name_ into
the scope of the parameterized language element (see [uniqueness of
identifiers]).

A _formal parameter_ has a static type associated with it.
The type of the parameter shall be specified using a _[type expression]_
and shall not change during its [lifetime].

A _formal parameter_ may be declared as array by using the [array short-hand
notation].

A _formal parameter_ shall not reference any formal parameters defined in the
same _formal parameter list_.

> NOTE: Referencing type-parameters, visible components (e.g. `mtc`, `self`, ...)
> or variables of the runs-on component is permitted.

A _formal parameter_ is either an _in-parameter_, indicated by the `in`
keyword, an _inout-parameter_, indicated by the `inout` keyword, or
_out-parameter_, indicated by the `out` keyword.

A _formal parameter_ without any of these keywords is considered an
_in-parameter_.

A _formal parameter_ of kind `in` may have an _initial value assignment_.
Such parameter is called _default parameter_ and is described in [default
parameters](#default-parameters).

A _formal parameter_ shall behave like a variable in respect to assignment,
type compatibility, evaluation rules and memory (i.e. each parameter has its
distinct copy of a value, see following note)

> NOTE: The term _value_ means all representable data in TTCN-3 and includes
> references, too. The referenced object may be unique, but a copy reference
> itself is stored in the parameter's memory location.

Access to parameters passed by reference (i.e. `inout`) shall dereference implicitly.

A _formal parameter_ gets assigned its value when the parameterized TTCN-3
language element is passed an [actual parameter], i.e. when a function is invoked,
a template is instantiated, etc.

**Restrictions**

1. Formal parameters for templates shall be `in`-parameters only.
2. Formal parameters for altsteps shall be `in`-parameters only.
3. String type parameters shall not be `inout` parameters.


**Examples**

```ttcn3 example="scope"
function f(integer p) {
    log(p) // p is in the scope of function f
}
```

```ttcn3 example="array"
function f(integer a[3]) {
    // expect: typeof(a) == "array of integer"
}
```

```ttcn3 example="shall not reference other parameters in the same scope"
external function f(integer n, integer a[n]); // error
external function g(integer n, integer a := n*2); // error
external function h(integer n, record length(n) of integer a); // error
```

```ttcn3 example="only in-parameters may have a default value"
external function f(in integer p := 1); // okay
external function g(inout integer p := 1); // error
external function h(out integer p := 1); // error
```

```ttcn3 example="in only for templates"
template integer t1(inout integer p) := p // error
template integer t2(out integer p) := p // error
```

```ttcn3 example="in only for altsteps"
altstep as1(inout integer p) {} // error
altstep as2(out integer p) {} // error
```

```ttcn3 example="automatic dereferencing"
function inc(inout integer p) { p := p + 1 }
control {
    var integer i := 1;
    inc(i); // expect: i == 2
}
```

### Actual parameters

An _actual parameter list_ supplies parameters to an invocation or
instantiation of a parameterized TTCN-3 language element. 

**Syntactical Description**

```ebnf
ActualParList ::= "(" [ ActualPar {"," ActualPar} {"," ActualParAssignment}
                      | ActualParAssignment {"," ActualParAssignment} ] ")".
ActualPar ::= Expr | Expr "..." | "-".
ActualParAssignment ::= name ":=" Expr.
```

- notations
  - list notation
  - assignment notation
  - mixed notation


**Semantic description**

- To invoke a parameterized TTCN-3 language elements, its actual parameters are evaluated and assigned to the formal parameters.
- for functions: then control is transfered
- for templates: see section 15 (parameterized templates)
- for altsteps, testcases, ...: ?

- actual parameters are evaluated in order of their appearance
- evaluated in the scope of the caller
- assignment notation: each parameter shall be specified only once.
- parameter kinds
    - in:
      - _pass by value_ (copy, type compatibility)
      - optional default value (reference-types == null)
      - recommended: no-side effects == constant evaluation
      - expand values according to right-hand-side-semantics ([assignment])
    - out:
      - _pass by value_ (copy, type compatibility)
      - copy back when changed
      - expand values according to left-hand-side-semantics ([assignment])
    - inout:
      - _pass by reference_ (type equality required, strong typing)
      - expand values according to left-hand-side-semantics ([assignment])

**Restrictions**

- Passing references of fields or elements and references to the container in the same actual parameter list is forbidden; to prevent aliasing related issues.
- values that that cannot be used on the left-hand-side of an assignment, must also be `in
- empty parameter list may be omitted for parameterized object that are not functions, testcase, altstep, external functions

> TODO: Check 5.4.2.m aliasing (of structs and unions) in the same function is forbidden (can we actually enforce that?)

**Examples**

```ttcn3 example="each parameter only once"
external function f(in integer p1, in integer p2 := 99);
control {
    f(1, p1:= 2); // error
}
```

```ttcn3 example="non left-hand-side value"
external function f(inout integer p1);
control {
    f(bit2int('1100101')); // error: reference to temporary value
}
```

### Default parameters

A _default parameter_ is an `in` formal value or template parameter whose
declaration includes a default value or default template assignment.

**Syntactical Description**

A default parameter is a `FormalPar` whose `DefaultAssignment` is present in
the shared [formal-parameter syntax](#formal-parameters). `:= Expr` declares a
default value and `:= TemplateInstance` declares a default template. The
assignment is part of the formal parameter declaration and is not an actual
parameter assignment. `TemplateType` includes any template restriction
declared for the formal template parameter.

**Semantic description**

The declared default value or template shall be used when no actual parameter
maps to the default parameter. In list notation, `-` shall also select the
declared default for the formal parameter at that position. In assignment
notation, the default shall be selected by omitting an assignment for that
formal parameter; `-` shall not be used as the right-hand side of an actual
parameter assignment.

In list notation, a default parameter may be omitted without `-` only when no
actual parameter follows it. Thus, omission without a marker applies to a
trailing sequence of default, `out`, or variadic formal parameters. Assignment
notation may omit any default parameter by name irrespective of its position.

Explicit actual parameter expressions shall first be evaluated in their
textual order as specified in [actual parameters](#actual-parameters). The
default expressions and template instances selected by omission or `-` shall
then be evaluated in formal-parameter order. A default expression or template
instance shall be evaluated in the scope of the parameterized entity, not in
the caller's scope.

The resulting default value or template shall be assigned to distinct storage
for the formal parameter using the same compatibility and assignment rules as
an explicit actual value or template for an `in` parameter.

For a [variadic parameter](#variadic-parameters) with a declared default,
omission or `-` shall select that default. Without a declared default, omission
or `-` shall produce the empty `record of` value or template specified by the
variadic-parameter rules.

**Restrictions**

1. Only an `in` formal parameter may declare a default value or template.
2. A default expression or template instance shall be compatible with the
   formal parameter's type. A default template shall also satisfy the template
   restriction of its formal parameter.
3. A default expression or template instance shall be well-defined at entry to
   the scope of the parameterized entity and shall not refer to another formal
   parameter in the same formal parameter list.
4. A default value of component type shall be `null`, `mtc`, `self`, or
   `system`. A default template for component type shall be built from these
   special values.
5. A default value of port, timer, or `default` type shall be `null`.
6. Functions used while evaluating a default value or template should avoid
   side effects.

**Examples**

```
f(1, -) // the same
f(1)
```

```ttcn3 example="default omission and dash marker"
function report(integer value, integer base := 10, integer width := 2) {
    log(value, base, width);
}

control {
    report(7);                   // base == 10, width == 2
    report(7, -, 4);             // base == 10, width == 4
    report(value := 7, width := 4); // base == 10
}
```

```ttcn3 example="default template omission"
function matches(template integer expected := ?) {
    // ...
}

control {
    matches();  // expected uses the default template ?
    matches(-); // expected uses the default template ?
}
```

```ttcn3 example="default evaluation order and scope"
const integer fallback := 1;

external function mark(charstring event) return integer;

function observe(integer first := mark("first"),
                 integer second := mark("second"),
                 integer third := fallback) {
    log(first, second);
}

control {
    const integer fallback := 99;
    observe(second := mark("explicit"));
    // mark is called for "explicit" first and "first" second.
    // third uses the module-scope fallback value 1, not the caller's 99.
}
```

```ttcn3 example="invalid default declarations and omissions"
function badOut(out integer p := 1) {}       // error: only in may have a default
function badInout(inout integer p := 1) {}   // error: only in may have a default
function badType(integer p := "one") {}      // error: incompatible default
function badReference(integer p, integer q := p) {} // error: same-list reference
function badTemplate(template(value) integer p := ?) {} // error: ? violates value
function required(integer p, integer q := 2) {}

control {
    required(-, 3); // error: p has no default
    required(q := 3); // error: required p is omitted
}
```

### Variadic parameters

The last formal parameter of a _formal parameter list_ may have the suffix `...`
(three dots). Such formal parameter is called variadic parameter and accepts
zero or more values of the specified type.

The _variadic formal parameter_ shall be an _in-parameter_.

A _variadic formal parameter_ may declare array-types using the array-notation.

A _variadic formal parameter_ may have an _default value assignment_.

Inside the scope of parameterized language element, the type of the formal
variadic parameter shall be a `record of` of the specified type.

There are three ways of passing an actual parameter to the formal variadic
parameter when using the list notation:

- The actual parameter is not present in the list of actual parameters or
  skipped by using the dash symbol. If the variadic parameter has a declared
  default, that default is used; otherwise, the actual parameter is equal to an
  empty `record of` value or template.
- Using comma-separated values of the specified type as the actual
  parameter. The actual variadic parameter is equal to a `record of` value
  that contains all the comma-separated values in their passing order.
- Using a single parameter of a compatible `record of` the specified type
  followed by `...`. The actual parameter is passed directly to the formal
  variadic parameter in this case. When using the assignment notation for an
  actual variadic parameter, the actual parameter shall be a value or template
  of a compatible record of the specified type without any additional ellipsis.
  The actual parameter is passed directly to the formal variadic parameter in
  this case.


**Examples**

```ttcn3 example="invalid position"
external function f(integer args..., integer n); // error
external function g(integer args1..., integer args2...); // error 
```

```ttcn3 example="in parameters only"
external function f(inout integer args...); // error
external function g(out integer args...); // error
```

```ttcn3 example="array notation"
function f(integer args[3]...) {
    // expect: typeof(args) == "record of record length(3) of integer"
}
```

```ttcn3 example="passing variadic parameters"
control {
    f(1,2,3); //

    var record of integer a := {1,2,3}
    f(a...);
    f(args := a);
}

function f(integer args...) {
    // expect: args == {1,2,3}
}
```

```ttcn3 example="pass empty argument"
control {
    f();
    f(-);
    f(args := {});
}

function f(integer args...) {
    // expect: lengthof(args) == 0
}
```

```ttcn3 example="default parameter"
control {
    f();
    f(-);
}

function f(integer args... := {1,2,3}) {
    // expect: args == {1,2,3}
}
```

## Invoking functions

A function is invoked by referring to the function and supplying its [actual
+441 −0

File added.

Preview size limit exceeded, changes collapsed.