Computer Laboratory

Names.cpp
Go to the documentation of this file.
1 
2 /*
3  * Copyright (c) 2013 Jonathan Anderson
4  * All rights reserved.
5  *
6  * This software was developed by SRI International and the University of
7  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
8  * ("CTSRD"), as part of the DARPA CRASH research programme.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include "tesla.pb.h"
33 #include "Names.h"
34 
35 #include "llvm/ADT/Twine.h"
36 
37 #include <string>
38 #include <sstream>
39 
40 using llvm::Twine;
41 
42 
43 std::string tesla::ArgString(const Argument* A) {
44  if (A == NULL)
45  return "NULL";
46 
47  switch (A->type()) {
48  case Argument::Constant:
49  return A->name();
50 
51  case Argument::Variable:
52  return (Twine()
53  + Twine(A->index())
54  + "('"
55  + A->name()
56  + "')"
57  ).str();
58 
59  case Argument::Any:
60  return "<anything>";
61 
62  case Argument::Indirect:
63  assert(A->has_indirection());
64  return "*" + ShortName(&A->indirection());
65 
66  case Argument::Field:
67  assert(A->has_field());
68  return ShortName(&A->field().base()) + "." + A->field().name();
69  }
70 }
71 
72 static std::string ConstantName(const tesla::Argument* A) {
73  assert(A->type() == tesla::Argument::Constant);
74 
75  if (A->has_name())
76  return A->name();
77 
78  else
79  return Twine(A->value()).str();
80 }
81 
82 std::string tesla::ShortName(const Argument* A) {
83  if (A == NULL)
84  return "X";
85 
86  switch (A->type()) {
87  case Argument::Constant:
88  return ConstantName(A);
89 
90  case Argument::Variable:
91  return A->name();
92 
93  case Argument::Any:
94  return "X";
95 
96  case Argument::Indirect:
97  assert(A->has_indirection());
98  return "*" + ShortName(&A->indirection());
99 
100  case Argument::Field:
101  assert(A->has_field());
102  return ShortName(&A->field().base()) + "." + A->field().name();
103  }
104 }
105 
106 std::string tesla::DotName(const Argument* A) {
107  const static std::string Star = "&#8902;";
108  const static std::string Asterisk = "&#x2217;";
109 
110  if (A == NULL)
111  return Star;
112 
113  switch (A->type()) {
114  case Argument::Constant:
115  return ConstantName(A);
116 
117  case Argument::Variable:
118  return A->name();
119 
120  case Argument::Any:
121  return Star;
122 
123  case Argument::Indirect:
124  assert(A->has_indirection());
125  return Asterisk + DotName(&A->indirection());
126 
127  case Argument::Field:
128  assert(A->has_field());
129  return ShortName(&A->field().base()) + "." + A->field().name();
130  }
131 }
132 
133 std::string tesla::ShortName(const Identifier& ID) {
134  if (ID.has_name())
135  return ID.name();
136 
137  return ShortName(ID.location());
138 }
139 
140 std::string tesla::ShortName(const Location& Loc) {
141  return (Twine()
142  + Loc.filename()
143  + ":"
144  + Twine(Loc.line())
145  + "#"
146  + Twine(Loc.counter())
147  ).str();
148 }
149 
150 std::string tesla::InstanceName(const ReferenceVector& Refs, bool PlainAscii) {
151 
152  std::stringstream InstanceName;
153 
154  InstanceName << "(";
155 
156  for (auto i = Refs.begin(); i != Refs.end(); ) {
157  InstanceName << (PlainAscii ? ShortName(*i) : DotName(*i));
158 
159  if (++i != Refs.end())
160  InstanceName << ",";
161  }
162 
163  InstanceName << ")";
164 
165  return InstanceName.str();
166 }