djrieger / mjplusplus

A compiler for the MiniJava language
http://djrieger.github.io/mjplusplus/doc/doxygen/html/
6 stars 1 forks source link

Fix error position for UnaryExpressions #54

Closed djrieger closed 9 years ago

djrieger commented 9 years ago

UnaryExpressions currently set the error position tot the position of their child expression but it should be set to this (UnaryExpression.cpp, lines 100 and 138). I tried replacing child in "is not ..."., child); with std::const_pointer_cast<PositionAwareNode>(shared_from_this()). I need a (pointer) cast since reportError() expects a shptr<PositionAwareNode> and shared_from_this() returns a shptr<Node>. In fact, since the method reporting the error is declared const, shared_from_this() returns a shptr<const Node>. This is why I thought a const_pointer_castwould be correct, but the compiler simply tells me "Cast is not allowed":

error: const_cast from 'element_type *' (aka 'const ast::Node *') to '_RTp *'
      (aka 'ast::PositionAwareNode *') is not allowed
maxvogel commented 9 years ago

In MethodDeclaration.cpp there was a similar problem, maybe this snippet helps:

    // insert this method into the method table in the class table
    auto ct = sa.getClassTable();
    auto const foo = shared_from_this();
    auto md_node = std::static_pointer_cast<const MethodDeclaration>(foo);
    ct[class_name].methodTable->insertMethod(return_type_and_name->getName(), md_node, returnType, param_types);

I'm not sure if I really get the problem without the code, maybe you need different versions of printError (in regard to const-ness).

Nidan commented 9 years ago

As far as I know a const(_pointer)_cast can only add or remove the const qualifier, as Max mentioned a static_pointer_cast<const ast::PositionAwareNode> is needed. Any complaints about constness in the arguments to reportError should be fixed by declaring the offending parameter const. If the compiler can't convert from shptr<Foo> to shptr<Foo const> automatically, this will require const_pointer_casts in every other call to reportError however.

djrieger commented 9 years ago

Just figured there's a much easier solution:

sa.reportError(.., this->getPosition());