@@ -52,6 +52,7 @@ import NFInstNode.NodeTree;
5252import NFInstNode.CachedData ;
5353import NFComponent.Component ;
5454import Subscript = NFSubscript ;
55+ import ComplexType = NFComplexType ;
5556
5657public
5758type MatchType = enumeration(FOUND , NOT_FOUND , PARTIAL );
@@ -97,7 +98,8 @@ protected
9798 LookupState state;
9899 InstNode node;
99100algorithm
100- (foundCref, foundScope, state) := lookupCref(cref, scope, info);
101+ (foundCref, foundScope, state) := lookupCref(cref, scope, info,
102+ Error . LOOKUP_VARIABLE_ERROR );
101103 node := ComponentRef . node(foundCref);
102104 state := fixTypenameState(node, state);
103105 LookupState . assertComponent(state, node, cref, info);
@@ -137,26 +139,6 @@ algorithm
137139 LookupState . assertComponent(state, ComponentRef . node(foundCref), cref, info);
138140end lookupLocalComponent;
139141
140- function lookupCallableName
141- input Absyn . ComponentRef cref;
142- input InstNode scope "The scope to look in." ;
143- input SourceInfo info;
144- output ComponentRef foundCref;
145- output InstNode foundScope;
146- protected
147- LookupState state;
148- InstNode node;
149- algorithm
150- (foundCref, foundScope, state) := lookupCref(cref, scope, info);
151- node := ComponentRef . node(foundCref);
152-
153- // try
154- LookupState . assertFunction(state, node, cref, info);
155- // else
156- // end try;
157-
158- end lookupCallableName;
159-
160142function lookupFunctionName
161143 input Absyn . ComponentRef cref;
162144 input InstNode scope "The scope to look in." ;
@@ -167,11 +149,52 @@ protected
167149 LookupState state;
168150 InstNode node;
169151algorithm
170- (foundCref, foundScope, state) := lookupCref(cref, scope, info);
152+ (foundCref, foundScope, state) := lookupCref(cref, scope, info,
153+ Error . LOOKUP_FUNCTION_ERROR );
171154 node := ComponentRef . node(foundCref);
155+ (foundCref, state) := fixExternalObjectCall(node, foundCref, state);
172156 LookupState . assertFunction(state, node, cref, info);
173157end lookupFunctionName;
174158
159+ function fixExternalObjectCall
160+ "Changes calls to external objects so that the constructor is called instead,
161+ i.e. a call such as
162+ 'ExtObj eo = ExtObj(...)'
163+ is changed to
164+ 'ExtObj eo = ExtObj.constructor(...)'"
165+ input InstNode node;
166+ input output ComponentRef cref;
167+ input output LookupState state;
168+ protected
169+ Class cls;
170+ InstNode constructor;
171+ algorithm
172+ // If it's not a class it can't be an external object.
173+ if not LookupState . isClass(state) then
174+ return ;
175+ end if ;
176+
177+ // External objects are identified by extending from ExternalObject, so the
178+ // node needs to be expanded before we know whether it's an external object or
179+ // not. Components are instantiated before their bindings, so in proper models
180+ // we shouldn't get any non-expanded external objects here. But to avoid
181+ // getting weird errors in erroneous models we make sure it's expanded anyway.
182+ Inst . expand(node);
183+ cls := InstNode . getClass(node);
184+
185+ () := match cls
186+ case Class . PARTIAL_BUILTIN (ty = Type . COMPLEX (complexTy =
187+ ComplexType . EXTERNAL_OBJECT (constructor = constructor)))
188+ algorithm
189+ cref := ComponentRef . prefixCref(constructor, Type . UNKNOWN (), {}, cref);
190+ state := LookupState . FUNC ();
191+ then
192+ ();
193+
194+ else ();
195+ end match;
196+ end fixExternalObjectCall;
197+
175198function lookupImport
176199 input Absyn . Path name;
177200 input InstNode scope;
@@ -188,6 +211,7 @@ function lookupCref
188211 input Absyn . ComponentRef cref;
189212 input InstNode scope "The scope to look in." ;
190213 input SourceInfo info;
214+ input Error . Message errMsg;
191215 output ComponentRef foundCref;
192216 output InstNode foundScope "The scope where the first part of the cref was found." ;
193217 output LookupState state;
@@ -224,7 +248,7 @@ algorithm
224248 (foundCref, foundScope, state);
225249
226250 case Absyn . ComponentRef . CREF_FULLYQUALIFIED ()
227- then lookupCref(cref. componentRef, InstNode . topScope(scope), info);
251+ then lookupCref(cref. componentRef, InstNode . topScope(scope), info, errMsg );
228252
229253 case Absyn . ComponentRef . WILD () then (ComponentRef . WILD (), scope, LookupState . PREDEF_COMP ());
230254 case Absyn . ComponentRef . ALLWILD () then (ComponentRef . WILD (), scope, LookupState . PREDEF_COMP ());
@@ -242,7 +266,7 @@ algorithm
242266 end if ;
243267
244268 if match_ty <> MatchType . FOUND then
245- Error . addSourceMessage(Error . LOOKUP_VARIABLE_ERROR ,
269+ Error . addSourceMessage(errMsg ,
246270 {Dump . printComponentRefStr(cref), InstNode . name(scope)}, info);
247271 fail();
248272 end if ;
0 commit comments