2
// Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved.
3
// Use of this source code is governed by a BSD-style license that can be
4
// found in the LICENSE file.
7
#include "compiler/DetectRecursion.h"
9
DetectRecursion::FunctionNode::FunctionNode(const TString& fname)
15
const TString& DetectRecursion::FunctionNode::getName() const
20
void DetectRecursion::FunctionNode::addCallee(
21
DetectRecursion::FunctionNode* callee)
23
for (size_t i = 0; i < callees.size(); ++i) {
24
if (callees[i] == callee)
27
callees.push_back(callee);
30
bool DetectRecursion::FunctionNode::detectRecursion()
32
ASSERT(visit == PreVisit);
34
for (size_t i = 0; i < callees.size(); ++i) {
35
switch (callees[i]->visit) {
37
// cycle detected, i.e., recursion detected.
42
bool recursion = callees[i]->detectRecursion();
56
DetectRecursion::DetectRecursion()
57
: currentFunction(NULL)
61
DetectRecursion::~DetectRecursion()
63
for (size_t i = 0; i < functions.size(); ++i)
67
bool DetectRecursion::visitAggregate(Visit visit, TIntermAggregate* node)
69
switch (node->getOp())
72
// Function declaration.
73
// Don't add FunctionNode here because node->getName() is the
74
// unmangled function name.
77
// Function definition.
78
if (visit == PreVisit) {
79
currentFunction = findFunctionByName(node->getName());
80
if (currentFunction == NULL) {
81
currentFunction = new FunctionNode(node->getName());
82
functions.push_back(currentFunction);
87
case EOpFunctionCall: {
89
if (visit == PreVisit) {
90
ASSERT(currentFunction != NULL);
91
FunctionNode* func = findFunctionByName(node->getName());
93
func = new FunctionNode(node->getName());
94
functions.push_back(func);
96
currentFunction->addCallee(func);
106
DetectRecursion::ErrorCode DetectRecursion::detectRecursion()
108
FunctionNode* main = findFunctionByName("main(");
110
return kErrorMissingMain;
111
if (main->detectRecursion())
112
return kErrorRecursion;
116
DetectRecursion::FunctionNode* DetectRecursion::findFunctionByName(
119
for (size_t i = 0; i < functions.size(); ++i) {
120
if (functions[i]->getName() == name)