[r368]: cmajor++ / Cm / Cm.Core / Name.cpp Maximize Restore History

Download this file

Name.cpp    242 lines (223 with data), 7.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*========================================================================
Copyright (c) 2012-2013 Seppo Laakko
http://sourceforge.net/projects/cmajor/
Distributed under the GNU General Public License, version 3 (GPLv3).
(See accompanying LICENSE.txt or http://www.gnu.org/licenses/gpl.html)
========================================================================*/
#include <Cm.Core/Name.hpp>
#include <Cm.Core/Scope.hpp>
#include <Cm.Core/Error.hpp>
#include <Cm.Core/Assert.hpp>
#include <Cm.Core/Namespace.hpp>
#include <Cm.Core/Expression.hpp>
#include <Cm.Core/Template.hpp>
#include <Cm.Core/Function.hpp>
#include <Cm.Core/Util.hpp>
#include <Cm.Core/Clone.hpp>
#include <Cm.Core/ConversionTable.hpp>
#include <Cm.Core/BasicTypeFun.hpp>
namespace Cm { namespace Core {
using Soul::Util::Split;
Name::Name(const std::string& name_, const Position& pos_, const ScopePtr& enclosingScope_, const ScopePtr& fileScope_, bool isTypeName_):
Object(name_, pos_, enclosingScope_, fileScope_), isTypeName(isTypeName_)
{
}
void Name::Dispose()
{
if (!Disposed())
{
Object::Dispose();
}
}
ObjectPtr Name::Clone(CloneContext& context) const
{
return ObjectPtr(new Cm::Core::Name(Object::Name(), Pos(), context.CurrentScope(), FileScope(), isTypeName));
}
std::string Name::FullName() const
{
if (isTypeName)
{
std::string ns = EnclosingScope()->GetNamespaceScope()->FullName();
std::string fullName = ns.empty() ? Object::Name() : ns + "." + Object::Name();
SetFullNameCache(fullName);
return fullName;
}
else
{
std::string fullName = Object::Name();
SetFullNameCache(fullName);
return fullName;
}
}
std::string Name::DocName() const
{
ObjectPtr to = linkedTo.lock();
if (to && to->IsTemplateName())
{
return to->DocName();
}
return Object::Name();
}
void Name::TypeCheckName(TypeCheckContext& context)
{
Object::TypeCheckName(context);
TypeCheckException ex;
ObjectPtr to = linkedTo.lock();
if (!to && FileScope())
{
to = FileScope()->GetObject(Object::Name(), Lookup::this_scope, Pos(), ex);
}
if (!to)
{
to = EnclosingScope()->GetObject(Object::Name(), Lookup::this_enclosing_and_base_scope, Pos());
}
if (!to && context.GetFileScope())
{
to = context.GetFileScope()->GetObject(Object::Name(), Lookup::this_scope, Pos(), ex);
}
if (!to)
{
ScopePtr currentFileScope = GetCurrentFileScope();
if (currentFileScope)
{
to = currentFileScope->GetObject(Object::Name(), Lookup::this_scope, Pos(), ex);
}
}
if (!to)
{
TemplateParameterList templateParameters = context.GetTemplateParameters();
int n = templateParameters.size();
for (int i = 0; i < n; ++i)
{
TemplateParameterPtr param = templateParameters[i];
if (param->Name() == Object::Name())
{
to = param;
break;
}
}
}
if (!to)
{
ScopePtr paramScope = context.GetParamScope();
if (paramScope)
{
to = paramScope->GetObject(Object::Name(), Lookup::this_enclosing_and_base_scope, Pos());
}
}
if (context.NoThrow())
{
linkedTo = to;
return;
}
if (!to && !ex.IsDefault())
{
throw ex;
}
if (!to)
{
throw TypeCheckException(NAME_NOT_FOUND, "name '" + Object::Name() + "' not found", Pos());
}
linkedTo = to;
to->SetOriginalPos(Pos());
to->TypeCheck(context);
}
bool Name::HasTypeCheckReplacement(TypeCheckContext& context) const
{
return linkedTo.lock() != ObjectPtr();
}
ObjectPtr Name::GetTypeCheckReplacement(TypeCheckContext& context) const
{
return linkedTo.lock();
}
void Name::TypeCheckSignature(TypeCheckContext& context)
{
Object::TypeCheckSignature(context);
ObjectPtr to = linkedTo.lock();
if (!to)
{
TypeCheckName(context);
}
to = linkedTo.lock();
if (to->EnclosingScope()->GetKind() == ScopeKind::class_ && to->IsMemberVariable() && !to->IsStatic())
{
to = PostfixExpressionPtr(new PostfixExpression(to->Pos(), to->EnclosingScope(), to->FileScope(), to, PostfixOpPtr(new ThisMember(to->Pos(), to->EnclosingScope(), to->FileScope(), to->Name()))));
to->SetFunction(GetFunction());
to->SetCompound(Compound());
to->TypeCheck(context);
GetFunction()->AddNewObject(to);
linkedTo = to;
}
else
{
to->SetFunction(GetFunction());
to->SetCompound(Compound());
}
}
void Name::TypeCheckBody(TypeCheckContext& context)
{
Object::TypeCheckBody(context);
ObjectPtr to = linkedTo.lock();
to->TypeCheck(context);
}
bool Name::Bind(const TypePtr& argumentType, const ScopePtr& scope, ExpressionList& templateArguments, int& unifications, FunctionPtr& conversion, TypePtr& boundType)
{
#if !defined(NDEBUG) || defined(ASSERTIONS)
const char* fullName = GetFullNameFor(shared_from_this());
#endif
ObjectPtr parameterObject = scope->GetObject(Object::Name(), Lookup::this_enclosing_and_base_scope, Pos());
if (parameterObject->IsType())
{
TypePtr parameterType = parameterObject->GetType();
if (IsConvertible(argumentType, parameterType))
{
conversion = ConversionTable::Instance()->GetConversion(argumentType->FullName(), parameterType->FullName());
return true;
}
else
{
return false;
}
}
else if (parameterObject->IsParameter()) // unbound template parameter
{
ParameterPtr templateParameter = std::static_pointer_cast<Parameter>(parameterObject);
int templateParameterIndex = templateParameter->GetIndex();
CM_CORE_ASSERT(templateParameterIndex >= 0 && templateParameterIndex < templateArguments.size(), fullName);
if (templateArguments[templateParameterIndex] && templateArguments[templateParameterIndex]->IsType())
{
TypePtr prevArgumentType = std::static_pointer_cast<Type>(templateArguments[templateParameterIndex]);
if (TypesEqual(argumentType, prevArgumentType))
{
return true;
}
else if (argumentType->HasBaseClass(prevArgumentType))
{
boundType = prevArgumentType;
FunctionPtr ctor = GetBaseDerivedConversion(boundType, argumentType, ConversionOption::implicit);
conversion = ctor;
return true;
}
return false;
}
else
{
scope->SetObject(BoundTemplateParameterPtr(new BoundTemplateParameter(parameterObject->Name(), templateParameter->Pos(),
templateParameter->EnclosingScope(), templateParameter->FileScope(), argumentType)));
templateArguments[templateParameterIndex] = argumentType;
}
return true;
}
else if (parameterObject->IsBoundTemplateParameter()) // bound template parameter
{
BoundTemplateParameterPtr boundTemplateParameter = std::static_pointer_cast<BoundTemplateParameter>(parameterObject);
return TypesEqual(boundTemplateParameter->GetType(), argumentType);
}
return false;
}
TypePtr Name::GetType() const
{
ObjectPtr to = linkedTo.lock();
return to ? to->GetType() : TypePtr();
}
} } // namespace Cm::Core