1 (* M2Const.mod maintain and resolve the types of constants.
3 Copyright (C) 2010-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)
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 M2Const ;
30 constList = POINTER TO cList ;
33 constmeta: constType ;
41 headOfConsts: constList ;
44 PROCEDURE stop ; BEGIN END stop ;
48 addToConstList - add a constant, sym, to the head of the constants list.
51 PROCEDURE addToConstList (sym: CARDINAL) ;
59 InternalError ('should never see the same symbol id declared twice')
66 constmeta := unknown ;
76 FixupConstAsString - fixes up a constant, sym, which will have the string type.
79 PROCEDURE FixupConstAsString (sym: CARDINAL) ;
81 fixupConstMeta(sym, str)
82 END FixupConstAsString ;
86 FixupConstType - fixes up a constant, sym, which will have the type, consttype.
89 PROCEDURE FixupConstType (sym: CARDINAL; consttype: CARDINAL) ;
100 InternalError ('cannot fix up a constant to have a type if it is already known as a string')
103 PutConst(sym, consttype) ;
113 FixupProcedureType - creates a proctype from a procedure.
116 PROCEDURE FixupProcedureType (p: CARDINAL) : CARDINAL ;
124 t := MakeProcType(CheckAnonymous(NulName)) ;
128 par := GetParam(p, i) ;
129 IF IsParameterVar(par)
131 PutProcTypeVarParam(t, GetType(par), IsParameterUnbounded(par))
133 PutProcTypeParam(t, GetType(par), IsParameterUnbounded(par))
139 PutFunction(t, GetType(p))
143 InternalError ('expecting a procedure')
146 END FixupProcedureType ;
150 FixupConstProcedure - fixes up a constant, sym, which will be equivalent to e.
153 PROCEDURE FixupConstProcedure (sym: CARDINAL; e: CARDINAL) ;
163 type := FixupProcedureType(e) ;
164 PutConst(sym, type) ;
170 END FixupConstProcedure ;
174 FixupConstExpr - fixes up a constant, sym, which will be equivalent to e.
177 PROCEDURE FixupConstExpr (sym: CARDINAL; e: CARDINAL) ;
196 fixupConstMeta - fixes up symbol, sym, to have the, meta, constType.
199 PROCEDURE FixupConstMeta (sym: CARDINAL; meta: constType) ;
221 PROCEDURE fixupConstCast (sym: CARDINAL; castType: CARDINAL) ;
243 PROCEDURE findConstType (sym: CARDINAL) : CARDINAL ;
272 PROCEDURE findConstMeta (sym: CARDINAL) : constType ;
291 ReportUnresolvedConstTypes - emits an error message for any unresolved constant type.
294 PROCEDURE ReportUnresolvedConstTypes ;
301 IF (constmeta#unknown) AND (constmeta#str) AND (type=NulSym)
303 MetaError1('unable to resolve the type of the constant {%1Dad}', h^.constsym)
308 END ReportUnresolvedConstTypes ;
315 PROCEDURE DebugMeta (h: constList) ;
322 n := GetSymName(constsym) ;
323 printf1('constant %a ', n) ;
326 printf0('type is unknown\n')
328 printf0('type is known\n')
339 PROCEDURE constTypeResolved (h: constList) : BOOLEAN ;
341 RETURN( h^.type#NulSym )
342 END constTypeResolved ;
349 PROCEDURE constExprResolved (h: constList) : BOOLEAN ;
351 RETURN( h^.expr#NulSym )
352 END constExprResolved ;
359 PROCEDURE findConstMetaExpr (h: constList) : constType ;
361 RETURN( h^.constmeta )
362 END findConstMetaExpr ;
366 constResolveViaMeta -
369 PROCEDURE constResolveViaMeta (h: constList) : BOOLEAN ;
374 IF findConstMetaExpr(h)=str
376 PutConstString(constsym, MakeKey('')) ;
379 n := GetSymName(constsym) ;
380 printf1('resolved constant %a as a string\n', n)
386 END constResolveViaMeta ;
390 constResolvedViaType -
393 PROCEDURE constResolvedViaType (h: constList) : BOOLEAN ;
398 type := findConstType(expr) ;
401 PutConst(constsym, type) ;
404 n := GetSymName(constsym) ;
405 printf1('resolved type of constant %a\n', n)
411 END constResolvedViaType ;
418 PROCEDURE resolveConstType (h: constList) : BOOLEAN ;
421 IF (constmeta=unknown) OR (constmeta=str)
426 IF constTypeResolved(h)
430 IF constExprResolved(h)
432 IF constResolveViaMeta(h)
435 ELSIF constResolvedViaType(h)
444 END resolveConstType ;
448 ResolveConstTypes - resolves the types of all aggegrate constants.
451 PROCEDURE ResolveConstTypes ;
460 changed := resolveConstType(h) ;
464 ReportUnresolvedConstTypes
465 END ResolveConstTypes ;
469 SkipConst - returns the symbol which is a pseudonum of, sym.
472 PROCEDURE SkipConst (sym: CARDINAL) : CARDINAL ;
480 IF (h^.constsym=sym) AND (h^.expr#NulSym)
485 (* circular definition found *)