Summary: Make it possible to write one model which will be used by all template instantiations. There is one big missing piece: infer never tries to do template instantiation by itself. With current code, it's possible to use generic models as long as header contains `__infer_generic_model` annotation (see the test as an example). This is not viable to modify all headers with this annotation hence infer will try to do template instantiation for generic models in later diffs. Reviewed By: jberdine Differential Revision: D4826365 fbshipit-source-id: 2233e42master
parent
a90525f627
commit
5503487704
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (c) 2017 - present Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
#include "generic_model.h"
|
||||
|
||||
template <class T>
|
||||
T* GenericModelClass<T>::get() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T* NonGenericModelClass<T>::get() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T* genericModelFunction() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T* nonGenericModelFunction() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// explicit instantiations with <long long> as template argument
|
||||
template class GenericModelClass<long long>;
|
||||
template class NonGenericModelClass<long long>;
|
||||
template long long* genericModelFunction<long long>();
|
||||
template long long* nonGenericModelFunction<long long>();
|
@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (c) 2017 - present Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
template <class T>
|
||||
struct __attribute__((annotate("__infer_generic_model"))) GenericModelClass {
|
||||
T* get();
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct NonGenericModelClass {
|
||||
T* get();
|
||||
};
|
||||
|
||||
template <class T>
|
||||
__attribute__((annotate("__infer_generic_model"))) T* genericModelFunction();
|
||||
|
||||
template <class T>
|
||||
T* nonGenericModelFunction();
|
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2017 - present Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
#include "generic_model.h"
|
||||
|
||||
/* This code uses <int> template instantiations, but those are never
|
||||
instantiated because there is no implementation of templated code in
|
||||
"generic_model.h"
|
||||
However, there is instantation for <long long> coming from
|
||||
"generic_model.cpp". If generic model is truly generic, then infer will pick
|
||||
up specs for those and use them
|
||||
*/
|
||||
int genericModelNPE() {
|
||||
GenericModelClass<int> x;
|
||||
auto ptr = x.get();
|
||||
return *ptr;
|
||||
}
|
||||
|
||||
int nonGenericModelNoNPE() {
|
||||
NonGenericModelClass<int> x;
|
||||
auto ptr = x.get(); // this will be skip function
|
||||
return *ptr;
|
||||
}
|
||||
|
||||
int genericModelFunctionNPE() {
|
||||
auto ptr = genericModelFunction<int>();
|
||||
return *ptr;
|
||||
}
|
||||
int nonGenericModelFunctionNPE() {
|
||||
auto ptr = nonGenericModelFunction<int>(); // this will be skip function
|
||||
return *ptr;
|
||||
}
|
Loading…
Reference in new issue