Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
vczh committed Jan 25, 2020
1 parent 8694870 commit 07193fb
Show file tree
Hide file tree
Showing 3 changed files with 795 additions and 37 deletions.
157 changes: 126 additions & 31 deletions Tools/CppDoc/Core/Source/Ast_Resolving_InferFunctionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,43 +207,132 @@ namespace symbol_type_resolving
{
}

void ExecuteOnce(Ptr<Type> argumentType, ITsys* _offeredType, bool _exactMatch)
void ExecuteInvolvedOnce(Ptr<Type>& argumentType, ITsys* _offeredType, bool _exactMatch = true)
{
if (!involvedTypes.Contains(argumentType.Obj())) return;
offeredType = _offeredType;
exactMatch = exactMatch;
argumentType->Accept(this);
if (!offeredType->IsUnknownType())
{
argumentType->Accept(this);
}
}

void ExecuteOnce(Ptr<Type>& argumentType, ITsys* _offeredType, bool _exactMatch = true)
{
if (!involvedTypes.Contains(argumentType.Obj())) return;
ExecuteInvolvedOnce(argumentType, _offeredType, _exactMatch);
}

void Execute(Ptr<Type> argumentType, ITsys* _offeredType)
void Execute(Ptr<Type>& argumentType, ITsys* _offeredType)
{
InferTemplateArgumentVisitor(pa, taContext, variadicContext, freeTypeSymbols, involvedTypes)
.ExecuteOnce(argumentType, _offeredType, true);
.ExecuteOnce(argumentType, _offeredType);
}

void Visit(PrimitiveType* self)override
{
if(!involvedTypes.Contains(self)) return;
// TODO: not implemented
throw 0;
}

void VisitArrayOrPtr(Ptr<Type>& elementType, TsysType tsysType)
{
if (exactMatch)
{
if (offeredType->GetType() != tsysType)
{
throw TypeCheckerException();
}
ExecuteInvolvedOnce(elementType, offeredType->GetElement());
}
else
{
TsysCV cv;
TsysRefType refType;
auto entity = offeredType->GetEntity(cv, refType);
if (entity->GetType() != TsysType::Ptr && entity->GetType() != TsysType::Array)
{
throw TypeCheckerException();
}
ExecuteInvolvedOnce(elementType, entity->GetElement());
}
}

void Visit(ReferenceType* self)override
{
// TODO: not implemented
throw 0;
switch (self->reference)
{
case CppReferenceType::Ptr:
VisitArrayOrPtr(self->type, TsysType::Ptr);
break;
case CppReferenceType::LRef:
{
if (exactMatch)
{
switch (offeredType->GetType())
{
case TsysType::LRef:
ExecuteInvolvedOnce(self->type, offeredType->GetElement());
break;
default:
throw TypeCheckerException();
}
}
else
{
switch (offeredType->GetType())
{
case TsysType::LRef:
ExecuteInvolvedOnce(self->type, offeredType->GetElement());
break;
case TsysType::RRef:
throw TypeCheckerException();
default:
ExecuteInvolvedOnce(self->type, offeredType->CVOf({ true,false })->LRefOf());
}
}
}
break;
case CppReferenceType::RRef:
{
if (exactMatch)
{
switch (offeredType->GetType())
{
case TsysType::LRef:
ExecuteInvolvedOnce(self->type, offeredType);
break;
case TsysType::RRef:
ExecuteInvolvedOnce(self->type, offeredType->GetElement());
break;
default:
throw TypeCheckerException();
}
}
else
{
switch (offeredType->GetType())
{
case TsysType::LRef:
ExecuteInvolvedOnce(self->type, offeredType);
break;
case TsysType::RRef:
ExecuteInvolvedOnce(self->type, offeredType->GetElement());
default:
ExecuteInvolvedOnce(self->type, offeredType);
}
}
}
break;
}
}

void Visit(ArrayType* self)override
{
// TODO: not implemented
throw 0;
VisitArrayOrPtr(self->type, TsysType::Array);
}

void Visit(CallingConventionType* self)override
{
// TODO: not implemented
throw 0;
self->type->Accept(this);
}

void Visit(FunctionType* self)override
Expand All @@ -260,20 +349,28 @@ namespace symbol_type_resolving

void Visit(DeclType* self)override
{
// TODO: not implemented
throw 0;
}

void Visit(DecorateType* self)override
{
// TODO: not implemented
throw 0;
TsysCV cv;
TsysRefType refType;
auto entity = offeredType->GetEntity(cv, refType);
if(exactMatch)
{
if (refType != TsysRefType::None) throw TypeCheckerException();
if (self->isConst != cv.isGeneralConst) throw TypeCheckerException();
if (self->isVolatile != cv.isVolatile) throw TypeCheckerException();
ExecuteInvolvedOnce(self->type, entity);
}
else
{
ExecuteInvolvedOnce(self->type, entity);
}
}

void Visit(RootType* self)override
{
// TODO: not implemented
throw 0;
}

void Visit(IdType* self)override
Expand Down Expand Up @@ -365,19 +462,17 @@ namespace symbol_type_resolving
}
}

