Skip to content

Commit

Permalink
Added some proper error messages to NFLookup.
Browse files Browse the repository at this point in the history
  • Loading branch information
perost authored and OpenModelica-Hudson committed May 24, 2016
1 parent d816a47 commit 1e5da4b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 48 deletions.
18 changes: 10 additions & 8 deletions Compiler/FrontEnd/NFInst.mo
Expand Up @@ -71,7 +71,7 @@ algorithm
System.startTimer();
inst_tree := makeTree(program);

(cls, inst_tree) := Lookup.lookupName(classPath, inst_tree);
(cls, inst_tree) := Lookup.lookupClassName(classPath, inst_tree, Absyn.dummyInfo);
(cls, inst_tree) := instantiate(cls, Modifier.NOMOD(), inst_tree);

(cls, inst_tree) := instBindings(cls, inst_tree);
Expand Down Expand Up @@ -197,6 +197,7 @@ protected
Absyn.Import i;
Integer scope_idx;
InstNode node;
SourceInfo info;
algorithm
if listEmpty(imports) then
return;
Expand All @@ -205,20 +206,20 @@ algorithm
scope_idx := InstanceTree.currentScopeIndex(tree);

for imp in imports loop
SCode.IMPORT(imp = i) := imp;
SCode.IMPORT(imp = i, info = info) := imp;

_ := match i
case Absyn.NAMED_IMPORT()
algorithm
(node, tree) := Lookup.lookupName(Absyn.FULLYQUALIFIED(i.path), tree);
(node, tree) := Lookup.lookupClassName(Absyn.FULLYQUALIFIED(i.path), tree, info);
scope := ClassTree.add(scope, i.name,
ElementId.CLASS_ID(InstNode.index(node)));
then
();

case Absyn.QUAL_IMPORT()
algorithm
(node, tree) := Lookup.lookupName(Absyn.FULLYQUALIFIED(i.path), tree);
(node, tree) := Lookup.lookupClassName(Absyn.FULLYQUALIFIED(i.path), tree, info);
scope := ClassTree.add(scope, Absyn.pathLastIdent(i.path),
ElementId.CLASS_ID(InstNode.index(node)));
then
Expand Down Expand Up @@ -320,7 +321,7 @@ algorithm

// Lookup and expand the derived class.
Absyn.TPATH(path = ty_path) := ty;
(expandedNode, tree) := Lookup.lookupName(ty_path, tree);
(expandedNode, tree) := Lookup.lookupClassName(ty_path, tree, definition.info);
(expandedNode, tree) := expand(expandedNode, tree);

// Process the modifier from the class.
Expand Down Expand Up @@ -394,7 +395,7 @@ algorithm
case SCode.EXTENDS()
algorithm
// Look up the name and expand the class.
(ext_node, tree) := Lookup.lookupName(e.baseClassPath, tree);
(ext_node, tree) := Lookup.lookupBaseClassName(e.baseClassPath, tree, e.info);
(ext_node, tree) := expand(ext_node, tree);

// Initialize the modifier from the extends clause.
Expand Down Expand Up @@ -585,7 +586,7 @@ algorithm
comp_mod := Modifier.merge(component.modifier, comp_mod);
//comp_mod := Modifier.merge(modifier, comp_mod);
binding := Modifier.binding(comp_mod);
(cls, tree) := instTypeSpec(comp.typeSpec, comp_mod, tree);
(cls, tree) := instTypeSpec(comp.typeSpec, comp_mod, comp.info, tree);
ty := makeType(cls);
then
(Component.COMPONENT(comp.name, cls, ty, binding), tree);
Expand All @@ -595,13 +596,14 @@ end instComponent;
function instTypeSpec
input Absyn.TypeSpec typeSpec;
input Modifier modifier;
input SourceInfo info;
output InstNode node;
input output InstanceTree tree;
algorithm
(node, tree) := match typeSpec
case Absyn.TPATH()
algorithm
(node, tree) := Lookup.lookupName(typeSpec.path, tree);
(node, tree) := Lookup.lookupClassName(typeSpec.path, tree, info);
(node, tree) := instantiate(node, modifier, tree);
then
(node, tree);
Expand Down
118 changes: 78 additions & 40 deletions Compiler/FrontEnd/NFLookup.mo
Expand Up @@ -36,6 +36,7 @@ encapsulated package NFLookup
"

import Absyn;
import Error;
import Inst = NFInst;
import NFInstance.ClassTree;
import NFInstance.Instance;
Expand All @@ -52,24 +53,62 @@ constant NFInst.InstNode BOOL_TYPE = NFInstNode.INST_NODE("Boolean",
constant NFInst.InstNode STRING_TYPE = NFInstNode.INST_NODE("String",
SOME(NFBuiltin.BUILTIN_STRING), NFInstance.PARTIAL_BUILTIN(), 0, 0);

function lookupClassName
input Absyn.Path name;
output InstNode instance;
input output InstanceTree tree;
input SourceInfo info;
algorithm
(instance, tree) := lookupName(name, tree, info, Error.LOOKUP_ERROR);
end lookupClassName;

function lookupBaseClassName
input Absyn.Path name;
output InstNode instance;
input output InstanceTree tree;
input SourceInfo info;
algorithm
(instance, tree) := lookupName(name, tree, info, Error.LOOKUP_BASECLASS_ERROR);
end lookupBaseClassName;

function lookupVariableName
input Absyn.Path name;
output InstNode instance;
input output InstanceTree tree;
input SourceInfo info;
algorithm
(instance, tree) := lookupName(name, tree, info, Error.LOOKUP_VARIABLE_ERROR);
end lookupVariableName;

function lookupFunctionName
input Absyn.Path name;
output InstNode instance;
input output InstanceTree tree;
input SourceInfo info;
algorithm
(instance, tree) := lookupName(name, tree, info, Error.LOOKUP_FUNCTION_ERROR);
end lookupFunctionName;

protected

function lookupSimpleName
input String inName;
input InstanceTree inTree;
output InstNode outInstance;
input String name;
input InstanceTree tree;
output InstNode instance;
protected
InstNode scope;
InstVector.Vector iv;
ClassTree.Tree scope_tree;
Integer scope_idx, idx;
algorithm
InstanceTree.INST_TREE(currentScope = scope_idx, instances = iv) := inTree;
InstanceTree.INST_TREE(currentScope = scope_idx, instances = iv) := tree;

while scope_idx <> NFInstanceTree.NO_SCOPE loop
scope := InstVector.get(iv, scope_idx);

try
idx := Instance.lookupClassId(inName, InstNode.instance(scope));
outInstance := InstVector.get(iv, idx);
idx := Instance.lookupClassId(name, InstNode.instance(scope));
instance := InstVector.get(iv, idx);
return;
else
scope_idx := InstNode.parent(scope);
Expand All @@ -80,92 +119,91 @@ algorithm
end lookupSimpleName;

function lookupName
input Absyn.Path inName;
input InstanceTree inTree;
output InstNode outInstance;
output InstanceTree outTree;
input Absyn.Path name;
output InstNode instance;
input output InstanceTree tree;
input SourceInfo info;
input Error.Message errorType;
algorithm
(outInstance, outTree) := matchcontinue inName
(instance, tree) := matchcontinue name
local
InstanceTree tree;
InstNode i;

case Absyn.IDENT()
then (lookupSimpleBuiltinName(inName.name), inTree);
then (lookupSimpleBuiltinName(name.name), tree);

case Absyn.IDENT()
then (lookupSimpleName(inName.name, inTree), inTree);
then (lookupSimpleName(name.name, tree), tree);

// Qualified name, look up first part, expand it, and look up the rest of
// the name in the expanded instance.
case Absyn.QUALIFIED()
algorithm
i := lookupSimpleName(inName.name, inTree);
(i, tree) := Inst.expand(i, inTree);
i := lookupSimpleName(name.name, tree);
(i, tree) := Inst.expand(i, tree);
then
lookupLocalName(inName.path, tree);
lookupLocalName(name.path, tree);

// Fully qualified path, start from top scope.
case Absyn.FULLYQUALIFIED()
algorithm
tree := InstanceTree.setCurrentScope(inTree, NFInstanceTree.TOP_SCOPE);
tree := InstanceTree.setCurrentScope(tree, NFInstanceTree.TOP_SCOPE);
then
lookupName(inName.path, tree);
lookupName(name.path, tree, info, errorType);

else
algorithm
print(Absyn.pathString(inName) + " could not be found.\n");
Error.addSourceMessage(errorType, {Absyn.pathString(name), "<unknown>"}, info);
then
fail();

end matchcontinue;
end lookupName;

function lookupLocalSimpleName
input String inName;
input InstanceTree inTree;
output InstNode outInstance;
input String name;
input InstanceTree tree;
output InstNode instance;
protected
ClassTree.Tree scope_tree;
InstVector.Vector iv;
Integer idx;
algorithm
// Look up the current scope.
InstanceTree.INST_TREE(currentScope = idx, instances = iv) := inTree;
outInstance := InstVector.get(iv, idx);
InstanceTree.INST_TREE(currentScope = idx, instances = iv) := tree;
instance := InstVector.get(iv, idx);
// Look up the name in that scope.
idx := Instance.lookupClassId(inName, InstNode.instance(outInstance));
outInstance := InstVector.get(iv, idx);
idx := Instance.lookupClassId(name, InstNode.instance(instance));
instance := InstVector.get(iv, idx);
end lookupLocalSimpleName;

function lookupLocalName
input Absyn.Path inName;
input InstanceTree inTree;
output InstNode outInstance;
output InstanceTree outTree;
input Absyn.Path name;
output InstNode instance;
input output InstanceTree tree;
algorithm
(outInstance, outTree) := match inName
(instance, tree) := match name
local
InstNode i;
InstanceTree tree;

case Absyn.IDENT()
then (lookupLocalSimpleName(inName.name, inTree), inTree);
then (lookupLocalSimpleName(name.name, tree), tree);

case Absyn.QUALIFIED()
algorithm
i := lookupLocalSimpleName(inName.name, inTree);
(i, tree) := Inst.expand(i, inTree);
i := lookupLocalSimpleName(name.name, tree);
(i, tree) := Inst.expand(i, tree);
then
lookupLocalName(inName.path, tree);
lookupLocalName(name.path, tree);

end match;
end lookupLocalName;

function lookupSimpleBuiltinName
input String inName;
output InstNode outBuiltin;
input String name;
output InstNode builtin;
algorithm
outBuiltin := match inName
builtin := match name
case "Real" then REAL_TYPE;
case "Integer" then INT_TYPE;
case "Boolean" then BOOL_TYPE;
Expand Down

0 comments on commit 1e5da4b

Please sign in to comment.