Skip to content

Commit f604f2e

Browse files
Merge branch 'slides/193-break-135_visibility-into-chapters' into 'master'
Resolve "break 135_visibility into chapters" Closes #193 See merge request feng/training/material!246
2 parents 21e8e44 + 6a4a720 commit f604f2e

File tree

8 files changed

+550
-501
lines changed

8 files changed

+550
-501
lines changed

courses/fundamentals_of_ada/135_visibility.rst

Lines changed: 41 additions & 501 deletions
Large diffs are not rendered by default.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
==============
2+
Introduction
3+
==============
4+
5+
-----------------------
6+
Improving Readability
7+
-----------------------
8+
9+
* Descriptive names plus hierarchical packages makes for very long statements
10+
11+
.. code::
12+
13+
Messages.Queue.Diagnostics.Inject_Fault (
14+
Fault => Messages.Queue.Diagnostics.CRC_Failure,
15+
Position => Messages.Queue.Front);
16+
17+
* Operators treated as functions defeat the purpose of overloading
18+
19+
.. code::
20+
21+
Complex1 := Complex_Types."+" (Complex2, Complex3);
22+
23+
* Ada has mechanisms to simplify hierarchies
24+
25+
--------------------------
26+
Operators and Primitives
27+
--------------------------
28+
29+
* :dfn:`Operators`
30+
31+
- Constructs which behave generally like functions but which differ syntactically or semantically
32+
- Typically arithmetic, comparison, and logical
33+
34+
* **Primitive operation**
35+
36+
- Predefined operations such as ``=`` and ``+`` etc.
37+
- Subprograms declared in the same package as the type and which operate on the type
38+
- Inherited or overridden subprograms
39+
- For :ada:`tagged` types, class-wide subprograms
40+
- Enumeration literals
41+
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
===============
2+
"use" Clauses
3+
===============
4+
5+
---------------
6+
"use" Clauses
7+
---------------
8+
9+
* :ada:`use Pkg;` provides direct visibility into public items in :ada:`Pkg`
10+
11+
+ :dfn:`Direct Visibility` - as if object was referenced from within package being used
12+
+ :dfn:`Public Items` - any entity defined in package spec public section
13+
14+
* May still use expanded name
15+
16+
.. code:: Ada
17+
18+
package Ada.Text_IO is
19+
procedure Put_Line (...);
20+
procedure New_Line (...);
21+
...
22+
end Ada.Text_IO;
23+
24+
with Ada.Text_IO;
25+
procedure Hello is
26+
use Ada.Text_IO;
27+
begin
28+
Put_Line ("Hello World");
29+
New_Line (3);
30+
Ada.Text_IO.Put_Line ("Good bye");
31+
end Hello;
32+
33+
---------------------
34+
"use" Clause Syntax
35+
---------------------
36+
37+
* May have several, like :ada:`with` clauses
38+
* Can refer to any visible package (including nested packages)
39+
* Syntax
40+
41+
.. code:: Ada
42+
43+
use_package_clause ::= use package_name {, package_name};
44+
45+
* Can only :ada:`use` a package
46+
47+
- Subprograms have no contents to :ada:`use`
48+
49+
--------------------
50+
"use" Clause Scope
51+
--------------------
52+
53+
* Applies to end of body, from first occurrence
54+
55+
.. code:: Ada
56+
57+
package Pkg_A is
58+
Constant_A : constant := 123;
59+
end Pkg_A;
60+
61+
package Pkg_B is
62+
Constant_B : constant := 987;
63+
end Pkg_B;
64+
65+
with Pkg_A;
66+
with Pkg_B;
67+
use Pkg_A; -- everything in Pkg_A is now visible
68+
package P is
69+
A : Integer := Constant_A; -- legal
70+
B1 : Integer := Constant_B; -- illegal
71+
use Pkg_B; -- everything in Pkg_B is now visible
72+
B2 : Integer := Constant_B; -- legal
73+
function F return Integer;
74+
end P;
75+
76+
package body P is
77+
-- all of Pkg_A and Pkg_B is visible here
78+
function F return Integer is (Constant_A + Constant_B);
79+
end P;
80+
81+
--------------------
82+
No Meaning Changes
83+
--------------------
84+
85+
* A new :ada:`use` clause won't change a program's meaning!
86+
* Any directly visible names still refer to the original entities
87+
88+
.. code:: Ada
89+
90+
package D is
91+
T : Float;
92+
end D;
93+
94+
with D;
95+
procedure P is
96+
procedure Q is
97+
T, X : Float;
98+
begin
99+
...
100+
declare
101+
use D;
102+
begin
103+
-- With or without the clause, "T" means Q.T
104+
X := T;
105+
end;
106+
...
107+
end Q;
108+
109+
---------------------------
110+
No Ambiguity Introduction
111+
---------------------------
112+
113+
.. code:: Ada
114+
115+
package D is
116+
V : Boolean;
117+
end D;
118+
119+
package E is
120+
V : Integer;
121+
end E;
122+
with D, E;
123+
124+
procedure P is
125+
procedure Q is
126+
use D, E;
127+
begin
128+
-- to use V here, must specify D.V or E.V
129+
...
130+
end Q;
131+
begin
132+
...
133+
134+
.. container:: speakernote
135+
136+
For declarations in different packages that would not be directly visible in the absence of a "use" clause, none with the same identifier will be directly visible in the presence of such a clause, unless both are overloadable (i.e., enumeration literals and subprogram declarations)
137+
138+
------------------------------
139+
"use" Clauses and Child Units
140+
------------------------------
141+
142+
* A clause for a child does **not** imply one for its parent
143+
* A clause for a parent makes the child **directly** visible
144+
145+
- Since children are 'inside' declarative region of parent
146+
147+
.. code:: Ada
148+
149+
package Parent is
150+
P1 : Integer;
151+
end Parent;
152+
153+
package Parent.Child is
154+
PC1 : Integer;
155+
end Parent.Child;
156+
157+
with Parent;
158+
with Parent.Child; use Parent.Child;
159+
procedure Demo is
160+
D1 : Integer := Parent.P1;
161+
D2 : Integer := Parent.Child.PC1;
162+
use Parent;
163+
D3 : Integer := P1; -- illegal
164+
D4 : Integer := PC1;
165+
...
166+
167+
.. container:: speakernote
168+
169+
D4 has access to CHILD because PARENT is "use"d
170+
171+
----------------------------------------
172+
"use" Clause and Implicit Declarations
173+
----------------------------------------
174+
175+
* Visibility rules apply to implicit declarations too
176+
177+
.. code:: Ada
178+
179+
package P is
180+
type Int is range Lower .. Upper;
181+
-- implicit declarations
182+
-- function "+"(Left, Right : Int) return Int;
183+
-- function "="(Left, Right : Int) return Boolean;
184+
end P;
185+
186+
with P;
187+
procedure Test is
188+
A, B, C : P.Int := some_value;
189+
begin
190+
C := A + B; -- illegal reference to operator
191+
C := P."+" (A,B);
192+
declare
193+
use P;
194+
begin
195+
C := A + B; -- now legal
196+
end;
197+
end Test;
198+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
=======================================
2+
"use type" and "use all type" Clauses
3+
=======================================
4+
5+
-------------------------------
6+
"use type" and "use all type"
7+
-------------------------------
8+
9+
* :ada:`use type` makes **primitive operators** directly visible for specified type
10+
11+
- Implicit and explicit operator function declarations
12+
13+
.. code:: Ada
14+
15+
use type subtype_mark {, subtype_mark};
16+
17+
* :ada:`use all type` makes primitive operators **and all other operations** directly visible for specified type
18+
19+
- All **enumerated type values** will also be directly visible
20+
21+
.. code:: Ada
22+
23+
use all type subtype_mark {, subtype_mark};
24+
25+
* More specific alternatives to :ada:`use` clauses
26+
27+
- Especially useful when multiple :ada:`use` clauses introduce ambiguity
28+
29+
--------------
30+
Example Code
31+
--------------
32+
33+
.. code:: Ada
34+
35+
package Types is
36+
type Distance_T is range 0 .. Integer'Last;
37+
38+
-- explicit declaration
39+
-- (we don't want a negative distance)
40+
function "-" (Left, Right : Distance_T)
41+
return Distance_T;
42+
43+
-- implicit declarations (we get the division operator
44+
-- for "free", showing it for completeness)
45+
-- function "/" (Left, Right : Distance_T) return
46+
-- Distance_T;
47+
48+
-- primitive operation
49+
function Min (A, B : Distance_T)
50+
return Distance_T;
51+
52+
end Types;
53+
54+
--------------------------
55+
"use" Clauses Comparison
56+
--------------------------
57+
58+
.. image:: use_clause_comparison.png
59+
60+
-----------------------------
61+
Multiple "use type" Clauses
62+
-----------------------------
63+
64+
* May be necessary
65+
* Only those that mention the type in their profile are made visible
66+
67+
.. code:: Ada
68+
69+
package P is
70+
type T1 is range 1 .. 10;
71+
type T2 is range 1 .. 10;
72+
-- implicit
73+
-- function "+"(Left : T2; Right : T2) return T2;
74+
type T3 is range 1 .. 10;
75+
-- explicit
76+
function "+"(Left : T1; Right : T2) return T3;
77+
end P;
78+
79+
with P;
80+
procedure UseType is
81+
X1 : P.T1;
82+
X2 : P.T2;
83+
X3 : P.T3;
84+
use type P.T1;
85+
begin
86+
X3 := X1 + X2; -- operator visible because it uses T1
87+
X2 := X2 + X2; -- operator not visible
88+
end UseType;
89+

0 commit comments

Comments
 (0)