if (assignedTsys->GetType() == TsysType::Any)
// infer all affected types to any_t, result will be overrided if more precise types are inferred
for (vint j = 0; j < vas.Count(); j++)
{
// if the assigned argument is any_t, infer all affected types to any_t
for (vint j = 0; j < vas.Count(); j++)
{
SetInferredResult(taContext, vas[j], pa.tsys->Any());
}
for (vint j = 0; j < nvas.Count(); j++)
{
SetInferredResult(taContext, nvas[j], pa.tsys->Any());
}
SetInferredResult(taContext, vas[j], pa.tsys->Any());
}
else
for (vint j = 0; j < nvas.Count(); j++)
{
SetInferredResult(taContext, nvas[j], pa.tsys->Any());
}

if (assignedTsys->GetType() != TsysType::Any)
{
if (parameter.isVariadic)
{
Expand Down
13 changes: 7 additions & 6 deletions Tools/CppDoc/UnitTest/TestOverloadingGenericFunctionInfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ namespace Input__TestOverloadingGenericFunction_TypeInferKinds
template<typename T> auto LRef(T&) -> Types<T> ;
template<typename T> auto RRef(T&&) -> Types<T> ;
template<typename T> auto Pointer(T*) -> Types<T> ;
template<typename R, typename... TArgs> auto Function(R(*)(TArgs...)) -> Types<R, TArgs...> ;
template<typename T, typename U> auto Member(T U::*) -> Types<T, U> ;
template<typename T> auto C(const T) -> Types<T> ;
template<typename T> auto V(volatile T) -> Types<T> ;
template<typename T> auto CV(const volatile T) -> Types<T> ;
template<typename R, typename... TArgs> auto Function(R(*)(TArgs...)) -> Types<R, TArgs...> ;
template<typename T, typename U> auto Member(T U::*) -> Types<T, U> ;
template<typename... TArgs> auto VtaPtr(TArgs*...) -> Types<TArgs...> ;
template<typename... TArgs> auto VtaTypes(Types<TArgs...>) -> Types<TArgs...> ;
template<typename R, typename... TArgs> auto VtaFunc(Types<R(*)(TArgs*)...>) -> Types<R, TArgs...> ;
Expand Down Expand Up @@ -221,10 +221,6 @@ TEST_FILE
ASSERT_OVERLOADING_FORMATTED_VERBOSE(Pointer(Value<bool const *>()), L"::Types<{bool const $PR}> $PR", Types<bool const>);
ASSERT_OVERLOADING_FORMATTED_VERBOSE(Pointer(Value<bool volatile *>()), L"::Types<{bool volatile $PR}> $PR", Types<bool volatile>);
ASSERT_OVERLOADING_FORMATTED_VERBOSE(Pointer(Value<bool const volatile *>()), L"::Types<{bool const volatile $PR}> $PR", Types<bool const volatile>);

ASSERT_OVERLOADING_FORMATTED_VERBOSE(Function(Value<FunctionOf<bool>>()), L"::Types<{bool $PR}> $PR", Types<bool>);
ASSERT_OVERLOADING_FORMATTED_VERBOSE(Function(Value<FunctionOf<bool, float, double>>()), L"::Types<{bool $PR, float $PR, double $PR}> $PR", Types<bool, float, double>);
ASSERT_OVERLOADING_FORMATTED_VERBOSE(Member(Value<MemberOf<bool, Types<>>>()), L"::Types<{bool $PR, ::Types<{}> $PR}> $PR", Types<bool, Types<>>);

ASSERT_OVERLOADING_FORMATTED_VERBOSE(C(Value<bool>()), L"::Types<{bool $PR}> $PR", Types<bool>);
ASSERT_OVERLOADING_FORMATTED_VERBOSE(C(Value<bool const>()), L"::Types<{bool $PR}> $PR", Types<bool>);
Expand All @@ -238,6 +234,11 @@ TEST_FILE
ASSERT_OVERLOADING_FORMATTED_VERBOSE(CV(Value<bool const>()), L"::Types<{bool $PR}> $PR", Types<bool>);
ASSERT_OVERLOADING_FORMATTED_VERBOSE(CV(Value<bool volatile>()), L"::Types<{bool $PR}> $PR", Types<bool>);
ASSERT_OVERLOADING_FORMATTED_VERBOSE(CV(Value<bool const volatile>()), L"::Types<{bool $PR}> $PR", Types<bool>);

return;
ASSERT_OVERLOADING_FORMATTED_VERBOSE(Function(Value<FunctionOf<bool>>()), L"::Types<{bool $PR}> $PR", Types<bool>);
ASSERT_OVERLOADING_FORMATTED_VERBOSE(Function(Value<FunctionOf<bool, float, double>>()), L"::Types<{bool $PR, float $PR, double $PR}> $PR", Types<bool, float, double>);
ASSERT_OVERLOADING_FORMATTED_VERBOSE(Member(Value<MemberOf<bool, Types<>>>()), L"::Types<{bool $PR, ::Types<{}> $PR}> $PR", Types<bool, Types<>>);

ASSERT_OVERLOADING_FORMATTED_VERBOSE(
VtaPtr(Value<bool *>(), Value<bool const *>(), Value<bool volatile *>()),
Expand Down
Loading

0 comments on commit 07193fb

Please sign in to comment.