void cGraphMaster::do_substitutions(string& input, const SubstitutionList& subs_vec) const { for (SubstitutionList::const_iterator it = subs_vec.begin(); it != subs_vec.end(); ++it) { // from is already lowercase const string& from = it->from; const string& to = it->to; if (!it->regex) { for (size_t i = 0; i < input.length(); i++) { size_t j; for (j = 0; (i+j) < input.length() && j < from.length(); j++) { if (tolower(input[i+j]) != from[j]) break; } if (j == from.length()) { // even if there's a match I need that chars around the matching part be [^A-Za-z0-9'] if (((i > 0 && !isalnum(input[i-1]) && input[i-1] != '\'') || i == 0) && (((i + from.length()) < input.length() && !isalnum(input[i + from.length()]) && input[i + from.length()] != '\'') || i + from.length() >= input.length())) { input.replace(i, from.length(), to); if (!to.empty()) i += (to.length() - 1); } } } } else { #ifdef ENABLE_PCRECPP pcrecpp::RE(from).GlobalReplace(to, &input); #endif } } }
/// Compute substitutions for making a direct call to a SIL function with /// @convention(witness_method) convention. /// /// Such functions have a substituted generic signature where the /// abstract `Self` parameter from the original type of the protocol /// requirement is replaced by a concrete type. /// /// Thus, the original substitutions of the apply instruction that /// are written in terms of the requirement's generic signature need /// to be remapped to substitutions suitable for the witness signature. /// /// \param conformanceRef The (possibly-specialized) conformance /// \param requirementSig The generic signature of the requirement /// \param witnessThunkSig The generic signature of the witness method /// \param origSubs The substitutions from the call instruction /// \param newSubs New substitutions are stored here static void getWitnessMethodSubstitutions( SILModule &M, ProtocolConformanceRef conformanceRef, GenericSignature *requirementSig, GenericSignature *witnessThunkSig, SubstitutionList origSubs, bool isDefaultWitness, SmallVectorImpl<Substitution> &newSubs) { if (witnessThunkSig == nullptr) return; if (isDefaultWitness) { newSubs.append(origSubs.begin(), origSubs.end()); return; } assert(!conformanceRef.isAbstract()); auto conformance = conformanceRef.getConcrete(); // If `Self` maps to a bound generic type, this gives us the // substitutions for the concrete type's generic parameters. auto baseSubMap = getSubstitutionsForProtocolConformance(conformanceRef); unsigned baseDepth = 0; auto *rootConformance = conformance->getRootNormalConformance(); if (auto *witnessSig = rootConformance->getGenericSignature()) baseDepth = witnessSig->getGenericParams().back()->getDepth() + 1; auto origDepth = 1; auto origSubMap = requirementSig->getSubstitutionMap(origSubs); auto subMap = SubstitutionMap::combineSubstitutionMaps(baseSubMap, origSubMap, CombineSubstitutionMaps::AtDepth, baseDepth, origDepth, witnessThunkSig); witnessThunkSig->getSubstitutions(subMap, newSubs); }
int Specialization::substituteVariablesWithConst(SgNode* node, ConstReporter* constReporter) { typedef pair<SgExpression*,int> SubstitutionPair; typedef list<SubstitutionPair > SubstitutionList; SubstitutionList substitutionList; RoseAst ast(node); for(RoseAst::iterator i=ast.begin();i!=ast.end();++i) { if(constReporter->isConst(*i)) { int varIntValue=constReporter->getConstInt(); SgVarRefExp* varRefExp=constReporter->getVarRefExp(); SubstitutionPair p=make_pair(varRefExp,varIntValue); substitutionList.push_back(p); } } for(SubstitutionList::iterator i=substitutionList.begin(); i!=substitutionList.end(); ++i) { // buildSignedIntType() // buildFloatType() // buildDoubleType() // SgIntVal* buildIntVal(int) SgNodeHelper::replaceExpression((*i).first,SageBuilder::buildIntVal((*i).second),false); } return (int)substitutionList.size(); }