Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions ecmascript/compiler/codegen/maple/maple_ir/include/parse_preg_no.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2026 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef PARSE_PREG_NO_H
#define PARSE_PREG_NO_H

#include <charconv>
#include <cstdint>
#include <string>
#include <system_error>

inline bool ParsePregNo(const std::string &s, uint32_t &out)
{
if (s.empty()) {
return false;
}
uint32_t value = 0;
const char *first = s.data();
const char *last = first + s.size();
auto result = std::from_chars(first, last, value);
if (result.ec != std::errc() || result.ptr != last) {
return false;
}
out = value;
return true;
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
*/

#include "mir_function.h"
#include "parse_preg_no.h"
#include "printing.h"
#include <cctype>

namespace {
using namespace maple;
Expand Down Expand Up @@ -440,12 +442,13 @@ void MIRFunction::EnterFormals()
formalDef.formalSym->SetTyIdx(formalDef.formalTyIdx);
formalDef.formalSym->SetAttrs(formalDef.formalAttrs);
const std::string &formalName = GlobalTables::GetStrTable().GetStringFromStrIdx(formalDef.formalStrIdx);
if (!isdigit(formalName.front())) {
uint32 thepregno = 0;
if (formalName.empty() || !isdigit(static_cast<unsigned char>(formalName.front())) ||
!ParsePregNo(formalName, thepregno)) {
formalDef.formalSym->SetSKind(kStVar);
(void)symTab->AddToStringSymbolMap(*formalDef.formalSym);
} else {
formalDef.formalSym->SetSKind(kStPreg);
uint32 thepregno = static_cast<uint32>(std::stoi(formalName));
MIRType *mirType = GlobalTables::GetTypeTable().GetTypeFromTyIdx(formalDef.formalTyIdx);
PrimType pType = mirType->GetPrimType();
// if mirType info is not needed, set mirType to nullptr
Expand Down