122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//===- InputBuilder.h -----------------------------------------------------===//
222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//
322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//                     The MCLinker Project
422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//
522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao// This file is distributed under the University of Illinois Open Source
622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao// License. See LICENSE.TXT for details.
722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//
822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//===----------------------------------------------------------------------===//
937b74a387bb3993387029859c2d9d051c41c724eStephen Hines#ifndef MCLD_MC_INPUTBUILDER_H_
1037b74a387bb3993387029859c2d9d051c41c724eStephen Hines#define MCLD_MC_INPUTBUILDER_H_
1122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
1237b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/InputTree.h"
1337b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/MC/Input.h"
1437b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include "mcld/Support/FileHandle.h"
1522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
1637b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include <stack>
1737b74a387bb3993387029859c2d9d051c41c724eStephen Hines#include <string>
1822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
1922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaonamespace mcld {
2022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
2137b74a387bb3993387029859c2d9d051c41c724eStephen Hinesclass AttrConstraint;
2222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaoclass ContextFactory;
2337b74a387bb3993387029859c2d9d051c41c724eStephen Hinesclass InputFactory;
2437b74a387bb3993387029859c2d9d051c41c724eStephen Hinesclass LinkerConfig;
2522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liaoclass MemoryAreaFactory;
2622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
2722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao/** \class InputBuilder
2822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao *  \brief InputBuilder recieves InputActions and build the InputTree.
2922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao *
3022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao *  InputBuilder build input tree and inputs.
3122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao */
3237b74a387bb3993387029859c2d9d051c41c724eStephen Hinesclass InputBuilder {
3337b74a387bb3993387029859c2d9d051c41c724eStephen Hines public:
3422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  explicit InputBuilder(const LinkerConfig& pConfig);
3522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
3622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  InputBuilder(const LinkerConfig& pConfig,
3722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao               InputFactory& pInputFactory,
3822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao               ContextFactory& pContextFactory,
3922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao               MemoryAreaFactory& pMemoryFactory,
4022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao               bool pDelegate = true);
4122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
4222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  virtual ~InputBuilder();
4322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
4422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // -----  input tree operations  ----- //
4522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  const InputTree& getCurrentTree() const;
4637b74a387bb3993387029859c2d9d051c41c724eStephen Hines  InputTree& getCurrentTree();
4722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
4822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  void setCurrentTree(InputTree& pInputTree);
4922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
5022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // -----  root of input tree  ----- //
5122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  const InputTree::iterator& getCurrentNode() const { return m_Root; }
5237b74a387bb3993387029859c2d9d051c41c724eStephen Hines  InputTree::iterator& getCurrentNode() { return m_Root; }
5322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
5437b74a387bb3993387029859c2d9d051c41c724eStephen Hines  template <InputTree::Direction DIRECTION>
5522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  InputTree& createNode(const std::string& pName,
5622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                        const sys::fs::Path& pPath,
5722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                        unsigned int pType = Input::Unknown);
5822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
5922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  // -----  input operations  ----- //
6022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Input* createInput(const std::string& pName,
6122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                     const sys::fs::Path& pPath,
6222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                     unsigned int pType = Input::Unknown,
6322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                     off_t pFileOffset = 0);
6422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
6522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  bool setContext(Input& pInput, bool pCheck = true);
6622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
6722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  bool setMemory(Input& pInput,
6822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao                 FileHandle::OpenMode pMode,
6937b74a387bb3993387029859c2d9d051c41c724eStephen Hines                 FileHandle::Permission pPerm);
7022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
7122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  bool setMemory(Input& pInput, void* pMemBuffer, size_t pSize);
7222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
7322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  InputTree& enterGroup();
7422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
7522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  InputTree& exitGroup();
7622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
7722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  bool isInGroup() const;
7822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
7922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  const AttrConstraint& getConstraint() const;
8022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
8122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  const AttributeProxy& getAttributes() const;
8237b74a387bb3993387029859c2d9d051c41c724eStephen Hines  AttributeProxy& getAttributes();
8322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
8437b74a387bb3993387029859c2d9d051c41c724eStephen Hines private:
8522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  const LinkerConfig& m_Config;
8622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
8722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  InputFactory* m_pInputFactory;
8822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  MemoryAreaFactory* m_pMemFactory;
8922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  ContextFactory* m_pContextFactory;
9022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
9122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  InputTree* m_pCurrentTree;
9222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  InputTree::Mover* m_pMove;
9322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  InputTree::iterator m_Root;
9422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  std::stack<InputTree::iterator> m_ReturnStack;
9522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
9622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  bool m_bOwnFactory;
9722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao};
9822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
9922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//===----------------------------------------------------------------------===//
10022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao// Template implement
10122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao//===----------------------------------------------------------------------===//
10237b74a387bb3993387029859c2d9d051c41c724eStephen Hinestemplate <>
10337b74a387bb3993387029859c2d9d051c41c724eStephen Hinesinline InputTree& InputBuilder::createNode<InputTree::Inclusive>(
10437b74a387bb3993387029859c2d9d051c41c724eStephen Hines    const std::string& pName,
10537b74a387bb3993387029859c2d9d051c41c724eStephen Hines    const sys::fs::Path& pPath,
10637b74a387bb3993387029859c2d9d051c41c724eStephen Hines    unsigned int pType) {
10737b74a387bb3993387029859c2d9d051c41c724eStephen Hines  assert(m_pCurrentTree != NULL && m_pMove != NULL);
10822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
10922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Input* input = createInput(pName, pPath, pType);
11022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  m_pCurrentTree->insert(m_Root, *m_pMove, *input);
11122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  m_pMove->move(m_Root);
11222add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  m_pMove = &InputTree::Downward;
11322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
11422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  return *m_pCurrentTree;
11522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
11622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
11737b74a387bb3993387029859c2d9d051c41c724eStephen Hinestemplate <>
11837b74a387bb3993387029859c2d9d051c41c724eStephen Hinesinline InputTree& InputBuilder::createNode<InputTree::Positional>(
11937b74a387bb3993387029859c2d9d051c41c724eStephen Hines    const std::string& pName,
12037b74a387bb3993387029859c2d9d051c41c724eStephen Hines    const sys::fs::Path& pPath,
12137b74a387bb3993387029859c2d9d051c41c724eStephen Hines    unsigned int pType) {
12237b74a387bb3993387029859c2d9d051c41c724eStephen Hines  assert(m_pCurrentTree != NULL && m_pMove != NULL);
12322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
12422add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  Input* input = createInput(pName, pPath, pType);
12522add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  m_pCurrentTree->insert(m_Root, *m_pMove, *input);
12622add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  m_pMove->move(m_Root);
12722add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  m_pMove = &InputTree::Afterward;
12822add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
12922add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao  return *m_pCurrentTree;
13022add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao}
13122add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
13237b74a387bb3993387029859c2d9d051c41c724eStephen Hines}  // namespace mcld
13322add6ff3426df1a85089fe6a6e1597ee3b6f300Shih-wei Liao
13437b74a387bb3993387029859c2d9d051c41c724eStephen Hines#endif  // MCLD_MC_INPUTBUILDER_H_
135