diff --git a/src/pac_action.cc b/src/pac_action.cc index 05cd611..2863f65 100644 --- a/src/pac_action.cc +++ b/src/pac_action.cc @@ -34,15 +34,14 @@ void AnalyzerAction::GenCode(Output* out_h, Output* out_cc, AnalyzerDecl* decl) out_h->println("void %s;", action_func_proto.c_str()); - out_cc->println("void %s::%s", decl->class_name().c_str(), action_func_proto.c_str()); + out_cc->println("void %s::%s {", decl->class_name().c_str(), action_func_proto.c_str()); out_cc->inc_indent(); - out_cc->println("{"); code_->GenCode(out_cc, &action_func_env); out_cc->println(""); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); out_cc->println(""); } diff --git a/src/pac_array.cc b/src/pac_array.cc index bf7710a..b59d9a4 100644 --- a/src/pac_array.cc +++ b/src/pac_array.cc @@ -42,7 +42,7 @@ void ArrayType::init() { vector_str_ = strfmt("vector<%s>", elemtype_->DataTypeStr().c_str()); - datatype_str_ = strfmt("%s *", vector_str_.c_str()); + datatype_str_ = strfmt("%s*", vector_str_.c_str()); attr_generic_until_expr_ = nullptr; attr_until_element_expr_ = nullptr; @@ -210,14 +210,13 @@ void ArrayType::GenArrayLength(Output* out_cc, Env* env, const DataPtr& data) { env->SetEvaluated(arraylength_var()); // Check negative array length - out_cc->println("if ( %s < 0 )", env->LValue(arraylength_var())); + out_cc->println("if ( %s < 0 ) {", env->LValue(arraylength_var())); out_cc->inc_indent(); - out_cc->println("{"); out_cc->println("throw binpac::ExceptionOutOfBound(\"%s\",", data_id_str_.c_str()); out_cc->println(" %s, (%s) - (%s));", env->LValue(arraylength_var()), env->RValue(end_of_data), env->RValue(begin_of_data)); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); int element_size; @@ -309,19 +308,17 @@ void ArrayType::GenCleanUpCode(Output* out_cc, Env* env) { elem_var_field_->Prepare(env); } - out_cc->println("if ( %s )", env->RValue(value_var())); + out_cc->println("if ( %s ) {", env->RValue(value_var())); out_cc->inc_indent(); - out_cc->println("{"); - out_cc->println("for ( auto* %s : *%s )", env->LValue(elem_var()), env->RValue(value_var())); + out_cc->println("for ( auto* %s : *%s ) {", env->LValue(elem_var()), env->RValue(value_var())); out_cc->inc_indent(); - out_cc->println("{"); elemtype_->GenCleanUpCode(out_cc, env); - out_cc->println("}"); out_cc->dec_indent(); - out_cc->println("}"); + out_cc->dec_indent(); + out_cc->println("}"); } out_cc->println("delete %s;", lvalue()); } @@ -331,9 +328,8 @@ string ArrayType::GenArrayInit(Output* out_cc, Env* env, bool known_array_length array_str = lvalue(); if ( incremental_parsing() ) { - out_cc->println("if ( %s < 0 )", env->LValue(elem_it_var())); + out_cc->println("if ( %s < 0 ) {", env->LValue(elem_it_var())); out_cc->inc_indent(); - out_cc->println("{"); out_cc->println("// Initialize only once"); out_cc->println("%s = 0;", env->LValue(elem_it_var())); } @@ -345,8 +341,8 @@ string ArrayType::GenArrayInit(Output* out_cc, Env* env, bool known_array_length } if ( incremental_parsing() ) { - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); } return array_str; @@ -426,9 +422,8 @@ void ArrayType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data, in strfmt("%s < %s", env->LValue(elem_it_var()), env->RValue(arraylength_var())) : "/* forever */"; - out_cc->println("for (; %s; ++%s)", for_condition.c_str(), env->LValue(elem_it_var())); + out_cc->println("for (; %s; ++%s) {", for_condition.c_str(), env->LValue(elem_it_var())); out_cc->inc_indent(); - out_cc->println("{"); if ( attr_generic_until_expr_ ) GenUntilCheck(out_cc, env, attr_generic_until_expr_, true); @@ -472,8 +467,8 @@ void ArrayType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data, in if ( elemtype_->IsPointerType() ) out_cc->println("%s = nullptr;", env->LValue(elem_var())); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); out_cc->dec_indent(); out_cc->println("%s: ;", end_of_array_loop_label_.c_str()); @@ -508,9 +503,8 @@ void ArrayType::GenUntilCheck(Output* out_cc, Env* env, Expr* until_expr, bool d } out_cc->println("// Check &until(%s)", until_expr->orig()); - out_cc->println("if ( %s )", until_expr->EvalExpr(out_cc, &check_env)); + out_cc->println("if ( %s ) {", until_expr->EvalExpr(out_cc, &check_env)); out_cc->inc_indent(); - out_cc->println("{"); if ( parsing_complete_var() ) { out_cc->println("%s = true;", env->LValue(parsing_complete_var())); } @@ -523,8 +517,8 @@ void ArrayType::GenUntilCheck(Output* out_cc, Env* env, Expr* until_expr, bool d } out_cc->println("goto %s;", end_of_array_loop_label_.c_str()); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); } void ArrayType::GenDynamicSize(Output* out_cc, Env* env, const DataPtr& data) { diff --git a/src/pac_btype.cc b/src/pac_btype.cc index 9be860b..d56b9ab 100644 --- a/src/pac_btype.cc +++ b/src/pac_btype.cc @@ -94,7 +94,7 @@ void BuiltInType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data, case INT8: case UINT8: - out_cc->println("%s = *((%s const *) (%s));", lvalue(), DataTypeStr().c_str(), data.ptr_expr()); + out_cc->println("%s = *((%s const*)(%s));", lvalue(), DataTypeStr().c_str(), data.ptr_expr()); break; case INT16: case UINT16: @@ -104,13 +104,13 @@ void BuiltInType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data, case UINT64: #if 0 out_cc->println("%s = UnMarshall<%s>(%s, %s);", - lvalue(), - DataTypeStr().c_str(), + lvalue(), + DataTypeStr().c_str(), data.ptr_expr(), EvalByteOrder(out_cc, env).c_str()); #else - out_cc->println("%s = FixByteOrder(%s, *((%s const *) (%s)));", lvalue(), - EvalByteOrder(out_cc, env).c_str(), DataTypeStr().c_str(), data.ptr_expr()); + out_cc->println("%s = FixByteOrder(%s, *((%s const*)(%s)));", lvalue(), EvalByteOrder(out_cc, env).c_str(), + DataTypeStr().c_str(), data.ptr_expr()); #endif break; } diff --git a/src/pac_case.cc b/src/pac_case.cc index a2131ca..187eaf3 100644 --- a/src/pac_case.cc +++ b/src/pac_case.cc @@ -36,7 +36,7 @@ bool CaseType::DefineValueVar() const { return false; } string CaseType::DataTypeStr() const { ASSERT(type_decl()); - return strfmt("%s *", type_decl()->class_name().c_str()); + return strfmt("%s*", type_decl()->class_name().c_str()); } Type* CaseType::ValueType() const { @@ -117,15 +117,14 @@ void CaseType::GenCleanUpCode(Output* out_cc, Env* env) { Type::GenCleanUpCode(out_cc, env); env->set_in_branch(true); - out_cc->println("switch ( %s )", env->RValue(index_var_)); + out_cc->println("switch ( %s ) {", env->RValue(index_var_)); out_cc->inc_indent(); - out_cc->println("{"); foreach (i, CaseFieldList, cases_) { CaseField* c = *i; c->GenCleanUpCode(out_cc, env); } - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); env->set_in_branch(false); } @@ -142,9 +141,8 @@ void CaseType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data, int env->SetEvaluated(index_var_); env->set_in_branch(true); - out_cc->println("switch ( %s )", env->RValue(index_var_)); + out_cc->println("switch ( %s ) {", env->RValue(index_var_)); out_cc->inc_indent(); - out_cc->println("{"); bool has_default_case = false; foreach (i, CaseFieldList, cases_) { CaseField* c = *i; @@ -161,8 +159,8 @@ void CaseType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data, int out_cc->println("break;"); out_cc->dec_indent(); } - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); env->set_in_branch(false); if ( compute_size_var ) @@ -282,7 +280,7 @@ void GenCaseStr(ExprList* index_list, Output* out_cc, Env* env, Type* switch_typ // We're always using "int" for storage, so ok to just // cast into the type used by the switch statement since // some unsafe stuff is already checked above. - out_cc->println("case ((%s) %d):", switch_type->DataTypeStr().c_str(), index_const); + out_cc->println("case ((%s)%d):", switch_type->DataTypeStr().c_str(), index_const); } } else { @@ -303,17 +301,14 @@ void CaseField::GenPubDecls(Output* out_h, Env* env) { if ( type_->DataTypeStr().empty() ) return; - out_h->println("%s %s const", type_->DataTypeConstRefStr().c_str(), env->RValue(id_)); - + out_h->println("%s %s const {", type_->DataTypeConstRefStr().c_str(), env->RValue(id_)); out_h->inc_indent(); - out_h->println("{"); if ( ! index_ ) out_h->println("return %s;", lvalue()); else { - out_h->println("switch ( %s )", env->RValue(index_var_)); + out_h->println("switch ( %s ) {", env->RValue(index_var_)); out_h->inc_indent(); - out_h->println("{"); GenCaseStr(index_, out_h, env, case_type()->IndexExpr()->DataType(env)); out_h->inc_indent(); out_h->println("break; // OK"); @@ -326,14 +321,14 @@ void CaseField::GenPubDecls(Output* out_h, Env* env) { out_h->println("break;"); out_h->dec_indent(); - out_h->println("}"); out_h->dec_indent(); + out_h->println("}"); out_h->println("return %s;", lvalue()); } - out_h->println("}"); out_h->dec_indent(); + out_h->println("}"); } void CaseField::GenInitCode(Output* out_cc, Env* env) { @@ -351,10 +346,16 @@ void CaseField::GenCleanUpCode(Output* out_cc, Env* env) { GenCaseStr(index_, out_cc, env, case_type()->IndexExpr()->DataType(env)); out_cc->inc_indent(); out_cc->println("// Clean up \"%s\"", id_->Name()); - out_cc->println("{"); - if ( ! anonymous_field() ) + if ( ! anonymous_field() ) { + out_cc->println("{"); + out_cc->inc_indent(); type_->GenCleanUpCode(out_cc, env); - out_cc->println("}"); + out_cc->dec_indent(); + out_cc->println("}"); + } + else + out_cc->println("{}"); + out_cc->println("break;"); out_cc->dec_indent(); } @@ -364,6 +365,7 @@ void CaseField::GenParseCode(Output* out_cc, Env* env, const DataPtr& data, cons out_cc->inc_indent(); out_cc->println("// Parse \"%s\"", id_->Name()); out_cc->println("{"); + out_cc->inc_indent(); { Env case_env(env, this); @@ -378,9 +380,10 @@ void CaseField::GenParseCode(Output* out_cc, Env* env, const DataPtr& data, cons out_cc->println("%s = %s;", case_env.LValue(case_type()->parsing_complete_var()), case_env.RValue(type_->parsing_complete_var())); } - out_cc->println("}"); } + out_cc->dec_indent(); + out_cc->println("}"); out_cc->println("break;"); out_cc->dec_indent(); } diff --git a/src/pac_conn.cc b/src/pac_conn.cc index 4d04c69..4e7bc01 100644 --- a/src/pac_conn.cc +++ b/src/pac_conn.cc @@ -63,9 +63,8 @@ void ConnDecl::GenEOFFunc(Output* out_h, Output* out_cc) { out_h->println("void %s;", proto.c_str()); - out_cc->println("void %s::%s", class_name().c_str(), proto.c_str()); + out_cc->println("void %s::%s {", class_name().c_str(), proto.c_str()); out_cc->inc_indent(); - out_cc->println("{"); out_cc->println("if ( is_orig )"); out_cc->inc_indent(); @@ -81,8 +80,8 @@ void ConnDecl::GenEOFFunc(Output* out_h, Output* out_cc) { out_cc->dec_indent(); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); out_cc->println(""); } @@ -91,9 +90,8 @@ void ConnDecl::GenGapFunc(Output* out_h, Output* out_cc) { out_h->println("void %s;", proto.c_str()); - out_cc->println("void %s::%s", class_name().c_str(), proto.c_str()); + out_cc->println("void %s::%s {", class_name().c_str(), proto.c_str()); out_cc->inc_indent(); - out_cc->println("{"); out_cc->println("if ( is_orig )"); out_cc->inc_indent(); @@ -104,8 +102,8 @@ void ConnDecl::GenGapFunc(Output* out_h, Output* out_cc) { out_cc->println("%s->%s(gap_length);", env_->LValue(downflow_id), kFlowGap); out_cc->dec_indent(); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); out_cc->println(""); } @@ -114,9 +112,8 @@ void ConnDecl::GenProcessFunc(Output* out_h, Output* out_cc) { out_h->println("void %s;", proto.c_str()); - out_cc->println("void %s::%s", class_name().c_str(), proto.c_str()); + out_cc->println("void %s::%s {", class_name().c_str(), proto.c_str()); out_cc->inc_indent(); - out_cc->println("{"); out_cc->println("if ( is_orig )"); out_cc->inc_indent(); @@ -127,7 +124,7 @@ void ConnDecl::GenProcessFunc(Output* out_h, Output* out_cc) { out_cc->println("%s->%s(begin, end);", env_->LValue(downflow_id), kNewData); out_cc->dec_indent(); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); out_cc->println(""); } diff --git a/src/pac_ctype.cc b/src/pac_ctype.cc index 3ef8003..cb36a9f 100644 --- a/src/pac_ctype.cc +++ b/src/pac_ctype.cc @@ -3,11 +3,11 @@ string CType::DeclareInstance(const string& var) const { return strfmt("%s %s", name().c_str(), var.c_str()); } string CType::DeclareConstReference(const string& var) const { - return strfmt("%s const &%s", name().c_str(), var.c_str()); + return strfmt("%s const& %s", name().c_str(), var.c_str()); } string CType::DeclareConstPointer(const string& var) const { - return strfmt("%s const *%s", name().c_str(), var.c_str()); + return strfmt("%s const* %s", name().c_str(), var.c_str()); } -string CType::DeclarePointer(const string& var) const { return strfmt("%s *%s", name().c_str(), var.c_str()); } +string CType::DeclarePointer(const string& var) const { return strfmt("%s* %s", name().c_str(), var.c_str()); } diff --git a/src/pac_dataptr.cc b/src/pac_dataptr.cc index 1b3d196..53d44e6 100644 --- a/src/pac_dataptr.cc +++ b/src/pac_dataptr.cc @@ -32,11 +32,10 @@ void DataPtr::GenBoundaryCheck(Output* out_cc, Env* env, const char* data_size, ASSERT(id_); out_cc->println("// Checking out-of-bound for \"%s\"", data_name); - out_cc->println("if ( %s + (%s) > %s || %s + (%s) < %s )", ptr_expr(), data_size, env->RValue(end_of_data), + out_cc->println("if ( %s + (%s) > %s || %s + (%s) < %s ) {", ptr_expr(), data_size, env->RValue(end_of_data), ptr_expr(), data_size, ptr_expr()); out_cc->inc_indent(); - out_cc->println("{"); char* data_offset = AbsOffsetExpr(env, begin_of_data); @@ -47,6 +46,6 @@ void DataPtr::GenBoundaryCheck(Output* out_cc, Env* env, const char* data_size, delete[] data_offset; - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); } diff --git a/src/pac_expr.cc b/src/pac_expr.cc index 98fd1e2..4f74859 100644 --- a/src/pac_expr.cc +++ b/src/pac_expr.cc @@ -209,11 +209,10 @@ void Expr::GenCaseEval(Output* out_cc, Env* env) { foreach (i, CaseExprList, cases_) (*i)->value()->ForceIDEval(out_cc, env); - out_cc->println("switch ( %s )", operand_[0]->EvalExpr(out_cc, env)); + out_cc->println("switch ( %s ) {", operand_[0]->EvalExpr(out_cc, env)); Type* switch_type = operand_[0]->DataType(env); out_cc->inc_indent(); - out_cc->println("{"); CaseExpr* default_case = nullptr; foreach (i, CaseExprList, cases_) { @@ -246,8 +245,8 @@ void Expr::GenCaseEval(Output* out_cc, Env* env) { out_cc->println("break;"); out_cc->dec_indent(); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); env->SetEvaluated(val_var); str_ = env->RValue(val_var); diff --git a/src/pac_flow.cc b/src/pac_flow.cc index 72c8a83..2138acb 100644 --- a/src/pac_flow.cc +++ b/src/pac_flow.cc @@ -112,9 +112,8 @@ void FlowDecl::GenEOFFunc(Output* out_h, Output* out_cc) { out_h->println("void %s;", proto.c_str()); - out_cc->println("void %s::%s", class_name().c_str(), proto.c_str()); + out_cc->println("void %s::%s {", class_name().c_str(), proto.c_str()); out_cc->inc_indent(); - out_cc->println("{"); foreach (i, AnalyzerHelperList, eof_helpers_) { (*i)->GenCode(nullptr, out_cc, this); @@ -125,8 +124,8 @@ void FlowDecl::GenEOFFunc(Output* out_h, Output* out_cc) { out_cc->println("%s(nullptr, nullptr);", kNewData); } - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); } void FlowDecl::GenGapFunc(Output* out_h, Output* out_cc) { @@ -134,16 +133,15 @@ void FlowDecl::GenGapFunc(Output* out_h, Output* out_cc) { out_h->println("void %s;", proto.c_str()); - out_cc->println("void %s::%s", class_name().c_str(), proto.c_str()); + out_cc->println("void %s::%s {", class_name().c_str(), proto.c_str()); out_cc->inc_indent(); - out_cc->println("{"); if ( dataunit_->type() == AnalyzerDataUnit::FLOWUNIT ) { out_cc->println("%s->NewGap(gap_length);", env_->LValue(flow_buffer_id)); } - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); } void FlowDecl::GenProcessFunc(Output* out_h, Output* out_cc) { @@ -155,13 +153,11 @@ void FlowDecl::GenProcessFunc(Output* out_h, Output* out_cc) { out_h->println("void %s;", proto.c_str()); - out_cc->println("void %s::%s", class_name().c_str(), proto.c_str()); + out_cc->println("void %s::%s {", class_name().c_str(), proto.c_str()); out_cc->inc_indent(); - out_cc->println("{"); - out_cc->println("try"); + out_cc->println("try {"); out_cc->inc_indent(); - out_cc->println("{"); env_->SetEvaluated(begin_of_data); env_->SetEvaluated(end_of_data); @@ -172,22 +168,20 @@ void FlowDecl::GenProcessFunc(Output* out_h, Output* out_cc) { default: ASSERT(0); } - out_cc->println("}"); out_cc->dec_indent(); - out_cc->println("catch ( binpac::Exception const &e )"); + out_cc->println("} catch ( binpac::Exception const& e ) {"); out_cc->inc_indent(); - out_cc->println("{"); GenCleanUpCode(out_cc); if ( dataunit_->type() == AnalyzerDataUnit::FLOWUNIT ) { out_cc->println("%s->DiscardData();", env_->LValue(flow_buffer_id)); } out_cc->println("throw e;"); - out_cc->println("}"); out_cc->dec_indent(); - out_cc->println("}"); + out_cc->dec_indent(); + out_cc->println("}"); out_cc->println(""); } @@ -216,41 +210,36 @@ void FlowDecl::GenCodeFlowUnit(Output* out_cc) { out_cc->println("while ( %s->data_available() && ", env_->LValue(flow_buffer_id)); out_cc->inc_indent(); - out_cc->println("( !%s->have_pending_request() || %s->ready() ) )", env_->LValue(flow_buffer_id), + out_cc->println("( !%s->have_pending_request() || %s->ready() ) ) {", env_->LValue(flow_buffer_id), env_->LValue(flow_buffer_id)); - out_cc->println("{"); // Generate a new dataunit if necessary - out_cc->println("if ( ! %s )", env_->LValue(dataunit_id)); + out_cc->println("if ( ! %s ) {", env_->LValue(dataunit_id)); out_cc->inc_indent(); - out_cc->println("{"); out_cc->println("BINPAC_ASSERT(!%s);", env_->LValue(analyzer_context_id)); GenNewDataUnit(out_cc); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); DataPtr data(env_, nullptr, 0); unit_datatype->GenParseCode(out_cc, env_, data, 0); - out_cc->println("if ( %s )", unit_datatype->parsing_complete(env_).c_str()); + out_cc->println("if ( %s ) {", unit_datatype->parsing_complete(env_).c_str()); out_cc->inc_indent(); - out_cc->println("{"); out_cc->println("// Clean up the flow unit after parsing"); GenDeleteDataUnit(out_cc); // out_cc->println("BINPAC_ASSERT(%s == 0);", env_->LValue(dataunit_id)); - out_cc->println("}"); out_cc->dec_indent(); - out_cc->println("else"); + out_cc->println("} else {"); out_cc->inc_indent(); - out_cc->println("{"); out_cc->println("// Resume upon next input segment"); out_cc->println("BINPAC_ASSERT(!%s->ready());", env_->RValue(flow_buffer_id)); out_cc->println("break;"); - out_cc->println("}"); out_cc->dec_indent(); - out_cc->println("}"); + out_cc->dec_indent(); + out_cc->println("}"); } void FlowDecl::GenCodeDatagram(Output* out_cc) { diff --git a/src/pac_func.cc b/src/pac_func.cc index 3aa6d17..6c1e380 100644 --- a/src/pac_func.cc +++ b/src/pac_func.cc @@ -51,27 +51,23 @@ void Function::GenCode(Output* out_h, Output* out_cc) { ASSERT(! (expr_ && code_)); if ( expr_ ) { - out_cc->println("%s", proto_str.c_str()); - + out_cc->println("%s {", proto_str.c_str()); out_cc->inc_indent(); - out_cc->println("{"); out_cc->println("return static_cast<%s>(%s);", type_->DataTypeStr().c_str(), expr_->EvalExpr(out_cc, env_)); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); } else if ( code_ ) { - out_cc->println("%s", proto_str.c_str()); - + out_cc->println("%s {", proto_str.c_str()); out_cc->inc_indent(); - out_cc->println("{"); code_->GenCode(out_cc, env_); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); } out_cc->println(""); diff --git a/src/pac_let.cc b/src/pac_let.cc index 8aa3adc..27917cd 100644 --- a/src/pac_let.cc +++ b/src/pac_let.cc @@ -63,9 +63,8 @@ void LetField::GenParseCode(Output* out_cc, Env* env) { // force evaluation of IDs contained in this expr expr()->ForceIDEval(out_cc, env); - out_cc->println("if ( %s )", env->RValue(type_->has_value_var())); + out_cc->println("if ( %s ) {", env->RValue(type_->has_value_var())); out_cc->inc_indent(); - out_cc->println("{"); } out_cc->println("%s = %s;", env->LValue(id_), expr()->EvalExpr(out_cc, env)); @@ -73,8 +72,8 @@ void LetField::GenParseCode(Output* out_cc, Env* env) { env->SetEvaluated(id_); if ( type_->attr_if_expr() ) { - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); } } diff --git a/src/pac_output.cc b/src/pac_output.cc index 13b2568..6171b80 100644 --- a/src/pac_output.cc +++ b/src/pac_output.cc @@ -49,8 +49,13 @@ int Output::print(const char* fmt, ...) { } int Output::println(const char* fmt, ...) { + if ( strlen(fmt) == 0 ) { + fprintf(fp, "\n"); + return 0; + } + for ( int i = 0; i < indent(); ++i ) - fprintf(fp, "\t"); + fprintf(fp, " "); va_list ap; va_start(ap, fmt); diff --git a/src/pac_paramtype.cc b/src/pac_paramtype.cc index 25370a4..8cacd16 100644 --- a/src/pac_paramtype.cc +++ b/src/pac_paramtype.cc @@ -27,7 +27,7 @@ void ParameterizedType::AddParamArg(Expr* arg) { args_->push_back(arg); } bool ParameterizedType::DefineValueVar() const { return true; } -string ParameterizedType::DataTypeStr() const { return strfmt("%s *", type_id_->Name()); } +string ParameterizedType::DataTypeStr() const { return strfmt("%s*", type_id_->Name()); } Type* ParameterizedType::MemberDataType(const ID* member_id) const { Type* ref_type = TypeDecl::LookUpType(type_id_); diff --git a/src/pac_record.cc b/src/pac_record.cc index d7ed8db..a650747 100644 --- a/src/pac_record.cc +++ b/src/pac_record.cc @@ -41,7 +41,7 @@ bool RecordType::DefineValueVar() const { return false; } string RecordType::DataTypeStr() const { ASSERT(type_decl()); - return strfmt("%s *", type_decl()->class_name().c_str()); + return strfmt("%s*", type_decl()->class_name().c_str()); } void RecordType::Prepare(Env* env, int flags) { @@ -347,7 +347,7 @@ void RecordDataField::GenParseCode(Output* out_cc, Env* env) { out_cc->println("// Parse \"%s\"", id_->Name()); #if 0 - out_cc->println("DEBUG_MSG(\"%%.6f Parse %s\\n\", network_time());", + out_cc->println("DEBUG_MSG(\"%%.6f Parse %s\\n\", network_time());", id_->Name()); #endif type_->GenPreParsing(out_cc, env); @@ -357,8 +357,8 @@ void RecordDataField::GenParseCode(Output* out_cc, Env* env) { out_cc->println("/* fall through */"); out_cc->dec_indent(); out_cc->println("case %d:", parsing_state_seq()); - out_cc->inc_indent(); out_cc->println("{"); + out_cc->inc_indent(); } type_->GenParseCode(out_cc, env, data, 0); @@ -374,7 +374,7 @@ void RecordDataField::GenParseCode(Output* out_cc, Env* env) { if ( record_type()->incremental_parsing() ) { #if 0 - const ID *dataptr_var = + const ID *dataptr_var = record_type()->parsing_dataptr_var(); ASSERT(dataptr_var); out_cc->println("%s += (%s);", @@ -484,50 +484,46 @@ void RecordPaddingField::GenFieldEnd(Output* out_cc, Env* env, const DataPtr& fi char* padding_var; switch ( ptype_ ) { case PAD_BY_LENGTH: - out_cc->println("if ( (%s) < 0 ) // check for negative pad length", expr_->EvalExpr(out_cc, env)); + out_cc->println("if ( (%s) < 0 ) { // check for negative pad length", expr_->EvalExpr(out_cc, env)); out_cc->inc_indent(); - out_cc->println("{"); out_cc->println("throw binpac::ExceptionInvalidStringLength(\"%s\", %s);", Location(), expr_->EvalExpr(out_cc, env)); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); out_cc->println(""); out_cc->println("const_byteptr const %s = %s + (%s);", env->LValue(end_of_field_dataptr_var), field_begin.ptr_expr(), expr_->EvalExpr(out_cc, env)); out_cc->println("// Checking out-of-bound padding for \"%s\"", field_id_str_.c_str()); - out_cc->println("if ( %s > %s || %s < %s )", env->LValue(end_of_field_dataptr_var), + out_cc->println("if ( %s > %s || %s < %s ) {", env->LValue(end_of_field_dataptr_var), env->RValue(end_of_data), env->LValue(end_of_field_dataptr_var), field_begin.ptr_expr()); out_cc->inc_indent(); - out_cc->println("{"); out_cc->println("throw binpac::ExceptionOutOfBound(\"%s\",", field_id_str_.c_str()); out_cc->println(" (%s), ", expr_->EvalExpr(out_cc, env)); out_cc->println(" (%s) - (%s));", env->RValue(end_of_data), env->LValue(end_of_field_dataptr_var)); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); out_cc->println(""); break; case PAD_TO_OFFSET: out_cc->println("const_byteptr %s = %s + (%s);", env->LValue(end_of_field_dataptr_var), env->RValue(begin_of_data), expr_->EvalExpr(out_cc, env)); - out_cc->println("if ( %s < %s )", env->LValue(end_of_field_dataptr_var), field_begin.ptr_expr()); + out_cc->println("if ( %s < %s ) {", env->LValue(end_of_field_dataptr_var), field_begin.ptr_expr()); out_cc->inc_indent(); - out_cc->println("{"); out_cc->println("// throw binpac::ExceptionInvalidOffset(\"%s\", %s - %s, %s);", id_->LocName(), field_begin.ptr_expr(), env->RValue(begin_of_data), expr_->EvalExpr(out_cc, env)); out_cc->println("%s = %s;", env->LValue(end_of_field_dataptr_var), field_begin.ptr_expr()); - out_cc->println("}"); out_cc->dec_indent(); - out_cc->println("if ( %s > %s )", env->LValue(end_of_field_dataptr_var), env->RValue(end_of_data)); + out_cc->println("}"); + out_cc->println("if ( %s > %s ) {", env->LValue(end_of_field_dataptr_var), env->RValue(end_of_data)); out_cc->inc_indent(); - out_cc->println("{"); out_cc->println("throw binpac::ExceptionOutOfBound(\"%s\",", field_id_str_.c_str()); out_cc->println(" (%s), ", expr_->EvalExpr(out_cc, env)); out_cc->println(" (%s) - (%s));", env->RValue(end_of_data), env->LValue(end_of_field_dataptr_var)); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); break; case PAD_TO_NEXT_WORD: diff --git a/src/pac_strtype.cc b/src/pac_strtype.cc index 99fe7b3..00d9c3d 100644 --- a/src/pac_strtype.cc +++ b/src/pac_strtype.cc @@ -233,12 +233,11 @@ void StringType::GenCheckingCStr(Output* out_cc, Env* env, const DataPtr& data, string str_val = str_->str(); // Compare the string and report error on mismatch - out_cc->println("if ( memcmp(%s, %s, %s) != 0 )", data.ptr_expr(), str_val.c_str(), str_size.c_str()); + out_cc->println("if ( memcmp(%s, %s, %s) != 0 ) {", data.ptr_expr(), str_val.c_str(), str_size.c_str()); out_cc->inc_indent(); - out_cc->println("{"); GenStringMismatch(out_cc, env, data, str_val); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); } void StringType::GenDynamicSizeRegEx(Output* out_cc, Env* env, const DataPtr& data) { @@ -261,13 +260,12 @@ void StringType::GenDynamicSizeRegEx(Output* out_cc, Env* env, const DataPtr& da env->SetEvaluated(string_length_var()); - out_cc->println("if ( %s < 0 )", env->RValue(string_length_var())); + out_cc->println("if ( %s < 0 ) {", env->RValue(string_length_var())); out_cc->inc_indent(); - out_cc->println("{"); string tmp = strfmt("\"%s\"", regex_->str().c_str()); GenStringMismatch(out_cc, env, data, tmp); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); } void StringType::GenDynamicSizeAnyStr(Output* out_cc, Env* env, const DataPtr& data) { diff --git a/src/pac_type.cc b/src/pac_type.cc index b507777..9ea73fd 100644 --- a/src/pac_type.cc +++ b/src/pac_type.cc @@ -319,9 +319,8 @@ void Type::GenBufferConfiguration(Output* out_cc, Env* env) { break; ASSERT(env->GetDataType(buffering_state_id)); - out_cc->println("if ( %s == 0 )", env->RValue(buffering_state_id)); + out_cc->println("if ( %s == 0 ) {", env->RValue(buffering_state_id)); out_cc->inc_indent(); - out_cc->println("{"); if ( attr_length_expr_ ) { // frame_buffer_arg = attr_length_expr_->EvalExpr(out_cc, env); @@ -339,15 +338,14 @@ void Type::GenBufferConfiguration(Output* out_cc, Env* env) { attr_chunked() ? "true" : "false"); out_cc->println("%s = 1;", env->LValue(buffering_state_id)); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); break; case BUFFER_BY_LINE: ASSERT(env->GetDataType(buffering_state_id)); - out_cc->println("if ( %s == 0 )", env->RValue(buffering_state_id)); + out_cc->println("if ( %s == 0 ) {", env->RValue(buffering_state_id)); out_cc->inc_indent(); - out_cc->println("{"); if ( BufferableWithLineBreaker() ) out_cc->println("%s->SetLineBreaker((unsigned char*)%s);", env->LValue(flow_buffer_id), @@ -358,8 +356,8 @@ void Type::GenBufferConfiguration(Output* out_cc, Env* env) { out_cc->println("%s->NewLine();", env->LValue(flow_buffer_id)); out_cc->println("%s = 1;", env->LValue(buffering_state_id)); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); break; default: ASSERT(0); break; @@ -368,12 +366,11 @@ void Type::GenBufferConfiguration(Output* out_cc, Env* env) { void Type::GenPreParsing(Output* out_cc, Env* env) { if ( incremental_input() && IsPointerType() ) { - out_cc->println("if ( ! %s )", env->LValue(value_var())); + out_cc->println("if ( ! %s ) {", env->LValue(value_var())); out_cc->inc_indent(); - out_cc->println("{"); GenNewInstance(out_cc, env); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); } else GenNewInstance(out_cc, env); @@ -425,12 +422,14 @@ void Type::GenParseCode(Output* out_cc, Env* env, const DataPtr& data, int flags GenBoundaryCheck(out_cc, env, data); out_cc->println("{"); + out_cc->inc_indent(); out_cc->println("// Setting %s with &length", env->RValue(end_of_data)); out_cc->println("%s %s = %s + %s;", extern_type_const_byteptr->DataTypeStr().c_str(), env->LValue(end_of_data), data.ptr_expr(), EvalLengthExpr(out_cc, env).c_str()); GenParseCode2(out_cc, env, data, flags); + out_cc->dec_indent(); out_cc->println("}"); } else { @@ -440,16 +439,16 @@ void Type::GenParseCode(Output* out_cc, Env* env, const DataPtr& data, int flags } void Type::GenBufferingLoop(Output* out_cc, Env* env, int flags) { - out_cc->println("while ( ! %s && %s->ready() )", env->LValue(parsing_complete_var()), env->LValue(flow_buffer_id)); + out_cc->println("while ( ! %s && %s->ready() ) {", env->LValue(parsing_complete_var()), + env->LValue(flow_buffer_id)); out_cc->inc_indent(); - out_cc->println("{"); Env buffer_env(env, this); GenParseBuffer(out_cc, &buffer_env, flags); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); } void Type::GenParseBuffer(Output* out_cc, Env* env, int flags) { @@ -488,9 +487,8 @@ void Type::GenParseBuffer(Output* out_cc, Env* env, int flags) { if ( attr_length_expr() ) { ASSERT(buffer_mode() == BUFFER_BY_LENGTH); - out_cc->println("switch ( %s )", env->LValue(buffering_state_id)); + out_cc->println("switch ( %s ) {", env->LValue(buffering_state_id)); out_cc->inc_indent(); - out_cc->println("{"); out_cc->println("case 0:"); out_cc->inc_indent(); GenBufferConfiguration(out_cc, env); @@ -499,34 +497,33 @@ void Type::GenParseBuffer(Output* out_cc, Env* env, int flags) { out_cc->dec_indent(); out_cc->println("case 1:"); - out_cc->inc_indent(); out_cc->println("{"); + out_cc->inc_indent(); out_cc->println("%s = 2;", env->LValue(buffering_state_id)); Env frame_length_env(env, this); out_cc->println("%s->GrowFrame(%s);", env->LValue(flow_buffer_id), attr_length_expr_->EvalExpr(out_cc, &frame_length_env)); + out_cc->dec_indent(); out_cc->println("}"); out_cc->println("break;"); - out_cc->dec_indent(); out_cc->println("case 2:"); out_cc->inc_indent(); out_cc->println("BINPAC_ASSERT(%s->ready());", env->RValue(flow_buffer_id)); - out_cc->println("if ( %s->ready() )", env->RValue(flow_buffer_id)); + out_cc->println("if ( %s->ready() ) {", env->RValue(flow_buffer_id)); out_cc->inc_indent(); - out_cc->println("{"); Env parse_env(env, this); GenParseCode2(out_cc, &parse_env, data, 0); out_cc->println("BINPAC_ASSERT(%s);", parsing_complete(env).c_str()); out_cc->println("%s = 0;", env->LValue(buffering_state_id)); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); out_cc->println("break;"); @@ -538,8 +535,8 @@ void Type::GenParseBuffer(Output* out_cc, Env* env, int flags) { out_cc->println("break;"); out_cc->dec_indent(); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); } else if ( attr_restofflow_ ) { out_cc->println("BINPAC_ASSERT(%s->eof());", env->RValue(flow_buffer_id)); @@ -589,9 +586,8 @@ void Type::GenParseCode3(Output* out_cc, Env* env, const DataPtr& data, int flag DoGenParseCode(out_cc, env, data, flags); if ( incremental_input() ) { - out_cc->println("if ( %s )", parsing_complete(env).c_str()); + out_cc->println("if ( %s ) {", parsing_complete(env).c_str()); out_cc->inc_indent(); - out_cc->println("{"); } if ( ! fields_->empty() ) { @@ -614,8 +610,8 @@ void Type::GenParseCode3(Output* out_cc, Env* env, const DataPtr& data, int flag } if ( incremental_input() ) { - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); } if ( value_var() ) @@ -628,12 +624,11 @@ void Type::GenParseCode3(Output* out_cc, Env* env, const DataPtr& data, int flag Expr* enforce = *i; const char* enforce_expr = enforce->EvalExpr(out_cc, env); out_cc->println("// Evaluate '&enforce' attribute"); - out_cc->println("if (!%s)", enforce_expr); + out_cc->println("if (!%s) {", enforce_expr); out_cc->inc_indent(); - out_cc->println("{"); out_cc->println("throw binpac::ExceptionEnforceViolation(\"%s\");", data_id_str_.c_str()); - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); } } diff --git a/src/pac_type.h b/src/pac_type.h index faae7e2..f2cf137 100644 --- a/src/pac_type.h +++ b/src/pac_type.h @@ -111,7 +111,7 @@ class Type : public Object, public DataDepElement { string DataTypeConstRefStr() const { string data_type = DataTypeStr(); if ( ! IsPointerType() && ! IsNumericType() ) - data_type += " const &"; + data_type += " const&"; return data_type; } diff --git a/src/pac_typedecl.cc b/src/pac_typedecl.cc index 5e48c9e..5488490 100644 --- a/src/pac_typedecl.cc +++ b/src/pac_typedecl.cc @@ -114,10 +114,9 @@ void TypeDecl::GenCode(Output* out_h, Output* out_cc) { else out_h->print(", public %s", i->c_str()); } - out_h->print("\n"); + out_h->println(" {"); // Public members - out_h->println("{"); out_h->println("public:"); out_h->inc_indent(); @@ -166,18 +165,16 @@ void TypeDecl::GenConstructorFunc(Output* out_h, Output* out_cc) { out_h->println("%s;", proto.c_str()); - out_cc->println("%s::%s", class_name().c_str(), proto.c_str()); + out_cc->println("%s::%s {", class_name().c_str(), proto.c_str()); out_cc->inc_indent(); - out_cc->println("{"); - // GenParamAssignments(params_, out_cc, env_); type_->GenInitCode(out_cc, env_); GenInitCode(out_cc); - out_cc->println("}\n"); out_cc->dec_indent(); + out_cc->println("}\n"); } void TypeDecl::GenDestructorFunc(Output* out_h, Output* out_cc) { @@ -185,15 +182,14 @@ void TypeDecl::GenDestructorFunc(Output* out_h, Output* out_cc) { out_h->println("%s;", proto.c_str()); - out_cc->println("%s::%s", class_name().c_str(), proto.c_str()); + out_cc->println("%s::%s {", class_name().c_str(), proto.c_str()); out_cc->inc_indent(); - out_cc->println("{"); GenCleanUpCode(out_cc); type_->GenCleanUpCode(out_cc, env_); - out_cc->println("}\n"); out_cc->dec_indent(); + out_cc->println("}\n"); } string TypeDecl::ParseFuncPrototype(Env* env) { @@ -283,8 +279,7 @@ void TypeDecl::GenParseFunc(Output* out_h, Output* out_cc) { env->SetEvaluated(end_of_data); } - string proto; - proto = ParseFuncPrototype(env); + string proto = ParseFuncPrototype(env); #if 0 if ( func_type == PARSE ) @@ -303,9 +298,8 @@ void TypeDecl::GenParseFunc(Output* out_h, Output* out_cc) { out_h->println(proto.c_str(), "", ";"); string tmp = strfmt("%s::", class_name().c_str()); - out_cc->println(proto.c_str(), tmp.c_str(), ""); + out_cc->println(proto.c_str(), tmp.c_str(), " {"); out_cc->inc_indent(); - out_cc->println("{"); DataPtr data(env, nullptr, 0); @@ -314,8 +308,8 @@ void TypeDecl::GenParseFunc(Output* out_h, Output* out_cc) { type_->GenParseCode(out_cc, env, data, 0); GenParsingEnd(out_cc, env, data); - out_cc->println("}\n"); out_cc->dec_indent(); + out_cc->println("}\n"); } void TypeDecl::GenInitialBufferLengthFunc(Output* out_h, Output* out_cc) { diff --git a/src/pac_withinput.cc b/src/pac_withinput.cc index 50b8a2b..6eb43f0 100644 --- a/src/pac_withinput.cc +++ b/src/pac_withinput.cc @@ -39,9 +39,8 @@ void WithInputField::GenParseCode(Output* out_cc, Env* env) { if ( type_->attr_if_expr() ) { // A conditional field env->Evaluate(out_cc, type_->has_value_var()); - out_cc->println("if ( %s )", env->RValue(type_->has_value_var())); + out_cc->println("if ( %s ) {", env->RValue(type_->has_value_var())); out_cc->inc_indent(); - out_cc->println("{"); } else out_cc->println("{"); @@ -52,8 +51,8 @@ void WithInputField::GenParseCode(Output* out_cc, Env* env) { type_->GenParseCode(out_cc, &field_env, input()->GenDataBeginEnd(out_cc, &field_env), 0); if ( type_->attr_if_expr() ) { - out_cc->println("}"); out_cc->dec_indent(); + out_cc->println("}"); } else out_cc->println("}");