<commit_before>#include "insertThisParameters.h"
#include "expr.h"
#include "stmt.h"
#include "stringutil.h"
#include "symtab.h"

void InsertThisParameters::preProcessStmt(Stmt* stmt) {
  DefStmt* def_stmt = dynamic_cast<DefStmt*>(stmt);

  if (!def_stmt) {
    return;
  }

  FnSymbol* fn = def_stmt->fnDef();

  if (!fn) {
    /*** mangle type names in class types ***/
    if (def_stmt->definesTypes()) {
      for (DefExpr* defExpr = def_stmt->defExprls->first();
           defExpr;
           defExpr = def_stmt->defExprls->next()) {
        TypeSymbol* type_sym = dynamic_cast<TypeSymbol*>(defExpr->sym);
        if (StructuralType* classType = dynamic_cast<StructuralType*>(type_sym->type)) {
          forv_Vec(TypeSymbol, type, classType->types) {
            type->cname = glomstrings(4, "_", classType->symbol->cname, "_", type->cname);
          }
        }
      }
    }
    return;
  }

  /***
   *** Resolve typeBinding
   ***/
  if (dynamic_cast<UnresolvedSymbol*>(fn->typeBinding)) {
    Symbol* typeBindingSymbol = Symboltable::lookup(fn->typeBinding->name);
    if (dynamic_cast<TypeSymbol*>(typeBindingSymbol)) {
      Type* typeBinding = typeBindingSymbol->type;
      fn->typeBinding = typeBindingSymbol;
      if (!fn->isConstructor) {
        fn->method_type = SECONDARY_METHOD;
      }
      typeBinding->methods.add(fn);
    } else {
      USR_FATAL(fn, "Function is not bound to type");
    }
  }
  
  /***
   *** Mangle code-generation name and insert this as first parameter
   ***/
  
  if (TypeSymbol* typeSym = dynamic_cast<TypeSymbol*>(fn->typeBinding)) {
    fn->cname = glomstrings(4, "_", typeSym->cname, "_", fn->cname);
    if (fn->isConstructor) {
      SymScope* saveScope = Symboltable::setCurrentScope(fn->body->body->only()->parentScope);
      DefStmt* this_decl = Symboltable::defineSingleVarDefStmt(copystring("this"),
                                                               typeSym->type,
                                                               NULL,
                                                               VAR_NORMAL,
                                                               VAR_VAR);
      fn->_this = this_decl->varDef();
      fn->retType = typeSym->type;
      dynamic_cast<VarSymbol*>(fn->_this)->noDefaultInit = true;
      fn->body->body->insertAtHead(this_decl);
      if (dynamic_cast<ClassType*>(typeSym->type)) {
        char* description = glomstrings(2, "instance of class ", typeSym->name);
        AList<Expr>* alloc_args = new AList<Expr>(new IntLiteral("1", 1));
        alloc_args->insertAtTail(new SizeofExpr(new Variable(fn->_this)));
        alloc_args->insertAtTail(new StringLiteral(description));
        Symbol* alloc_sym = Symboltable::lookupInternal("_chpl_malloc");
        Expr* alloc_call = new FnCall(new Variable(alloc_sym), alloc_args);
        Expr* alloc_lhs = new Variable(fn->_this);
        Expr* alloc_rhs = new CastLikeExpr(new Variable(fn->_this), alloc_call);
        Expr* alloc_expr = new AssignOp(GETS_NORM, alloc_lhs, alloc_rhs);
        Stmt* alloc_stmt = new ExprStmt(alloc_expr);
        this_decl->insertAfter(alloc_stmt);
      }
      fn->body->body->insertAtTail(new ReturnStmt(new Variable(fn->_this)));
      Symboltable::setCurrentScope(saveScope);
<commit_msg>A small change in which AList::only() was being used on statement lists that weren't guaranteed to just have a single element in them.<commit_after>#include "insertThisParameters.h"
#include "expr.h"
#include "stmt.h"
#include "stringutil.h"
#include "symtab.h"

void InsertThisParameters::preProcessStmt(Stmt* stmt) {
  DefStmt* def_stmt = dynamic_cast<DefStmt*>(stmt);

  if (!def_stmt) {
    return;
  }

  FnSymbol* fn = def_stmt->fnDef();

  if (!fn) {
    /*** mangle type names in class types ***/
    if (def_stmt->definesTypes()) {
      for (DefExpr* defExpr = def_stmt->defExprls->first();
           defExpr;
           defExpr = def_stmt->defExprls->next()) {
        TypeSymbol* type_sym = dynamic_cast<TypeSymbol*>(defExpr->sym);
        if (StructuralType* classType = dynamic_cast<StructuralType*>(type_sym->type)) {
          forv_Vec(TypeSymbol, type, classType->types) {
            type->cname = glomstrings(4, "_", classType->symbol->cname, "_", type->cname);
          }
        }
      }
    }
    return;
  }

  /***
   *** Resolve typeBinding
   ***/
  if (dynamic_cast<UnresolvedSymbol*>(fn->typeBinding)) {
    Symbol* typeBindingSymbol = Symboltable::lookup(fn->typeBinding->name);
    if (dynamic_cast<TypeSymbol*>(typeBindingSymbol)) {
      Type* typeBinding = typeBindingSymbol->type;
      fn->typeBinding = typeBindingSymbol;
      if (!fn->isConstructor) {
        fn->method_type = SECONDARY_METHOD;
      }
      typeBinding->methods.add(fn);
    } else {
      USR_FATAL(fn, "Function is not bound to type");
    }
  }
  
  /***
   *** Mangle code-generation name and insert this as first parameter
   ***/
  
  if (TypeSymbol* typeSym = dynamic_cast<TypeSymbol*>(fn->typeBinding)) {
    fn->cname = glomstrings(4, "_", typeSym->cname, "_", fn->cname);
    if (fn->isConstructor) {
      SymScope* saveScope = Symboltable::setCurrentScope(fn->body->body->first()->parentScope);
      fn->body->body->reset(); // reset iteration
      DefStmt* this_decl = Symboltable::defineSingleVarDefStmt(copystring("this"),
                                                               typeSym->type,
                                                               NULL,
                                                               VAR_NORMAL,
                                                               VAR_VAR);
      fn->_this = this_decl->varDef();
      fn->retType = typeSym->type;
      dynamic_cast<VarSymbol*>(fn->_this)->noDefaultInit = true;
      fn->body->body->insertAtHead(this_decl);
      if (dynamic_cast<ClassType*>(typeSym->type)) {
        char* description = glomstrings(2, "instance of class ", typeSym->name);
        AList<Expr>* alloc_args = new AList<Expr>(new IntLiteral("1", 1));
        alloc_args->insertAtTail(new SizeofExpr(new Variable(fn->_this)));
        alloc_args->insertAtTail(new StringLiteral(description));
        Symbol* alloc_sym = Symboltable::lookupInternal("_chpl_malloc");
        Expr* alloc_call = new FnCall(new Variable(alloc_sym), alloc_args);
        Expr* alloc_lhs = new Variable(fn->_this);
        Expr* alloc_rhs = new CastLikeExpr(new Variable(fn->_this), alloc_call);
        Expr* alloc_expr = new AssignOp(GETS_NORM, alloc_lhs, alloc_rhs);
        Stmt* alloc_stmt = new ExprStmt(alloc_expr);
        this_decl->insertAfter(alloc_stmt);
      }
      fn->body->body->insertAtTail(new ReturnStmt(new Variable(fn->_this)));
      Symboltable::setCurrentScope(saveScope);

{'fraction_non_alphanumeric': 0.13865048831015092, 'fraction_numerical': 0.001479727730097662, 'mean_word_length': 2.5856763925729442, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 130, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6335345', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1998, 'n_words': 367}