All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.joern.fuzzyc2cpg.parser.shared.builders.IdentifierDeclBuilder Maven / Gradle / Ivy

There is a newer version: 1.1.911
Show newest version
package io.joern.fuzzyc2cpg.parser.shared.builders;

import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTree;

import io.joern.fuzzyc2cpg.ast.declarations.IdentifierDecl;
import io.joern.fuzzyc2cpg.ast.declarations.IdentifierDeclType;
import io.joern.fuzzyc2cpg.ast.expressions.Identifier;
import io.joern.fuzzyc2cpg.parser.AstNodeFactory;
import io.joern.fuzzyc2cpg.parser.ParseTreeUtils;
import io.joern.fuzzyc2cpg.parser.shared.InitDeclContextWrapper;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class IdentifierDeclBuilder extends TemplateAstBuilder {

  @Override
  public void createNew(ParserRuleContext ctx) {
    item = new IdentifierDecl();
    AstNodeFactory.initializeFromContext(item, ctx);
  }

  public void setType(InitDeclContextWrapper decl_ctx,
      ParserRuleContext typeName) {
    String baseType = "";
    if (typeName != null) {
      baseType = ParseTreeUtils.childTokenString(typeName);
    }
    String completeType = baseType;
    if (decl_ctx.ptrs() != null) {
      completeType += " "
          + ParseTreeUtils.childTokenString(decl_ctx.ptrs());
    }
    if (decl_ctx.func_ptrs() != null) {
      completeType += " ( "
          + ParseTreeUtils.childTokenString(decl_ctx.func_ptrs()) + " )";
    }
    if (decl_ctx.type_suffix() != null) {
      completeType += " "
          + ParseTreeUtils.childTokenString(decl_ctx.type_suffix());
    }

    // baseType = completeType for function pointers
    if (decl_ctx.func_ptrs() != null) {
      baseType = completeType;
    }

    IdentifierDeclType newType = new IdentifierDeclType();
    AstNodeFactory.initializeFromContext(newType,
        decl_ctx.getWrappedObject());
    newType.baseType = baseType;
    newType.completeType = completeType;
    item.addChild(newType);
  }

  public void setName(InitDeclContextWrapper decl_ctx) {
    ParserRuleContext identifier = decl_ctx.identifier();
    Identifier newName = new Identifier();
    AstNodeFactory.initializeFromContext(newName, identifier);
    item.addChild(newName);
  }

  public List getDeclarations(ParserRuleContext decl_list,
      ParserRuleContext typeName) {
    List declarations = new LinkedList<>();
    InitDeclContextWrapper decl_ctx;
    for (ParseTree parseTree : decl_list.children) {
      decl_ctx = new InitDeclContextWrapper(parseTree);
      // for ','s
      if (decl_ctx.getWrappedObject() == null) {
        continue;
      }
      createNew(decl_ctx.getWrappedObject());
      setType(decl_ctx, typeName);
      setName(decl_ctx);
      declarations.add(getItem());
    }
    return declarations;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy