libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / m2 / mc / mcComment.mod
blobf26b207b75c8f9d1ebd66b8829e70bc855389048
1 (* mcComment.mod provides a module to remember the comments.
3 Copyright (C) 2015-2024 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
22 IMPLEMENTATION MODULE mcComment ; (*!m2pim*)
24 FROM DynamicStrings IMPORT String, InitString, ConCat, RemoveWhitePrefix, Mark, KillString, InitStringCharStar, EqualCharStar, Length, Slice, string, char ;
25 FROM Storage IMPORT ALLOCATE ;
26 FROM nameKey IMPORT Name, keyToCharStar, lengthKey, NulName ;
27 FROM mcDebug IMPORT assert ;
28 FROM ASCII IMPORT nl ;
29 FROM libc IMPORT printf ;
32 TYPE
33 commentType = (unknown, procedureHeading, inBody, afterStatement) ;
35 commentDesc = POINTER TO RECORD
36 type : commentType ;
37 content : String ;
38 procName: Name ;
39 used : BOOLEAN ;
40 END ;
45 isProcedureComment - returns TRUE if, cd, is a procedure comment.
48 PROCEDURE isProcedureComment (cd: commentDesc) : BOOLEAN ;
49 BEGIN
50 RETURN (cd # NIL) AND (cd^.type = procedureHeading)
51 END isProcedureComment;
55 isBodyComment - returns TRUE if, cd, is a body comment.
58 PROCEDURE isBodyComment (cd: commentDesc) : BOOLEAN ;
59 BEGIN
60 RETURN (cd # NIL) AND (cd^.type = inBody)
61 END isBodyComment;
65 isAfterComment - returns TRUE if, cd, is an after comment.
68 PROCEDURE isAfterComment (cd: commentDesc) : BOOLEAN ;
69 BEGIN
70 RETURN (cd # NIL) AND (cd^.type = afterStatement)
71 END isAfterComment;
75 initComment - the start of a new comment has been seen by the lexical analyser.
76 A new comment block is created and all addText contents are placed
77 in this block. onlySpaces indicates whether we have only seen
78 spaces on this line.
81 PROCEDURE initComment (onlySpaces: BOOLEAN) : commentDesc ;
82 VAR
83 cd: commentDesc ;
84 BEGIN
85 NEW (cd) ;
86 assert (cd # NIL) ;
87 WITH cd^ DO
88 IF onlySpaces
89 THEN
90 type := inBody
91 ELSE
92 type := afterStatement
93 END ;
94 content := InitString ('') ;
95 procName := NulName ;
96 used := FALSE
97 END ;
98 RETURN cd
99 END initComment ;
103 addText - cs is a C string (null terminated) which contains comment text.
104 This is appended to the comment, cd.
107 PROCEDURE addText (cd: commentDesc; cs: ADDRESS) ;
108 BEGIN
109 IF cd # NIL
110 THEN
111 cd^.content := ConCat (cd^.content, InitStringCharStar (cs))
113 END addText ;
117 Min - returns the lower of, a, and, b.
120 PROCEDURE Min (a, b: CARDINAL) : CARDINAL ;
121 BEGIN
122 IF a < b
123 THEN
124 RETURN a
125 ELSE
126 RETURN b
128 END Min ;
132 RemoveNewlines -
135 PROCEDURE RemoveNewlines (s: String) : String ;
136 BEGIN
137 WHILE Length (s) > 0 DO
138 IF char (s, 0) = nl
139 THEN
140 s := RemoveWhitePrefix (Slice (s, 1, 0))
141 ELSE
142 RETURN RemoveWhitePrefix (s)
144 END ;
145 RETURN s
146 END RemoveNewlines ;
150 seenProcedure - returns TRUE if the name, procName, appears as the first word
151 in the comment.
154 PROCEDURE seenProcedure (cd: commentDesc; procName: Name) : BOOLEAN ;
156 s : String ;
157 a : ADDRESS ;
158 i, h: CARDINAL ;
159 res : BOOLEAN ;
160 BEGIN
161 a := keyToCharStar (procName) ;
162 s := RemoveNewlines (cd^.content) ;
163 s := Slice (Mark (s), 0, Min (Length (s), lengthKey (procName))) ;
164 res := EqualCharStar (s, a) ;
165 s := KillString (s) ;
166 RETURN res
167 END seenProcedure ;
171 setProcedureComment - changes the type of comment, cd, to a
172 procedure heading comment,
173 providing it has the procname as the first word.
176 PROCEDURE setProcedureComment (cd: commentDesc; procname: Name) ;
177 BEGIN
178 IF cd # NIL
179 THEN
180 IF seenProcedure (cd, procname)
181 THEN
182 cd^.type := procedureHeading ;
183 cd^.procName := procname
186 END setProcedureComment ;
190 getContent - returns the content of comment, cd.
193 PROCEDURE getContent (cd: commentDesc) : String ;
194 BEGIN
195 IF cd # NIL
196 THEN
197 RETURN cd^.content
198 END ;
199 RETURN NIL
200 END getContent ;
204 getCommentCharStar - returns the C string content of comment, cd.
207 PROCEDURE getCommentCharStar (cd: commentDesc) : ADDRESS ;
209 s: String ;
210 BEGIN
211 s := getContent (cd) ;
212 IF s = NIL
213 THEN
214 RETURN NIL
215 ELSE
216 RETURN string (s)
218 END getCommentCharStar ;
222 getProcedureComment - returns the current procedure comment if available.
225 PROCEDURE getProcedureComment (cd: commentDesc) : String ;
226 BEGIN
227 IF (cd^.type = procedureHeading) AND (NOT cd^.used)
228 THEN
229 cd^.used := TRUE ;
230 RETURN cd^.content
231 END ;
232 RETURN NIL
233 END getProcedureComment ;
237 getAfterStatementComment - returns the current statement after comment if available.
240 PROCEDURE getAfterStatementComment (cd: commentDesc) : String ;
241 BEGIN
242 IF (cd^.type = afterStatement) AND (NOT cd^.used)
243 THEN
244 cd^.used := TRUE ;
245 RETURN cd^.content
246 END ;
247 RETURN NIL
248 END getAfterStatementComment ;
252 getInbodyStatementComment - returns the current statement after comment if available.
255 PROCEDURE getInbodyStatementComment (cd: commentDesc) : String ;
256 BEGIN
257 IF (cd^.type = inBody) AND (NOT cd^.used)
258 THEN
259 cd^.used := TRUE ;
260 RETURN cd^.content
261 END ;
262 RETURN NIL
263 END getInbodyStatementComment ;
267 dumpComment -
270 PROCEDURE dumpComment (cd: commentDesc) ;
271 BEGIN
272 printf ("comment : ");
273 WITH cd^ DO
274 CASE type OF
276 unknown : printf ("unknown") |
277 procedureHeading: printf ("procedureheading") |
278 inBody : printf ("inbody") |
279 afterStatement : printf ("afterstatement")
281 END ;
282 IF used
283 THEN
284 printf (" used")
285 ELSE
286 printf (" unused")
287 END ;
288 printf (" contents = %s\n", string (content))
290 END dumpComment ;
293 END mcComment.