Skip to content

Commit 403ea7d

Browse files
jensmaurerzygoloid
authored andcommitted
P1703R1 Recognizing header unit imports requires full preprocessing
1 parent fc62568 commit 403ea7d

File tree

4 files changed

+98
-113
lines changed

4 files changed

+98
-113
lines changed

source/basic.tex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -963,8 +963,9 @@
963963
\end{note}
964964

965965
\pnum
966-
\begin{note} For point of instantiation of a template,
967-
see~\ref{temp.point}.\end{note}%
966+
\begin{note}
967+
For point of instantiation of a template, see~\ref{temp.point}.
968+
\end{note}
968969
\indextext{scope!declarations and|)}
969970

970971
\rSec2[basic.scope.block]{Block scope}
@@ -2590,8 +2591,7 @@
25902591

25912592
\pnum
25922593
A token sequence beginning with
2593-
\opt{\tcode{export}} \tcode{module} or
2594-
\opt{\tcode{export}} \tcode{import}
2594+
\opt{\tcode{export}} \tcode{module}
25952595
and not immediately followed by \tcode{::}
25962596
is never interpreted as the \grammarterm{declaration}
25972597
of a \grammarterm{top-level-declaration}.

source/lex.tex

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@
273273
\begin{bnf}
274274
\nontermdef{preprocessing-token}\br
275275
header-name\br
276+
import-keyword\br
276277
identifier\br
277278
pp-number\br
278279
character-literal\br
@@ -338,17 +339,13 @@
338339
except that a \grammarterm{header-name}\iref{lex.header} is only formed
339340
\begin{itemize}
340341
\item
341-
within a \tcode{\#include} directive\iref{cpp.include},
342+
after the \tcode{include} or \tcode{import} preprocessing token in an
343+
\tcode{\#include}\iref{cpp.include} or
344+
\tcode{import}\iref{cpp.import} directive, or
342345

343346
\item
344-
within a \grammarterm{has-include-expression}, or
347+
within a \grammarterm{has-include-expression}.
345348

346-
\item
347-
outside of any preprocessing directive,
348-
if applying phase 4 of translation to the sequence
349-
of preprocessing tokens produced thus far
350-
is valid and
351-
results in an \grammarterm{import-seq}\iref{cpp.module}.
352349
\end{itemize}
353350
\end{itemize}
354351

@@ -359,6 +356,11 @@
359356
\end{codeblock}
360357
\end{example}
361358

359+
\pnum
360+
The \grammarterm{import-keyword} is produced
361+
by processing an \tcode{import} directive\iref{cpp.import} and
362+
has no associated grammar productions.
363+
362364
\pnum
363365
\begin{example} The program fragment \tcode{0xe+foo} is parsed as a
364366
preprocessing number token (one that is not a valid floating or integer

source/modules.tex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,9 @@
411411

412412
\begin{bnf}
413413
\nontermdef{module-import-declaration}\br
414-
\opt{\keyword{export}} \keyword{import} module-name \opt{attribute-specifier-seq} \terminal{;}\br
415-
\opt{\keyword{export}} \keyword{import} module-partition \opt{attribute-specifier-seq} \terminal{;}\br
416-
\opt{\keyword{export}} \keyword{import} header-name \opt{attribute-specifier-seq} \terminal{;}
414+
\opt{\keyword{export}} import-keyword module-name \opt{attribute-specifier-seq} \terminal{;}\br
415+
\opt{\keyword{export}} import-keyword module-partition \opt{attribute-specifier-seq} \terminal{;}\br
416+
\opt{\keyword{export}} import-keyword header-name \opt{attribute-specifier-seq} \terminal{;}
417417
\end{bnf}
418418

419419
\pnum
@@ -478,7 +478,7 @@
478478
a \grammarterm{header-name} is also recognized by the
479479
preprocessor, and results in macros defined at the
480480
end of phase 4 of translation of the header unit
481-
being made visible as described in \ref{cpp.module}.
481+
being made visible as described in \ref{cpp.import}.
482482
\end{note}
483483
A declaration of a name with internal linkage is
484484
permitted within a header unit despite all

source/preprocessor.tex

Lines changed: 80 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
\pnum
1212
A \defn{preprocessing directive} consists of a sequence of preprocessing tokens
1313
that satisfies the following constraints:
14-
The first token in the sequence is a
15-
\tcode{\#}
16-
preprocessing token that (at the start of translation phase 4)
17-
either is the first character in the source file
14+
The first token in the sequence,
15+
referred to as a \defnadj{directive-introducing}{token},
16+
is a \tcode{\#} preprocessing token,
17+
an \tcode{import} preprocessing token, or
18+
an \tcode{export} preprocessing token
19+
immediately followed by an \tcode{import} preprocessing token,
20+
that (at the start of translation phase 4)
21+
either begins with the first character in the source file
1822
(optionally after white space containing no new-line characters)
1923
or follows white space containing at least one new-line character.
2024
The last token in the sequence is the first new-line character
@@ -50,6 +54,7 @@
5054
\begin{bnf}\obeyspaces
5155
\nontermdef{control-line}\br
5256
\terminal{\# include} pp-tokens new-line\br
57+
\opt{\terminal{export}} \terminal{import} pp-tokens new-line\br
5358
\terminal{\# define } identifier replacement-list new-line\br
5459
\terminal{\# define } identifier lparen \opt{identifier-list} \terminal{)} replacement-list new-line\br
5560
\terminal{\# define } identifier lparen \terminal{... )} replacement-list new-line\br
@@ -149,9 +154,8 @@
149154
The only white-space characters that shall appear
150155
between preprocessing tokens
151156
within a preprocessing directive
152-
(from just after the introducing
153-
\tcode{\#}
154-
preprocessing token through just before the terminating new-line character)
157+
(from just after the directive-introducing token
158+
through just before the terminating new-line character)
155159
are space and horizontal-tab
156160
(including spaces that have replaced comments
157161
or possibly other white-space characters
@@ -243,7 +247,7 @@
243247
if the identifier is currently defined
244248
as a macro name
245249
(that is, if it is predefined
246-
or if it has one or more active macro definitions\iref{cpp.module},
250+
or if it has one or more active macro definitions\iref{cpp.import},
247251
for example because
248252
it has been the subject of a
249253
\tcode{\#define}
@@ -623,11 +627,11 @@
623627
denotes an importable header\iref{module.import},
624628
it is
625629
\impldef{whether source file inclusion of importable header
626-
is replaced with \tcode{import}}
627-
whether the preprocessing directive
628-
is instead replaced by the \grammarterm{preprocessing-token}{s}
630+
is replaced with \tcode{import} directive}
631+
whether the \tcode{\#include} preprocessing directive
632+
is instead replaced by an \tcode{import} directive\iref{cpp.import} of the form
629633
\begin{ncbnf}
630-
\terminal{import} header-name \terminal{;}
634+
\terminal{import} header-name \terminal{;} new-line
631635
\end{ncbnf}
632636

633637
\pnum
@@ -665,104 +669,39 @@
665669
\end{codeblock}
666670
\end{example}
667671

668-
\rSec1[cpp.glob.frag]{Global module fragment}
669-
670-
\begin{bnf}
671-
\nontermdef{pp-global-module-fragment}\br
672-
\terminal{module} \terminal{;} pp-balanced-token-seq \terminal{module}
673-
\end{bnf}
674-
675-
\pnum
676-
If the first two preprocessing tokens at the start of phase 4 of translation
677-
are \tcode{module} \tcode{;},
678-
the result of preprocessing shall begin with
679-
a \grammarterm{pp-global-module-fragment}
680-
for which all \grammarterm{preprocessing-token}{s}
681-
in the \grammarterm{pp-balanced-token-seq}
682-
were produced directly or indirectly by source file inclusion\iref{cpp.include},
683-
and for which the second \tcode{module} \grammarterm{preprocessing-token}
684-
was not produced by source file inclusion or
685-
macro replacement\iref{cpp.replace}.
686-
Otherwise,
687-
the first two preprocessing tokens at the end of phase 4 of translation
688-
shall not be \tcode{module} \tcode{;}.
689-
690-
\rSec1[cpp.module]{Header units}
672+
\rSec1[cpp.import]{Header unit importation}
691673
\indextext{header unit!preprocessing}%
692674
\indextext{macro import|(}%
693675

694-
\begin{bnf}
695-
\nontermdef{import-seq}\br
696-
\opt{top-level-token-seq} \opt{\terminal{export}} \terminal{import}
697-
\end{bnf}
698-
699-
\begin{bnf}
700-
\nontermdef{top-level-token-seq}\br
701-
\descr{any \grammarterm{pp-balanced-token-seq} ending in \terminal{;} or \terminal{\}}}
702-
\end{bnf}
703-
704676
\begin{bnf}
705677
\nontermdef{pp-import}\br
706-
\terminal{import} header-name \opt{pp-import-suffix} \terminal{;}\br
707-
\terminal{import} header-name-tokens \opt{pp-import-suffix} \terminal{;}
708-
\end{bnf}
709-
710-
\begin{bnf}
711-
\nontermdef{pp-import-suffix}\br
712-
pp-import-suffix-token\br
713-
pp-import-suffix pp-import-suffix-token
714-
\end{bnf}
715-
716-
\begin{bnf}
717-
\nontermdef{pp-import-suffix-token}\br
718-
\descr{any \grammarterm{pp-balanced-token} other than \terminal{;}}
719-
\end{bnf}
720-
721-
\begin{bnf}
722-
\nontermdef{pp-balanced-token-seq}\br
723-
pp-balanced-token\br
724-
pp-balanced-token-seq pp-balanced-token
725-
\end{bnf}
726-
727-
\begin{bnf}
728-
\nontermdef{pp-balanced-token}\br
729-
pp-ldelim \opt{pp-balanced-token-seq} pp-rdelim\br
730-
\descr{any \grammarterm{preprocessing-token} other than a \grammarterm{pp-ldelim} or \grammarterm{pp-rdelim}}
731-
\end{bnf}
732-
733-
\begin{bnf}
734-
\nontermdef{pp-ldelim} \descr{one of}\br
735-
\terminal{( [ \{ <: <\%}
736-
\end{bnf}
737-
738-
\begin{bnf}
739-
\nontermdef{pp-rdelim} \descr{one of}\br
740-
\terminal{) ] \} :> \%>}
678+
\opt{\terminal{export}} \terminal{import} header-name \opt{pp-tokens} \terminal{;} new-line\br
679+
\opt{\terminal{export}} \terminal{import} header-name-tokens \opt{pp-tokens} \terminal{;} new-line\br
680+
\opt{\terminal{export}} \terminal{import} pp-tokens \terminal{;} new-line
741681
\end{bnf}
742682

743683
\pnum
744-
A sequence of \grammarterm{preprocessing-token}{s}
745-
matching the form of a \grammarterm{pp-import}
684+
The preprocessing tokens after the \tcode{import} preprocessing token
685+
in the \tcode{import} \grammarterm{control-line}
686+
are processed just as in normal text
687+
(i.e., each identifier currently defined as a macro name
688+
is replaced by its replacement list of preprocessing tokens).
689+
An \tcode{import} directive
690+
matching the first two forms of a \grammarterm{pp-import}
746691
instructs the preprocessor to import macros
747692
from the header unit\iref{module.import}
748693
denoted by the \grammarterm{header-name}.
749-
A \grammarterm{pp-import} is only recognized
750-
when the sequence of tokens
751-
produced by phase 4 of translation
752-
up to the \tcode{import} token
753-
forms an \grammarterm{import-seq},
754-
and the \tcode{import} token is not within
755-
the \grammarterm{header-name-tokens} or \grammarterm{pp-import-suffix}
756-
of another \grammarterm{pp-import}.
757-
The \tcode{;} \grammarterm{preprocessing-token}
758-
terminating a \grammarterm{pp-import}
759-
shall not have been produced by
760-
macro replacement\iref{cpp.replace}.
761-
The \defnadj{point of}{macro import} for a \grammarterm{pp-import} is
762-
immediately after the \tcode{;} terminating the \grammarterm{pp-import}.
694+
The \defnadj{point of}{macro import} for the
695+
first two forms of \grammarterm{pp-import} is
696+
immediately after the \grammarterm{new-line} terminating
697+
the \grammarterm{pp-import}.
698+
The last form of \grammarterm{pp-import} is only considered
699+
if the first two forms did not match.
763700

764701
\pnum
765-
In the second form of \grammarterm{pp-import},
702+
In all three forms of \grammarterm{pp-import},
703+
the \tcode{import} token is replaced by the \grammarterm{import-keyword} token.
704+
Additionally, in the second form of \grammarterm{pp-import},
766705
a \grammarterm{header-name} token is formed as if
767706
the \grammarterm{header-name-tokens}
768707
were the \grammarterm{pp-tokens} of a \tcode{\#include} directive.
@@ -847,6 +786,50 @@
847786

848787
\indextext{macro import|)}%
849788

789+
\rSec1[cpp.glob.frag]{Global module fragment}
790+
791+
\begin{bnf}
792+
\nontermdef{pp-global-module-fragment}\br
793+
\terminal{module} \terminal{;} pp-balanced-token-seq \terminal{module}
794+
\end{bnf}
795+
\begin{bnf}
796+
797+
\nontermdef{pp-balanced-token-seq}\br
798+
pp-balanced-token\br
799+
pp-balanced-token-seq pp-balanced-token
800+
\end{bnf}
801+
802+
\begin{bnf}
803+
\nontermdef{pp-balanced-token}\br
804+
pp-ldelim \opt{pp-balanced-token-seq} pp-rdelim\br
805+
\descr{any \grammarterm{preprocessing-token} other than a \grammarterm{pp-ldelim} or \grammarterm{pp-rdelim}}
806+
\end{bnf}
807+
808+
\begin{bnf}
809+
\nontermdef{pp-ldelim} \descr{one of}\br
810+
\terminal{( [ \{ <: <\%}
811+
\end{bnf}
812+
813+
\begin{bnf}
814+
\nontermdef{pp-rdelim} \descr{one of}\br
815+
\terminal{) ] \} :> \%>}
816+
\end{bnf}
817+
818+
\pnum
819+
If the first two preprocessing tokens at the start of phase 4 of translation
820+
are \tcode{module} \tcode{;},
821+
the result of preprocessing shall begin with
822+
a \grammarterm{pp-global-module-fragment}
823+
for which all \grammarterm{preprocessing-token}{s}
824+
in the \grammarterm{pp-balanced-token-seq}
825+
were produced directly or indirectly by source file inclusion\iref{cpp.include},
826+
and for which the second \tcode{module} \grammarterm{preprocessing-token}
827+
was not produced by source file inclusion or
828+
macro replacement\iref{cpp.replace}.
829+
Otherwise,
830+
the first two preprocessing tokens at the end of phase 4 of translation
831+
shall not be \tcode{module} \tcode{;}.
832+
850833
\rSec1[cpp.replace]{Macro replacement}%
851834
\indextext{macro!replacement|(}%
852835
\indextext{replacement!macro|see{macro, replacement}}%

0 commit comments

Comments
 (0)