djrieger / mjplusplus

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

Fix initializer list warnings due to PositionAwareNode base constructor #53

Closed djrieger closed 9 years ago

djrieger commented 9 years ago

I made Expression and ReturnStatement derive from PositionAwareNode and therefore all of their children such as PrimaryExpression, BinaryExpression, UnaryExpression, PostfixExpression and again their children and so forth. Not we have tons of warnings like:

src/ast/PrimaryExpression.cpp:49:48: warning: field 'identifier' will be initialized after base 'ast::PositionAwareNode' [-Wreorder]
                Ident::Ident(shptr<ast::Ident> identifier) : identifier(identifier), PositionAwareNode(identifier->getPosition())

These warnings are all identical for the constructors of these classes. The thing is: If I swap PositionAwareNode(...) and identifier(...) in the initializer list, I get rid of the warning but identifier is not initialized when identifier->getPosition() is called and everything breaks.

Can anyone tell me how to solve this correctly?

Nidan commented 9 years ago

Have you tried renaming the parameter to something else than "identifier"? I think the compiler uses the member identifier when initializing PositionAwareNode. Removing the name clash might help.

djrieger commented 9 years ago

I don't think this will solve the problem as the warnings are the same for all constructors, whatever the name of the arguments may be:

src/ast/UnaryExpression.cpp:122:23: warning: base class 'UnaryExpression::UnaryExpression' will be
      initialized after base 'ast::PositionAwareNode' [-Wreorder]
                        : UnaryExpression::UnaryExpression(child, size), PositionAwareNode(ch...
                                           ^

src/ast/ReturnStatement.cpp:5:2: warning: field 'expression' will be initialized after base
      'ast::PositionAwareNode' [-Wreorder]
        expression(expression), PositionAwareNode(expression->getPosition())
        ^

src/ast/PrimaryExpression.cpp:168:73: warning: field 'object_type' will be initialized after base
      'ast::PositionAwareNode' [-Wreorder]
                Object::Object(Object_Type object_type, source_position_t position) : object_...
                                                                                      ^

and so on

Nidan commented 9 years ago

Had my own shot at the problem now. g++ complained about PositionAwareNode not having a zero arguments constructor while compiling BinaryExpression.cpp, which I think was caused by combining PositionAwareNode being virtual with abstract classes inheriting from it. (No idea for what reason exactly.) Replacing that with "normal" inheritance removed that error and allowed reordering the constructors