1//===- InputSectDesc.cpp --------------------------------------------------===//
2//
3//                     The MCLinker Project
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#include "mcld/Script/InputSectDesc.h"
10
11#include "mcld/Script/WildcardPattern.h"
12#include "mcld/Support/raw_ostream.h"
13#include "mcld/LinkerScript.h"
14#include "mcld/Module.h"
15
16#include <llvm/Support/Casting.h>
17
18namespace mcld {
19
20//===----------------------------------------------------------------------===//
21// InputSectDesc
22//===----------------------------------------------------------------------===//
23InputSectDesc::InputSectDesc(KeepPolicy pPolicy,
24                             const Spec& pSpec,
25                             const OutputSectDesc& pOutputDesc)
26    : ScriptCommand(ScriptCommand::INPUT_SECT_DESC),
27      m_KeepPolicy(pPolicy),
28      m_Spec(pSpec),
29      m_OutputSectDesc(pOutputDesc) {
30}
31
32InputSectDesc::~InputSectDesc() {
33}
34
35void InputSectDesc::dump() const {
36  if (m_KeepPolicy == Keep)
37    mcld::outs() << "KEEP (";
38
39  assert(m_Spec.hasFile());
40  if (m_Spec.file().sortPolicy() == WildcardPattern::SORT_BY_NAME)
41    mcld::outs() << "SORT (";
42
43  mcld::outs() << m_Spec.file().name();
44
45  if (m_Spec.hasSections()) {
46    mcld::outs() << "(";
47
48    if (m_Spec.hasExcludeFiles()) {
49      mcld::outs() << "EXCLUDE_FILE (";
50      for (StringList::const_iterator it = m_Spec.excludeFiles().begin(),
51                                      ie = m_Spec.excludeFiles().end();
52           it != ie;
53           ++it) {
54        mcld::outs() << (*it)->name() << " ";
55      }
56      mcld::outs() << ")";
57    }
58
59    if (m_Spec.hasSections()) {
60      for (StringList::const_iterator it = m_Spec.sections().begin(),
61                                      ie = m_Spec.sections().end();
62           it != ie;
63           ++it) {
64        assert((*it)->kind() == StrToken::Wildcard);
65        WildcardPattern* wildcard = llvm::cast<WildcardPattern>(*it);
66
67        switch (wildcard->sortPolicy()) {
68          case WildcardPattern::SORT_BY_NAME:
69            mcld::outs() << "SORT (";
70            break;
71          case WildcardPattern::SORT_BY_ALIGNMENT:
72            mcld::outs() << "SORT_BY_ALIGNMENT (";
73            break;
74          case WildcardPattern::SORT_BY_NAME_ALIGNMENT:
75            mcld::outs() << "SORT_BY_NAME_ALIGNMENT (";
76            break;
77          case WildcardPattern::SORT_BY_ALIGNMENT_NAME:
78            mcld::outs() << "SORT_BY_ALIGNMENT_NAME (";
79            break;
80          default:
81            break;
82        }
83
84        mcld::outs() << wildcard->name() << " ";
85
86        if (wildcard->sortPolicy() != WildcardPattern::SORT_NONE)
87          mcld::outs() << ")";
88      }
89    }
90    mcld::outs() << ")";
91  }
92
93  if (m_Spec.file().sortPolicy() == WildcardPattern::SORT_BY_NAME)
94    mcld::outs() << ")";
95
96  if (m_KeepPolicy == Keep)
97    mcld::outs() << ")";
98
99  mcld::outs() << "\n";
100}
101
102void InputSectDesc::activate(Module& pModule) {
103  pModule.getScript().sectionMap().insert(*this, m_OutputSectDesc);
104}
105
106}  // namespace mcld
107